关于MyBatis在插入时获取自增id
1.1 注释方法
@Insert("insert into book(bid, bname, price, number, cratedate) values(null,#{bname},#{price},#{number},#{cratedate}) ")
int add(Book book);
//注意:在mapper中是不能重载的这里我特意写为add01
@Insert("insert into book set bname = #{name}, price =#{price}")
@Options(useGeneratedKeys = true,keyColumn = "bid",keyProperty = "id")
int add01(Map<String,Object> map);
主程序也就是测试程序可以查看结果
@Test
void add(){
Map<String,Object> map = new HashMap<>();
map.put("name","《钢铁是怎样连城的》");
map.put("price",60.6);
map.put("cratedate",LocalDateTime.now());
System.out.println(map);
bm.add01(map);
System.out.println(map);
}
结果
1.2 xml配置文件中书写
mapper
int add02(Map<String,Object> map);
对应的xml
<insert id="add02" useGeneratedKeys="true" keyColumn="bid" keyProperty="id">
insert into book set bname = #{name}, price =#{price};
</insert>
@Test
void add2xml(){
Map<String,Object> map = new HashMap<>();
map.put("name","《活着》");
map.put("price",60.6);
map.put("cratedate",LocalDateTime.now());
System.out.println(map);
bm.add02(map);
System.out.println(map);
}