mybatis 添加成功并返回主键 /mybatis中useGeneratedKeys和keyProperty的作用


当主键是自增的情况下,添加一条记录的同时,其主键是不能使用的,但是有时我们需要该主键,这时我们该如何处理呢?这时我们只需要在其对应xml中加入以下属性即可:

useGeneratedKeys="true"  keyProperty="对应的主键的对象"。

<insert id="insertTest" parameterType="map" keyProperty="id" useGeneratedKeys="true"></insert>
Map map=new HashMap<>();
insertTest(map);
System.out.println(JSON.toJSONString(map));

返回:

{id:"**"}

猜你喜欢

转载自blog.csdn.net/lwang_IT/article/details/82253472