Mybatis-plus插入后返回元素id的問題
mybatis-plus插入后返回插入元素的id
有三種方法,第三種最簡單。
不想麻煩的直接看第三種
1.mybatis原生
mybaits-plus要使用mybatis原生需要一下配置,指定下mapper文件的位置就好
mybatis-plus: ? mapper-locations: classpath*:mapperxml/*Mapper.xml
直接先看mapper.xml文件,這個(gè)insert語句實(shí)際上就是插入MouldMessage這個(gè)我定義的實(shí)體類。
<mapper namespace="com.hwz.MessageMouldMapper"> ? ? <insert id="testInsert" useGeneratedKeys="true" keyProperty="id"> ? ? ? ? INSERT INTO t_XXXX ? ? ? ? XXXXXX,XXXX,XXXXX ? ? ? ? VALUES XXXX,XXXX,XXXX ? ? </insert> </mapper>
userGenerateKeys告訴mybatis使用自增主鍵,keyProperty指定這個(gè)主鍵名稱叫id。
然后再mapper接口定義這個(gè)方法
Long testInsert(MessageMould messageMould);
調(diào)用這個(gè)插入語句,information這個(gè)實(shí)例時(shí)沒有定義id,創(chuàng)建時(shí)間這些字段的,輸出結(jié)果是數(shù)據(jù)表修改條數(shù),這里插入一條,所以返回1。
System.out.println(messageMouldMapper.testInsert(information)+“----”);
然后這時(shí)候我們輸出下information的一些屬性,發(fā)現(xiàn)本來作為參數(shù)的information被mybatis自動填充上了id和創(chuàng)建時(shí)間
System.out.println(information.getId()+“*******”+information.getCreateTime());
所以結(jié)論就是,插入之后找傳入的參數(shù),就能找到新增加元素的id
2.使用mybatis-plus注解
其實(shí)跟原生mybatis一樣,插入后元素的id會直接映射到參數(shù)中,只不過用注解代替了mapper.xml文件
@Insert(value = "INSERT INTO t_XXXX" + ? ? ? ? "XXX,XXX,XXX " + ? ? ? ? "VALUES (XXX,XXX,XXX)") @SelectKey(statement="select LAST_INSERT_ID()",keyProperty = "id",before = false,resultType = Long.class) Integer testInsert1(MessageMould messageMould);
執(zhí)行完這條insert操作后,直接拿形參messageMould的id,就能拿到id
3.使用mybatis-plus提供的insert
mybatis只要extends BaseMapper就可以調(diào)用他的insert方法。其實(shí)也就跟上面2個(gè)一樣。i調(diào)用insert(MessageMould messageMould)后,id會映射到形參messageMould中,直接拿形參messageMould的id,就能拿到id
Mybatis-plus設(shè)置id自增,插入數(shù)據(jù)
沒修改前
這是我的實(shí)體類。
@Data @AllArgsConstructor @NoArgsConstructor @ToString public class Company { ? private Integer id; ? private String cid; ? private String cname; ? private String address; ? private String representation; ? private String phone; ? private String email; ? private String weburl; ? private String introductory; }
我的數(shù)據(jù)庫設(shè)置的是id自增。 添加數(shù)據(jù)時(shí)沒有加上id的數(shù)據(jù)。
然后報(bào)錯(cuò)
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@59238b99]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3cc8aa87] was not registered for synchronization because synchronization is not active
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3cc8aa87]
2021-11-07 14:28:06.789 ERROR 5620 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'id' of 'class com.dk.pojo.Company' with value '1457233381002637313' Cause: java.lang.IllegalArgumentException: argument type mismatch] with root cause
查詢得知,當(dāng)時(shí)實(shí)體中,plus主鍵生成方式不設(shè)置生成方式時(shí),默認(rèn)的是自增。而且生成的值就如報(bào)錯(cuò)中的‘1457233381002637313’很長的一個(gè)值。
而主鍵的設(shè)置類型有:
AUTO(0, “數(shù)據(jù)庫ID自增”), INPUT(1, “用戶輸入ID”), ID_WORKER(2, “全局唯一ID”), UUID(3, “全局唯一ID”), NONE(4, “該類型為未設(shè)置主鍵類型”), ID_WORKER_STR(5, “字符串全局唯一ID”);
所以修改后
第一次:
@TableId( type = IdType.AUTO) private Integer id;
這樣的修改跟修改前一樣。我們需要的是12、13這樣的序列,所以需要設(shè)置id生成方式,就需要在注解設(shè)置value的值。
@TableId(value = “id”, type = IdType.AUTO) private Integer id;
這樣指定id的值,我們在用plus插件insert時(shí)就不用插入id的值。生成的id值跟數(shù)據(jù)庫對應(yīng)。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
零基礎(chǔ)搭建boot+MybatisPlus的詳細(xì)教程
這篇文章主要介紹了零基礎(chǔ)搭建boot+MybatisPlus,首先需要?jiǎng)?chuàng)建數(shù)據(jù)庫表和創(chuàng)建boot項(xiàng)目使用mybatisplus操作數(shù)據(jù)庫,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03詳解SpringBoot中的tomcat優(yōu)化和修改
這篇文章主要介紹了詳解SpringBoot中的tomcat優(yōu)化和修改,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Java使用POI實(shí)現(xiàn)excel文件的導(dǎo)入和導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了Java如何使用POI實(shí)現(xiàn)excel文件的導(dǎo)入和導(dǎo)出功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12詳解SpringMVC加載配置Properties文件的幾種方式
這篇文章主要介紹了詳解SpringMVC加載配置Properties文件的幾種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02Java中Socket實(shí)現(xiàn)數(shù)據(jù)通信的示例代碼
本文主要介紹了Java中Socket實(shí)現(xiàn)數(shù)據(jù)通信的示例代碼,Socket可以建立起客戶端和服務(wù)器之間的連接,實(shí)現(xiàn)數(shù)據(jù)的傳輸和交互,感興趣的可以了解一下2023-09-09