keyProperty="id" 和useGeneratedKeys="true"作用

在使用mybatis时,常常会出现这种需求:
当主键id是自增的情况下,添加一条记录的同时,其主键id是不能使用的,当我们取出主键id的值发现id为null,但是有时我们需要该主键,这时我们该如何处理呢?
这时我们只需要在其对应xxxmapper.xml中加入以下属性即可:
useGeneratedKeys=”true” keyProperty=”id”

 1 <insert id="insertSelective" parameterType="com.xxx.dataobject.UserDo" keyProperty="id" useGeneratedKeys="true">

2 ............

3 </insert> 

 主要是在主键是自增的情况下,添加成功后可以直接使用主键值,其中keyProperty的值是对象的属性值不是数据库表中的字段名

 

posted @ 2019-01-16 14:30  【曾】那时年少  阅读(12330)  评论(0编辑  收藏  举报