首页 > 其他 > 详细

Mybatis中insert中返回主键ID的方法

时间:2014-06-22 21:13:21      阅读:340      评论:0      收藏:0      [点我收藏+]

1、XyzMapper.xml


<insertid=“doSomething"parameterType="map"useGeneratedKeys="true"keyProperty=“yourId">

...

</insert>



<insert id=“doSomething" parameterType=“com.xx.yy.zz.YourClass" useGeneratedKeys="true" keyProperty=“yourId">

...

</insert>



2、XyzMapper.java


public int doSomething(Map<String, Object> parameters);


or


public int doSomething(YourClass c);


3、要在map或c中有一个字段名为yourId,Mybatis会自动把主键值赋给这个字段。


Map<String, Object> parameters = new HashMap<String, Object>();

parameters.put(“yourId”, 1234);

...

mapper.doSomething(parameters);

System.out.println(“id of the field that is primary key” + parameters.get(“yourId"));



YourClass c = new YourClass();

...

mapper.doSomething(c);

System.out.println(“id of the field that is primary key” + c.yourId);



Mybatis中insert中返回主键ID的方法,布布扣,bubuko.com

Mybatis中insert中返回主键ID的方法

原文:http://blog.csdn.net/prevention/article/details/32825081

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!