`
lucky_god
  • 浏览: 36288 次
社区版块
存档分类
最新评论

mybatis封装添加实体时的ID

    博客分类:
  • java
阅读更多
环境:JDK7、Mybatis3.2.8、Mysql5.6.25
目的:用Mybatis插入数据成功后,对象中封装主键
代码:
<insert id="save">
		<selectKey resultType="int" keyProperty="id">
	      SELECT LAST_INSERT_ID()
      	</selectKey>
		insert into menurow
			(nameC,nameE,priceOne,priceTwo)
		values
			(#{nameC},#{nameE},#{priceOne},#{priceTwo})
</insert>


如果数据库ID的类型与JAVA实体类型不一致,可用下面方法(数据库:int、JAVA:String)
<insert id="save">
		<selectKey resultType="String" keyProperty="id">
	      SELECT concat('',LAST_INSERT_ID())
      	</selectKey>
		insert into menurow
			(nameC,nameE,priceOne,priceTwo)
		values
			(#{nameC},#{nameE},#{priceOne},#{priceTwo})
	</insert>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics