Mybatis-Plus common annotations

Table of contents

Mybatis-Plus common annotations

1. @TableName

2、@TableId

2.1 IdType

3、@TableField

4、@Version

5、@EnumValue

6、@TableLogic

7、@KeySequence

8、@OrderBy


Mybatis-Plus common annotations

1. @TableName

  • Description: Table name annotation, identifying the table corresponding to the entity class
  • Where to use: Entity class
@TableName("sys_user")
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

 

Attributes type Must be specified default value describe
value String no "" Table Name
schema String no "" schema
keepGlobalPrefix boolean no false Whether to keep using the global tablePrefix value (when the global tablePrefix is ​​in effect)
resultMap String no "" The id of resultMap in xml (used to satisfy specific types of entity class object binding)
autoResultMap boolean no false Whether to automatically build and use resultMap (if resultMap is set, automatic construction and injection of resultMap will not be performed)
excludeProperty String[] no {} Attribute names to be excluded

Description about  autoResultMap :

MP will automatically build one  resultMap and inject it into MyBatis (generally not used), please note the following:

Because the bottom layer of MP is MyBatis, MP just helps you inject commonly used CRUD into MyBatis. Before the injection, it is dynamic (changes according to your Entity fields and annotations), but after the injection, it is static (equal to the content in the XML configuration ).

For  typeHandler attributes, MyBatis only supports writing in 2 places:

  1. Defined in resultMap, used to encapsulate query results
  2. Defined   after  the insert and  update statement   (for example: ), and only applies to the current #{property}property#{property,typehandler=xxx.xxx.xxx}设置值

In addition to the above two directly specified  typeHandler forms, MyBatis has a configuration for global scanning of custom  packages. The principle is  to find the corresponding one  typeHandler according to your  type  and use it.propertytypeHandler

2、@TableId

  • Description: Primary key annotation
  • Where to use: Entity class primary key field
@TableName("sys_user")
public class User {
    @TableId
    private Long id;
    private String name;
    private Integer age;
    private String email;
}
Attributes type Must be specified default value describe
value String no "" Primary key field name
type Enum no IdType.NONE Specify primary key type

2.1 IdType

Attributes

illustrate

AUTO Database ID auto-increment
NONE Stateless, this type has no primary key set (the annotation is equivalent to following the global, and the global Rio is approximately equal to INPUT)
INPUT Set the primary key value before inserting
ASSIGN_ID Assign ID (primary key type is Number (Long and Integer) or String) (since 3.3.0), use the interface IdentifierGeneratormethod nextId(the default implementation class is DefaultIdentifierGeneratorSnowflake algorithm)
ASSIGN_UUID Assign UUID, primary key type is String (since 3.3.0), use interface IdentifierGeneratormethod nextUUID(default default method)

3、@TableField

  • Description: Field annotation (non-primary key)
@TableName("sys_user")
public class User {
    @TableId
    private Long id;
    @TableField("nickname")
    private String name;
    private Integer age;
    private String email;
}
Attributes type Must be specified default value describe
value String no "" Database field name
exist boolean no true Whether it is a database table field
condition String no "" Field  where entity query comparison conditions. If there is a value set, the set value will prevail. If not, it will be global by default  %s=#{%s}. Please refer to (opens new window)
update String no "" Field  update set partial injection, for example: when annotating the version field to update="%s+1" indicate an update  set version=version+1 (this attribute has higher priority than  el the attribute)
insertStrategy Enum no FieldStrategy.DEFAULT Example: NOT_NULL
insert into table_a(<if test="columnProperty != null">column</if>) values (<if test="columnProperty != null">#{columnProperty}</if>)
updateStrategy Enum FieldStrategy.DEFAULT 举例:IGNORED
update table_a set column=#{columnProperty}
whereStrategy Enum FieldStrategy.DEFAULT 举例:NOT_EMPTY
where <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
fill Enum FieldFill.DEFAULT 字段自动填充策略
select boolean true 是否进行 select 查询
keepGlobalFormat boolean false 是否保持使用全局的 format 进行处理
jdbcType JdbcType JdbcType.UNDEFINED JDBC 类型 (该默认值不代表会按照该值生效)
typeHandler Class<? extends TypeHandler> UnknownTypeHandler.class 类型处理器 (该默认值不代表会按照该值生效)
numericScale String "" 指定小数点后保留的位数

 注意:

关于`jdbcType`和`typeHandler`以及`numericScale`的说明:

numericScale只生效于 update 的 sql. jdbcTypetypeHandler如果不配合@TableName#autoResultMap = true一起使用,也只生效于 update 的 sql. 对于typeHandler如果你的字段类型和 set 进去的类型为equals关系,则只需要让你的typeHandler让 Mybatis 加载到即可,不需要使用注解

FieldStrategy:

描述
IGNORED 忽略判断
NOT_NULL 非 NULL 判断
NOT_EMPTY 非空判断(只对字符串类型字段,其他类型字段依然为非 NULL 判断)
DEFAULT 追随全局配置
NEVER 不加入SQL

 FieldFill:

描述
DEFAULT 默认不处理
INSERT 插入时填充字段
UPDATE 更新时填充字段
INSERT_UPDATE 插入和更新时填充字段

4、@Version

描述:乐观锁注解、标记 @Version 在字段上

5、@EnumValue

描述:普通枚举类注解(注解在枚举字段上)

6、@TableLogic

Description: Table field logical processing annotations (logical deletion)

Attributes type Must be specified default value describe
value String no "" logical undeleted value
delval String no "" tombstone value

7、@KeySequence

  • Description: Sequence primary key strategy oracle
  • Attributes: value, dbType
Attributes type Must be specified default value describe
value String no "" sequence name
dbType Enum no DbType.OTHER Database type. If not configured, the default implementation is to inject IKeyGenerator. Multiple implementations must be specified.

8、@OrderBy

Description: Built-in SQL specifies sorting by default, with a priority lower than wrapper conditional query

Attributes type Must be specified default value describe
isDesc boolean no true Whether to query in reverse order
sort short no Short.MAX_VALUE The smaller the number, the higher it is

 

 Reference: Annotations | MyBatis-Plus

Guess you like

Origin blog.csdn.net/qi341500/article/details/132651227