SoFunction
Updated on 2025-03-03

@TableField annotation in depth and application methods

@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@TableFieldAnnotation is a very important annotation that defines the mapping relationship between entity class fields and database table fields.

This article will introduce in detail@TableFieldThe 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

@TableFieldAnnotation is an annotation provided by MyBatis-Plus, which is used to identify the mapping relationship between entity class fields and database table fields.

pass@TableFieldAnnotation: 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

@TableFieldAnnotations 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 asLIKEEQUALwait.

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@TableFieldAnnotatedvalueAttributes 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 fieldsidMap to database table fieldsuser_id@TableField("user_name")Put entity class fieldsuserNameMap 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@TableFieldAnnotatedexistProperties that specify whether this field is a database table field.

WillexistThe 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 fieldsageIdentified 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@TableFieldAnnotatedfillProperties, 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 = )SpecifycreateTimeThe fields are automatically filled when inserted.@TableField(fill = FieldFill.INSERT_UPDATE)SpecifyupdateTimeFields 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 asLIKEEQUALwait. pass@TableFieldAnnotatedconditionProperties 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}, '%%')")

SpecifyuserNameFields are used in query conditionsLIKEMatch 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@TableFieldAnnotatedselectProperties 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)SpecifydeletedThe fields do not participate in the query.@TableLogicIdentifies 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@TableFieldAnnotation 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@TableFieldAnnotation 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

@TableFieldAnnotations 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@TableFieldAnnotation 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@TableFieldAnnotation to achieve flexible and efficient database operations.

Through the introduction of this article, I hope readers can fully understand@TableFieldUse 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.