Mybatis中insert、Update操作返回主键

来源:互联网 发布:数控车床螺纹g92编程 编辑:程序博客网 时间:2024/05/17 17:46

Mybatis中insert、Update操作返回主键

 

How to get pk after operation of insert, update in mybatis?

 

两种方式

1. <insert id="insert" parameterType="User" keyProperty="id"   useGeneratedKeys="true">

 

java代码    调用insert(User u)后 u.getPK()就有值了

 

2.   不推荐,经测试,在SPRING事务环境中,永远返回1,因为在insert后,事务并没有提交所以。。。坑啊!

<insert id="insert" parameterType="com.wsmall.kalemao.dal.auth.dataobject.UserDo" >  <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >    SELECT LAST_INSERT_ID()  </selectKey>  insert into User ...</insert >

java代码    pk = insert(User u)


0 0