Mybatis-Plus?新增獲取自增列id方式
新增獲取自增列id
1、實(shí)體類定義
注意:@TableId(value = “id”, type = IdType.AUTO)注解中的 type = IdType.AUTO 屬性標(biāo)注主鍵為自增策略。
import lombok.Data; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableField; @Data @TableName("users") public class User { @TableId(value = "id", type = IdType.AUTO) private Integer id; @TableField("`name`") private String name; }
2、解決辦法
方法一:
使用框架自帶的insert方法。
int insert(T entity);
方法二:
@Insert("insert into users(`name`) values(#{user.name})") @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") Integer add(@Param("user") User user);
方法三:
@InsertProvider(type = UserMapperProvider.class, method = "add") @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") Integer add(@Param("user") User user);
UserMapperProvider類
public class UserMapperProvider { ? ? public String add(User user) { ? ? ? ? return "insert into users(id, `name`) values(#{user.id},#{user.name})"; ? ? } }
3、調(diào)用方法獲取id說明
方法調(diào)用前:
方法調(diào)用后:
解決id自增方法
在pojo文件中id加入
@TableId(value = “id”,type = IdType.AUTO)
application.yml中加入:
global-config: ?? ? db-config: ? ? ? ?? ??? ?id-type: auto
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Java實(shí)現(xiàn)qq郵箱發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了使用Java實(shí)現(xiàn)qq郵箱發(fā)送郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2010-05-05Java使用elasticsearch基礎(chǔ)API使用案例講解
這篇文章主要介紹了Java使用elasticsearch基礎(chǔ)API使用案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08詳解Spring Boot下使用logback 記錄多個(gè)文件日志
這篇文章主要介紹了詳解Spring Boot下使用logback 記錄多個(gè)文件日志,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08RabbitMQ實(shí)現(xiàn)消息可靠性傳遞過程講解
消息的可靠性傳遞是指保證消息百分百發(fā)送到消息隊(duì)列中去,這篇文章主要介紹了RabbitMQ實(shí)現(xiàn)消息可靠性傳遞過程,感興趣想要詳細(xì)了解可以參考下文2023-05-05關(guān)于Arrays.sort()使用的注意事項(xiàng)
這篇文章主要介紹了關(guān)于Arrays.sort()使用的注意事項(xiàng),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05利用Java實(shí)現(xiàn)在PDF中添加工具提示
這篇文章主要介紹了如何通過Java在PDF中添加工具提示,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定的參考價(jià)值,感興趣的可以學(xué)習(xí)一下2022-01-01java實(shí)現(xiàn)適用于安卓的文件下載線程類
本文給大家分享的是java實(shí)現(xiàn)適用于安卓的文件下載線程類的代碼,有需要的小伙伴可以參考下2015-07-07Java struts2 validate用戶登錄校驗(yàn)功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java struts2 validate用戶登錄校驗(yàn)功能實(shí)現(xiàn)的具體步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05