useGeneratedKeys和keyProperty成对与否使用的区别
Mybatis 中 Mapper.xml 内标签的useGeneratedKeys和keyProperty属性是否应该成对使用,当成对和不成对时,又有什么不同?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Mybatis 中 Mapper.xml 内标签的useGeneratedKeys和keyProperty属性是否应该成对使用,当成对和不成对时,又有什么不同?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
这个可以去看下mybatis的文档吧。
1:一起使用
如果你的数据库支持自动生成主键的字段(比如 MySQL 和 SQL Server),那么你可以设置 useGeneratedKeys=”true”,然后再把 keyProperty 设置到目标属性上就OK了。
2:只使用keyProperty,不使用useGeneratedKeys,就需要配合<selectKey >使用
在上面的示例中,selectKey 元素将会首先运行,Author 的 id 会被设置,然后插入语句会被调用。