最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis-plus插入后返回元素id的問題

 更新時(shí)間:2023年03月21日 16:16:44   作者:普通網(wǎng)友  
這篇文章主要介紹了Mybatis-plus插入后返回元素id的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)文章

最新評論

合阳县| 亚东县| 平邑县| 柳河县| 潞城市| 黑河市| 安乡县| 仪陇县| 措勤县| 历史| 普兰店市| 朝阳区| 定远县| 烟台市| 江阴市| 奉新县| 金湖县| 池州市| 扎兰屯市| 柳林县| 南宫市| 泽州县| 综艺| 东安县| 厦门市| 柳林县| 梁平县| 仪征市| 林芝县| 饶河县| 延吉市| 蓬安县| 大安市| 新丰县| 于田县| 福建省| 惠水县| 扎赉特旗| 光山县| 台中县| 广东省|