篮球
问题描述
想要通过Mybatis获得插入后的自增id,但总是返回1,代码如下:
long insertCommentInfo(CommentInfo commentInfo);
<insert id="insertCommentInfo" parameterType="CommentInfo" useGeneratedKeys="true" keyProperty="id">
insert into comment_info (content, ip
<if test="connect != null">
,connect
</if>
<if test="name != null">
,name
</if>
) values (#{content}, #{ip}
<if test="connect != null">
,#{connect}
</if>
<if test="name != null">
,#{name}
</if>
);
</insert>
但是如你所见,在调用方法时:
System.out.print(insertCommentInfo(commentInfo));
却总是返回1
问题原因
在Mybatis中,返回值代表的是插入的行数
解决办法
事实上,Mybatis把返回的id值已经封装在了传入的参数中,所以我们需要:
System.out.print(commentInfo.getId());