Mybatis?Plus插入數(shù)據(jù)后獲取新數(shù)據(jù)id值的踩坑記錄
問題描述:
Mybatis Plus的insert方法,按說插入數(shù)據(jù)后會(huì)自動(dòng)返回id
mapper方法:
@DS("wxuser") @Mapper public interface UserInfoMapper extends BaseMapper<UserInfo> { }
業(yè)務(wù)類:
@Autowired UserInfoMapper userInfoMapper; UserInfo user = new UserInfo(); user.setAddress(userInfo.getAddress()); user.setAge(userInfo.getAge()); user.setMobile(userInfo.getMobile()); user.setCreateTime(new Date()); user.setUpdateTime(new Date()); // 此num是指的插入數(shù)據(jù)成功的條數(shù) int num = userInfoMapper.insert(user); //如果想要獲取插入數(shù)據(jù)的id,需要:user.getUserId(); int id = user.getUserId();
這樣按說是可以的,但是我這里仍然獲取不到,所以需要再修改。
解決方法:
修改后mapper類:
@DS("wxuser") @Mapper public interface UserInfoMapper extends BaseMapper<UserInfo> { int saveUserInfo(@Param("userInfo")UserInfo userInfo); }
在resources目錄中新建mapper目錄,創(chuàng)建UserInfoMapper.xml文件:
注意三個(gè)值:
useGeneratedKeys=“true”
說明:允許 JDBC 支持自動(dòng)生成主鍵,需要數(shù)據(jù)庫驅(qū)動(dòng)支持。如果設(shè)置為 true,將強(qiáng)制使用自動(dòng)生成主鍵。盡管一些數(shù)據(jù)庫驅(qū)動(dòng)不支持此特性,但仍可正常工作(如 Derby)。
(僅適用于 insert 和 update)這會(huì)令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法來取出由數(shù)據(jù)庫內(nèi)部生成的主鍵(比如:像 MySQL 和 SQL Server 這樣的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)的自動(dòng)遞增字段),默認(rèn)值:false。
keyProperty=“userInfo.userId”
說明:keyProperty中對(duì)應(yīng)的值是實(shí)體類的屬性,而不是數(shù)據(jù)庫的字段
keyColumn=“userId”
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.wx.user.mapper.UserInfoMapper"> <insert id="saveUserInfo" useGeneratedKeys="true" keyProperty="userInfo.userId" keyColumn="userId"> insert into userInfo (userNick,userName,password,isdelete,address,mobile) values (#{userInfo.userNick},#{userInfo.userName},#{userInfo.password},#{userInfo.isDelete},#{userInfo.address},#{userInfo.mobile}) </insert> </mapper>
application.yml文件中:
# Mybatis Plus mybatis-plus: configuration: map-underscore-to-camel-case: false # 設(shè)置字段不使用下劃線方式 global-config: db-config: id-type: AUTO table-underline: false # 設(shè)置表名字不使用下劃線方式 mapper-locations: classpath*:/mapper/**/*.xml
修改后的業(yè)務(wù)類:
@Autowired UserInfoMapper userInfoMapper; UserInfo user = new UserInfo(); user.setAddress(userInfo.getAddress()); user.setAge(userInfo.getAge()); user.setMobile(userInfo.getMobile()); user.setCreateTime(new Date()); user.setUpdateTime(new Date()); // 此num是指的插入數(shù)據(jù)成功的條數(shù) int num = userInfoMapper.saveUserInfo(user); //如果想要獲取插入數(shù)據(jù)的id,需要:user.getUserId(); int id = user.getUserId();
這樣就可以獲取到新插入數(shù)據(jù)的id值了
總結(jié)
到此這篇關(guān)于Mybatis Plus插入數(shù)據(jù)后獲取新數(shù)據(jù)id值的文章就介紹到這了,更多相關(guān)Mybatis Plus獲取新數(shù)據(jù)id值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
通過Java?Reflection實(shí)現(xiàn)編譯時(shí)注解正確處理方法
Java注解是一種標(biāo)記在JDK5及以后的版本中引入,用于Java語言中向程序添加元數(shù)據(jù)的方法,這篇文章主要介紹了通過Java?Reflection實(shí)現(xiàn)編譯時(shí)注解處理方法,需要的朋友可以參考下2023-06-06java模擬實(shí)現(xiàn)銀行ATM機(jī)操作
這篇文章主要為大家詳細(xì)介紹了java模擬實(shí)現(xiàn)銀行ATM機(jī)操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Spring Security登錄添加驗(yàn)證碼的實(shí)現(xiàn)過程
這篇文章主要介紹了Spring Security登錄添加驗(yàn)證碼的實(shí)現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11關(guān)于idea中SpringBoot啟動(dòng)失敗的坑
這篇文章主要介紹了關(guān)于idea中SpringBoot啟動(dòng)失敗的坑,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Spring Boot系列教程之7步集成RabbitMQ的方法
RabbitMQ 即一個(gè)消息隊(duì)列,主要是用來實(shí)現(xiàn)應(yīng)用程序的異步和解耦,同時(shí)也能起到消息緩沖,消息分發(fā)的作用。下面這篇文章主要給大家介紹了關(guān)于Spring Boot之7步集成RabbitMQ的相關(guān)資料,需要的朋友可以參考下2018-11-11SpringBoot啟動(dòng)后啟動(dòng)內(nèi)嵌瀏覽器的方法
這篇文章主要介紹了SpringBoot啟動(dòng)后啟動(dòng)內(nèi)嵌瀏覽器的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12spring boot2結(jié)合mybatis增刪改查的實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于spring boot2結(jié)合mybatis增刪改查的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot2具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09java Iterator接口和LIstIterator接口分析
這篇文章主要介紹了java Iterator接口和LIstIterator接口分析的相關(guān)資料,需要的朋友可以參考下2017-05-05基于jQuery獲取table數(shù)據(jù)發(fā)送到后端
這篇文章主要介紹了基于jQuery獲取table數(shù)據(jù)發(fā)送到后端,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02