mybatis-plus常用注解符
1. 表名注解(@TableName)
作用:實體類和數(shù)據(jù)庫中表建立對應(yīng)關(guān)系:如
1
2
3
4
5
6
7
8
9
10
|
@TableName ( "thotset" ) public class HotsetEntity implements Serializable { private static final long serialVersionUID = 1L; private Integer fclass; private Integer fpwid; @JsonFormat (pattern = "yyyy-MM-dd" ) private Date fbdate; @JsonFormat (pattern = "yyyy-MM-dd" ) private Date fedate; } |
代表:HotsetEntity 對應(yīng)數(shù)據(jù)庫中表為thotset
2. 主鍵注解(@TableId)
作用:標(biāo)識實體類的屬性對應(yīng)的是表中的主鍵,還配置主鍵的生成策略,如:
1
2
3
4
5
6
7
8
9
|
@TableName ( "tsvbase" ) public class PaintLifeEntity implements Serializable { private static final long serialVersionUID = 1L; @TableId (type = IdType.AUTO) private String recid; private String fcode; private String fname; } |
代表:recid是表中的主鍵,主鍵的生成策略為自增類型。
在mybaits-plus中主鍵生成策略及注意事項
IdType.ASSIGN_ID
: 主鍵類型為長整型或字符串,使用這類主鍵時要注意,在前端長整形在數(shù)據(jù)轉(zhuǎn)換時和整型長度不匹配問題,會引發(fā)錯誤。
IdType.ASSIGN_UUID
:主鍵類型為String,為32為不重復(fù)字符串。注意該字符串為亂序,使用它時注意一條記錄,頁面刷新后新增的記錄并非最后一條或第一條,經(jīng)常找不到新增的記錄,維護(hù)時用戶體驗很差。
IdType.AUTO
:自增;
IdType.input
: 插入數(shù)據(jù)前需要使用其他方式得到主鍵,將得到的數(shù)據(jù)賦值到主鍵上。
IdType.NONE
:無狀態(tài),類同于Input
注 mybatis-plus其他主鍵注解在高版本已經(jīng)廢棄
3. 屬性注解(@TableField)
作用:該屬性非主鍵屬性,解決屬性名與字段名不匹配問題、屬性是否是數(shù)據(jù)表中字段、insert、update生成策略等。如:
1
2
3
4
5
6
7
8
9
10
|
@TableName ( "thotset" ) public class HotsetEntity implements Serializable { private static final long serialVersionUID = 1L; private Integer fclass; private Integer fpwid; @TableField (vlaue= "fb_date" ) private Date fbdate; @TableField (exist= false ) private Date fedate; } |
第一個注解代表屬性fbDate對應(yīng)的數(shù)據(jù)庫字段名為fb_date
第二個注解代表fedate屬性不與表中的字段匹配,在新增、修改時,不用去匹配
常用的就這三個,其他注解不再詳細(xì)描述。
以上就是java開發(fā)MyBatis常用plus實體類注解符詳解的詳細(xì)內(nèi)容,更多關(guān)于MyBatis常用plus實體類注解的資料請關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://blog.csdn.net/guoyp2126/article/details/112966167