Mybatis-plus插入后返回元素id的問題
mybatis-plus插入后返回插入元素的id
有三種方法,第三種最簡單。
不想麻煩的直接看第三種
1.mybatis原生
mybaits-plus要使用mybatis原生需要一下配置,指定下mapper文件的位置就好
mybatis-plus: ? mapper-locations: classpath*:mapperxml/*Mapper.xml
直接先看mapper.xml文件,這個insert語句實際上就是插入MouldMessage這個我定義的實體類。
<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指定這個主鍵名稱叫id。
然后再mapper接口定義這個方法
Long testInsert(MessageMould messageMould);
調用這個插入語句,information這個實例時沒有定義id,創(chuàng)建時間這些字段的,輸出結果是數據表修改條數,這里插入一條,所以返回1。
System.out.println(messageMouldMapper.testInsert(information)+“----”);
然后這時候我們輸出下information的一些屬性,發(fā)現本來作為參數的information被mybatis自動填充上了id和創(chuàng)建時間
System.out.println(information.getId()+“*******”+information.getCreateTime());
所以結論就是,插入之后找傳入的參數,就能找到新增加元素的id
2.使用mybatis-plus注解
其實跟原生mybatis一樣,插入后元素的id會直接映射到參數中,只不過用注解代替了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就可以調用他的insert方法。其實也就跟上面2個一樣。i調用insert(MessageMould messageMould)后,id會映射到形參messageMould中,直接拿形參messageMould的id,就能拿到id
Mybatis-plus設置id自增,插入數據
沒修改前
這是我的實體類。
@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; }
我的數據庫設置的是id自增。 添加數據時沒有加上id的數據。
然后報錯
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
查詢得知,當時實體中,plus主鍵生成方式不設置生成方式時,默認的是自增。而且生成的值就如報錯中的‘1457233381002637313’很長的一個值。
而主鍵的設置類型有:
AUTO(0, “數據庫ID自增”), INPUT(1, “用戶輸入ID”), ID_WORKER(2, “全局唯一ID”), UUID(3, “全局唯一ID”), NONE(4, “該類型為未設置主鍵類型”), ID_WORKER_STR(5, “字符串全局唯一ID”);
所以修改后
第一次:
@TableId( type = IdType.AUTO) private Integer id;
這樣的修改跟修改前一樣。我們需要的是12、13這樣的序列,所以需要設置id生成方式,就需要在注解設置value的值。
@TableId(value = “id”, type = IdType.AUTO) private Integer id;
這樣指定id的值,我們在用plus插件insert時就不用插入id的值。生成的id值跟數據庫對應。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
詳解SpringBoot中的tomcat優(yōu)化和修改
這篇文章主要介紹了詳解SpringBoot中的tomcat優(yōu)化和修改,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09詳解SpringMVC加載配置Properties文件的幾種方式
這篇文章主要介紹了詳解SpringMVC加載配置Properties文件的幾種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02