@TableField annotation: In-depth understanding and application
introduction
In modern software development, database operations are an indispensable part. In order to simplify database operations and improve development efficiency, many ORM (Object-Relational Mapping) frameworks emerged.
MyBatis-Plus is an enhancement tool for MyBatis, providing rich features and annotations, among which@TableField
Annotation is a very important annotation that defines the mapping relationship between entity class fields and database table fields.
This article will introduce in detail@TableField
The usage scenarios, attributes and their application in actual development help readers fully understand and master this important annotation.
Introduction to MyBatis-Plus
MyBatis-Plus (MP for short) is a MyBatis enhancement tool designed to simplify development and improve efficiency.
It provides many convenient functions, such as code generation, paging plug-ins, performance analysis plug-ins, etc., allowing developers to focus more on the implementation of business logic without paying too much attention to the underlying database operations.
@TableField annotation overview
Basic concepts
@TableField
Annotation is an annotation provided by MyBatis-Plus, which is used to identify the mapping relationship between entity class fields and database table fields.
pass@TableField
Annotation: You can specify the name of the field, whether it is a database field, whether it is a primary key, or whether it is a logical delete field, so as to achieve more flexible database operations.
Common properties
@TableField
Annotations provide multiple attributes, commonly used attributes include:
-
value
: Specify the name of the database table field. -
exist
: Specify whether this field is a database table field, the default istrue
。 -
fill
: Specify field filling policy, such as automatic filling when inserting, automatic filling when updating, etc. -
select
: Specify whether this field participates in the query, the default istrue
。 -
update
: Specify whether this field participates in the update, the default istrue
。 -
condition
: Specify how this field matches in the query condition, such asLIKE
、EQUAL
wait.
Use scenarios of @TableField annotation
1. Field name mapping
In actual development, the naming specification of database table fields may be inconsistent with the naming specification of entity class fields.
For example, database table fields are underlined (e.g.user_name
), while entity class fields use camel nomenclature (e.g.userName
). pass@TableField
Annotatedvalue
Attributes that specify the name of the database table field to implement the mapping of field names.
Sample code:
import ; import ; @TableName("user") public class User { @TableField("user_id") private Long id; @TableField("user_name") private String userName; // Other fields and methods}
In the above example,@TableField("user_id")
Put entity class fieldsid
Map to database table fieldsuser_id
,@TableField("user_name")
Put entity class fieldsuserName
Map to database table fieldsuser_name
。
2. Non-database field identification
In some cases, the entity class may contain some non-database fields, such as computed properties, temporary variables, etc.
pass@TableField
Annotatedexist
Properties that specify whether this field is a database table field.
Willexist
The property is set tofalse
, means that this field is not a database table field.
Sample code:
import ; import ; @TableName("user") public class User { private Long id; private String userName; @TableField(exist = false) private Integer age; // Other fields and methods}
In the above example,@TableField(exist = false)
Put entity class fieldsage
Identified as a non-database field, which MyBatis-Plus ignores when generating SQL statements.
3. Field fill strategy
In actual development, the values of certain fields may need to be automatically filled when inserted or updated, such as creation time, update time, etc. pass@TableField
Annotatedfill
Properties, you can specify the field fill policy to realize the automatic fill function.
Sample code:
import ; import ; import ; @TableName("user") public class User { private Long id; private String userName; @TableField(fill = ) private Date createTime; @TableField(fill = FieldFill.INSERT_UPDATE) private Date updateTime; // Other fields and methods}
In the above example,@TableField(fill = )
SpecifycreateTime
The fields are automatically filled when inserted.@TableField(fill = FieldFill.INSERT_UPDATE)
SpecifyupdateTime
Fields are automatically populated when inserted and updated.
4. Field query policy
In some cases, it may be necessary to specify how fields match in the query conditions, such asLIKE
、EQUAL
wait. pass@TableField
Annotatedcondition
Properties that specify how fields match in query conditions.
Sample code:
import ; import ; import ; @TableName("user") public class User { private Long id; @TableField(condition = "%s LIKE CONCAT('%%', #{%s}, '%%')") private String userName; // Other fields and methods} // Query exampleQueryWrapper<User> queryWrapper = new QueryWrapper<>(); ("user_name", "open"); List<User> userList = (queryWrapper);
In the above example,
@TableField(condition = "%s LIKE CONCAT('%%', #{%s}, '%%')")
SpecifyuserName
Fields are used in query conditionsLIKE
Match method.
5. Logical deletion field identifier
In some cases, it may be necessary to use logical deletion rather than physical deletion, i.e. identify whether the record is deleted by a field. pass@TableField
Annotatedselect
Properties that specify whether this field participates in the query.
Sample code:
import ; import ; import ; @TableName("user") public class User { private Long id; private String userName; @TableField(select = false) @TableLogic private Integer deleted; // Other fields and methods}
In the above example,@TableField(select = false)
Specifydeleted
The fields do not participate in the query.@TableLogic
Identifies this field as a logical delete field.
Practical application cases
Case 1: User Management System
In a user management system, it is necessary to add, delete, modify and check user information. pass@TableField
Annotation can realize field name mapping, non-database field identification, field filling strategy and other functions.
Sample code:
import ; import ; import ; import ; @TableName("user") public class User { @TableField("user_id") private Long id; @TableField("user_name") private String userName; @TableField(exist = false) private Integer age; @TableField(fill = ) private Date createTime; @TableField(fill = FieldFill.INSERT_UPDATE) private Date updateTime; @TableField(select = false) @TableLogic private Integer deleted; // Other fields and methods}
Case 2: Order Management System
In an order management system, it is necessary to add, delete, modify and check order information. pass@TableField
Annotation can realize field name mapping, field query strategy and other functions.
Sample code:
import ; import ; import ; @TableName("order") public class Order { @TableField("order_id") private Long id; @TableField("order_no") private String orderNo; @TableField(condition = "%s LIKE CONCAT('%%', #{%s}, '%%')") private String customerName; // Other fields and methods} // Query exampleQueryWrapper<Order> queryWrapper = new QueryWrapper<>(); ("customer_name", "open"); List<Order> orderList = (queryWrapper);
Summarize
@TableField
Annotations are a very important annotation provided by MyBatis-Plus, which are used to define the mapping relationship between entity class fields and database table fields.
pass@TableField
Annotation can realize field name mapping, non-database field identification, field filling strategy, field query strategy, logical deletion field identification and other functions, thereby simplifying database operations and improving development efficiency. In actual development, it should be used reasonably according to specific needs@TableField
Annotation to achieve flexible and efficient database operations.
Through the introduction of this article, I hope readers can fully understand@TableField
Use scenarios, attributes and their application in actual development. Master this important annotation and flexibly apply it in actual development to achieve high-quality software delivery.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.