MyBatis新增數(shù)據(jù)時自增id的兩種寫法小結(jié)
一、單個插入
- 接口方法:
public interface PlayerDao { int insertOnePlayer(Player player); int insertOnePlayer2(Player player); }
1.1 方式一
public void testInsertGenerateId1() throws IOException { // 2.獲取sqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); // 3.獲取對應(yīng)mapper PlayerDao mapper = sqlSession.getMapper(PlayerDao.class); // 4.執(zhí)行查詢語句并返回結(jié)果 Player player = new Player(); player.setPlayName("Allen Iverson"); player.setPlayNo(3); player.setTeam("76ers"); player.setHeight(1.83F); mapper.insertOnePlayer(player); sqlSession.commit(); System.out.println(player.getId()); }
- Mapper文件:
<insert id="insertOnePlayer" parameterType="Player" useGeneratedKeys="true" keyProperty="id"> insert into tb_player (id, playName, playNo,team, height) values ( #{id,jdbcType=INTEGER}, #{playName,jdbcType=VARCHAR}, #{playNo,jdbcType=INTEGER}, #{team,jdbcType=VARCHAR}, #{height,jdbcType=DECIMAL} ) </insert>
- 方式一配置:useGeneratedKeys=“true” keyProperty=“id” 即可
1.2 方式二
public void testInsertGenerateId2() throws IOException { // 2.獲取sqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); // 3.獲取對應(yīng)mapper PlayerDao mapper = sqlSession.getMapper(PlayerDao.class); // 4.執(zhí)行查詢語句并返回結(jié)果 Player player = new Player(); player.setPlayName("Tony Parker"); player.setPlayNo(9); player.setTeam("spurs"); player.setHeight(1.88F); mapper.insertOnePlayer2(player); sqlSession.commit(); System.out.println(player.getId()); } Mapper文件: <insert id="insertOnePlayer2" parameterType="Player"> <selectKey keyProperty="id" order="AFTER" resultType="int"> select LAST_INSERT_ID() </selectKey> insert into tb_player (id, playName, playNo,team, height) values ( #{id,jdbcType=INTEGER}, #{playName,jdbcType=VARCHAR}, #{playNo,jdbcType=INTEGER}, #{team,jdbcType=VARCHAR}, #{height,jdbcType=DECIMAL} ) </insert>
- 方式二通過 selectKey 標(biāo)簽完成 ,selectKey 更加靈活,支持一定程度的自定義
二、批量插入
- Java文件省略了,這里直接給出Mapper文件, Mapper 文件如下,其實就是:useGeneratedKeys=“true” keyProperty=“id”,其中id是JavaBean的主鍵id
<insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id"> INSERT INTO partition_info (id, node_ip_id, init_schema_info_id, prefix_table_index, partition_num, start_time, end_time, create_time, is_delete ) values <foreach collection="list" item="item" index="index" separator=","> (#{item.id,jdbcType=INTEGER}, #{item.nodeIpId,jdbcType=INTEGER}, #{item.initSchemaInfoId,jdbcType=INTEGER}, #{item.prefixTableIndex,jdbcType=VARCHAR}, #{item.partitionNum,jdbcType=VARCHAR}, #{item.startTime,jdbcType=TIMESTAMP}, #{item.endTime,jdbcType=TIMESTAMP}, #{item.createTime,jdbcType=TIMESTAMP}, #{item.isDelete,jdbcType=TINYINT} ) </foreach> </insert>
- Java代碼
System.out.println("before insert ..."); for (PartitionInfo p: list) { System.out.println(p); } PartitionInfoMapper mapper = sqlSession.getMapper(PartitionInfoMapper.class); int i = mapper.insertBatch(list); System.out.println("The rows be affected :" + i); System.out.println("after insert ..."); for (PartitionInfo p: list) { System.out.println(p); }
- 輸出
before insert ...
PartitionInfo(id=null, nodeIpId=1, initSchemaInfoId=1, prefixTableIndex=1, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
PartitionInfo(id=null, nodeIpId=2, initSchemaInfoId=2, prefixTableIndex=2, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
PartitionInfo(id=null, nodeIpId=3, initSchemaInfoId=3, prefixTableIndex=3, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
The rows be affected :3
after insert ...
PartitionInfo(id=701, nodeIpId=1, initSchemaInfoId=1, prefixTableIndex=1, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
PartitionInfo(id=702, nodeIpId=2, initSchemaInfoId=2, prefixTableIndex=2, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
PartitionInfo(id=703, nodeIpId=3, initSchemaInfoId=3, prefixTableIndex=3, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
- 這里其他的代碼都省略了,基本上就是: useGeneratedKeys=“true” keyProperty=“id” 這兩個標(biāo)簽起作用
- 另外我用的mybatis版本是 3.4.1
三、注意
- 注意Mapper文件中的 insert into tb_player (id, playName, playNo,team, height),這里不要多了一個逗號,之前height后面還有一個逗號導(dǎo)致一直空指針的錯誤。
到此這篇關(guān)于MyBatis新增數(shù)據(jù)時自增id的兩種寫法小結(jié)的文章就介紹到這了,更多相關(guān)MyBatis新增數(shù)據(jù)時自增id內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Cloud?Gateway?服務(wù)網(wǎng)關(guān)的部署與使用詳細講解
這篇文章主要介紹了Spring?Cloud?Gateway?服務(wù)網(wǎng)關(guān)的部署與使用詳細介紹,本文給大家講解的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04Java代碼性能測試實戰(zhàn)之ContiPerf安裝使用
這篇文章主要為大家介紹了Java代碼性能測試實戰(zhàn)之ContiPerf安裝使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06springboot實現(xiàn)定時器(一看即會,非常簡單)
這篇文章主要介紹了springboot實現(xiàn)定時器(一看即會,非常簡單),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12Intellij IDEA Debug調(diào)試技巧(小結(jié))
這篇文章主要介紹了Intellij IDEA Debug調(diào)試技巧(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10SpringBoot配置Redis連接池的實現(xiàn)步驟
本文主要介紹了SpringBoot配置Redis連接池的實現(xiàn)步驟,詳細的講解了連接池的作用、配置方式、連接池參數(shù)說明,具有一定的參考價值,感興趣的可以了解一下2025-03-03Java之Swagger配置掃描接口以及開關(guān)案例講解
這篇文章主要介紹了Java之Swagger配置掃描接口以及開關(guān)案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08使用IDEA創(chuàng)建maven父子工程項目 (圖文)
本文主要介紹了使用IDEA創(chuàng)建maven父子工程項目,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Spring Boot應(yīng)用監(jiān)控的實戰(zhàn)教程
Spring Boot 提供運行時的應(yīng)用監(jiān)控和管理功能,下面這篇文章主要給大家介紹了關(guān)于Spring Boot應(yīng)用監(jiān)控的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05