Mybatis新增/更新并返回结果

43 0 0 0

场景:

子表新增一条数据,主表某字段要+1,要获取该字段的值(例:新增一条文章评论,文章表的评论数字段要+1,并返回最新的评论数)

dao层

必须传对象,返回的结果在字段中

1
int updateCommentCount(Article info);

xml

1
2
3
4
5
6
<update id="updateCommentCount"> <selectKey resultType="SHORT" keyProperty="commentCount" order="AFTER"> select (select A.COMMENT_COUNT FROM ARTICLE A WHERE A.ID = #{id}) commentCount from DUAL </selectKey> update ARTICLE A set A.COMMENT_COUNT = A.COMMENT_COUNT + 1 where A.ID = #{id} </update>
目录