Article Directory
- Two ways to write auto-increment id when adding new data in MyBatis
- 1. Single insertion
- 1.1 Method 1
- 1.2 Method 2
- 2. Batch insertion
- 3. Pay attention
Two ways to write auto-increment id when adding new data in MyBatis
1. Single insertion
public interface PlayerDao {
int insertOnePlayer(Player player);
int insertOnePlayer2(Player player);
}
1.1 Method 1
public void testInsertGenerateId1() throws IOException {
SqlSession sqlSession = sqlSessionFactory.openSession();
PlayerDao mapper = sqlSession.getMapper(PlayerDao.class);
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());
}
<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>
- Method 1 configuration: useGeneratedKeys="true" keyProperty="id"
1.2 Method 2
public void testInsertGenerateId2() throws IOException {
SqlSession sqlSession = sqlSessionFactory.openSession();
PlayerDao mapper = sqlSession.getMapper(PlayerDao.class);
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 file:<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>
- Method 2 is completed through the selectKey tag. SelectKey is more flexible and supports a certain degree of customization.
2. Batch insertion
- The Java file is omitted, and the Mapper file is given here directly. The Mapper file is as follows, which is actually: useGeneratedKeys="true" keyProperty="id", where id is the primary key id of JavaBean
<insert id="insertBatch" parameterType="" 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=",">
(#{,jdbcType=INTEGER}, #{,jdbcType=INTEGER}, #{,jdbcType=INTEGER},
#{,jdbcType=VARCHAR}, #{,jdbcType=VARCHAR}, #{,jdbcType=TIMESTAMP},
#{,jdbcType=TIMESTAMP}, #{,jdbcType=TIMESTAMP}, #{,jdbcType=TINYINT}
)
</foreach>
</insert>
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)
- All other codes here are omitted, basically: useGeneratedKeys="true" keyProperty="id" These two tags work
- In addition, the version of mybatis I use is 3.4.1
3. Pay attention
- Note the insert into tb_player (id, playName, playNo, team, height) in the Mapper file. Don't add a comma here. There is a comma after the height before, causing the error of the pointer to be null.