码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis中useGeneratedKeys和keyProperty的作用

时间:2018-07-12 16:28:32      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:键值   generated   --   bat   creat   情况下   tis   value   insert   

使用场景:将对象插入id为自增的表中,同时在程序中仍需要用到该对象插入后自增的ID

方法:配置 useGeneratedKeys="true"  keyProperty="对应的主键的对象"

<!--   主要是在主键是自增的情况下,添加成功后可以直接使用主键值,其中keyProperty的值是对象的属性值不是数据库表中的字段名-->
 <insert id="insert" parameterType="user" useGeneratedKeys="true" keyProperty="id">
     insert into User(name,sex,year,create_time) values (#{name},#{sex},#{year},#{create_time})
</insert>

这样在之后的java代码中我们就可以获取该主键对应的对象的属性值(id)

mybatis中useGeneratedKeys和keyProperty的作用

标签:键值   generated   --   bat   creat   情况下   tis   value   insert   

原文地址:https://www.cnblogs.com/JoeyWong/p/9299282.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!