MyBatis新增數(shù)據(jù)時(shí)自增id的兩種寫(xiě)法小結(jié)
一、單個(gè)插入
- 接口方法:
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.獲取對(duì)應(yīng)mapper
PlayerDao mapper = sqlSession.getMapper(PlayerDao.class);
// 4.執(zhí)行查詢語(yǔ)句并返回結(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.獲取對(duì)應(yīng)mapper
PlayerDao mapper = sqlSession.getMapper(PlayerDao.class);
// 4.執(zhí)行查詢語(yǔ)句并返回結(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>
- 方式二通過(guò) selectKey 標(biāo)簽完成 ,selectKey 更加靈活,支持一定程度的自定義
二、批量插入
- Java文件省略了,這里直接給出Mapper文件, Mapper 文件如下,其實(shí)就是: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” 這兩個(gè)標(biāo)簽起作用
- 另外我用的mybatis版本是 3.4.1
三、注意
- 注意Mapper文件中的 insert into tb_player (id, playName, playNo,team, height),這里不要多了一個(gè)逗號(hào),之前height后面還有一個(gè)逗號(hào)導(dǎo)致一直空指針的錯(cuò)誤。
到此這篇關(guān)于MyBatis新增數(shù)據(jù)時(shí)自增id的兩種寫(xiě)法小結(jié)的文章就介紹到這了,更多相關(guān)MyBatis新增數(shù)據(jù)時(shí)自增id內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Cloud?Gateway?服務(wù)網(wǎng)關(guān)的部署與使用詳細(xì)講解
這篇文章主要介紹了Spring?Cloud?Gateway?服務(wù)網(wǎng)關(guān)的部署與使用詳細(xì)介紹,本文給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
Java代碼性能測(cè)試實(shí)戰(zhàn)之ContiPerf安裝使用
這篇文章主要為大家介紹了Java代碼性能測(cè)試實(shí)戰(zhàn)之ContiPerf安裝使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
基于Java Gradle復(fù)制項(xiàng)目模塊過(guò)程圖解
這篇文章主要介紹了基于Java Gradle復(fù)制項(xiàng)目模塊過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
springboot實(shí)現(xiàn)定時(shí)器(一看即會(huì),非常簡(jiǎn)單)
這篇文章主要介紹了springboot實(shí)現(xiàn)定時(shí)器(一看即會(huì),非常簡(jiǎn)單),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Intellij IDEA Debug調(diào)試技巧(小結(jié))
這篇文章主要介紹了Intellij IDEA Debug調(diào)試技巧(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
SpringBoot配置Redis連接池的實(shí)現(xiàn)步驟
本文主要介紹了SpringBoot配置Redis連接池的實(shí)現(xiàn)步驟,詳細(xì)的講解了連接池的作用、配置方式、連接池參數(shù)說(shuō)明,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03
Java之Swagger配置掃描接口以及開(kāi)關(guān)案例講解
這篇文章主要介紹了Java之Swagger配置掃描接口以及開(kāi)關(guān)案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
使用IDEA創(chuàng)建maven父子工程項(xiàng)目 (圖文)
本文主要介紹了使用IDEA創(chuàng)建maven父子工程項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Spring Boot應(yīng)用監(jiān)控的實(shí)戰(zhàn)教程
Spring Boot 提供運(yùn)行時(shí)的應(yīng)用監(jiān)控和管理功能,下面這篇文章主要給大家介紹了關(guān)于Spring Boot應(yīng)用監(jiān)控的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05

