MyBatis Plus is a powerful persistence layer framework that provides many convenient annotations and configurations, including @TableField
annotations for configuring field mapping.
@TableField annotation
@TableField
Annotations are used to configure field mapping information, and can be used to specify attributes such as database field names and field strategies in entity classes.
basic usage
import com.baomidou.mybatisplus.annotation.TableField;
public class User {
@TableField("user_name")
private String username;
// 省略其他字段和方法
}
In the above code, @TableField("user_name")
the annotation specifies username
the database field name corresponding to the field user_name
.
Detailed attribute
@TableField
Annotations have the following common properties:
- value: specifies the column name of the field in the database, and expressions can be used.
- exist: Specifies whether the field is a field in the database table, the default is
true
. - fill: The specified field is an auto-fill field, and the optional values are
FieldFill.DEFAULT
,FieldFill.INSERT
, andFieldFill.UPDATE
so on. - select: Specifies whether to query when the field is queried, the default is
true
. - condition: Specify the condition of the field during update operation, the default is an empty string.
complete example
- Entity class
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
public class User {
@TableField(value = "user_name", exist = true, fill = FieldFill.DEFAULT, select = true, condition = "")
private String username;
// 省略其他字段和方法
}
- configuration file
In the configuration file of MyBatis Plus (for example mybatis-config.xml
), @TableField
support for annotations needs to be added:
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
<typeAliases>
<package name="com.example.entity" />
</typeAliases>
</configuration>
- quote
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
public interface UserMapper extends BaseMapper<User> {
}
public class UserService extends ServiceImpl<UserMapper, User> {
}
In the above code, UserMapper
it is a custom Mapper interface, which is inherited from BaseMapper
; UserService
it is a custom Service class, which is inherited from ServiceImpl
.
In this way, we can perform database operations through UserMapper
and UserService
, and @TableField
the configuration in the annotation will also take effect.
The above is @TableField
the basic usage and configuration instructions of the annotation, I hope it can be helpful to you!