mysql insert 返回自增长主键问题

xiaoxiao2021-02-28  105

<insert id="insert" parameterType="SePushInfo" useGeneratedKeys="true" keyProperty="id">         insert into t_se_push         (nettype,aid,exceptdownflag,tags,approvalflag,remark,type,`status`,ctime,utime,cuser,uuser,enabledtime)         values         (#{nettype},#{aid},#{exceptdownflag},#{tags},#{approvalflag},#{remark},#{type},#{status},#{ctime},#{utime},#{cuser},#{uuser},#{ctime})     </insert> 其中id为long类型 mapper: public Long insert(SePushInfo sePushInfo); 错误写法: Long i ; i = sePushMapper.insert(sePushInfo);    这种写法只会返回影响行数

正确写法:

sePushMapper.insert(sePushInfo);   

i = sePushInfo.getId(); return i; 这样才是返回的自增的id。 取回来的主键直接赋值给当做参数的实体的id ,useGeneratedKeys:使用jdbc的getGenereatedKeys方法获取主键并赋值到keyProperty设置的属性中
转载请注明原文地址: https://www.6miu.com/read-21842.html

最新回复(0)