ssm 项目,往数据库添加一条数据的同时返回该条数据的id

前端为满足用户的良好体验,在插入一条数据后,根据返回的状态码,直接在页面追加一条数据,由于可能在后面操作时使用到该条数据的的id,需要再返回状态码的同时返回追加数据在数据库表中的id。

如下红色部分:

1在mapper中修改:

<insert id="insertSelective" parameterType="com.wangyun.vo.Yearcontrol">
    insert into yearcontrol
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="year != null">
        year,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="year != null">
        #{year,jdbcType=VARCHAR},
      </if>
    </trim>
    <selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
         SELECT LAST_INSERT_ID() AS id
    </selectKey>
  </insert>

2在controller中修改

int j=yearcontrolService.addYearcontrol(yearcontrol);
data.put("id", yearcontrol.getId());

修改以上两处即可得到添加数据的id


参考:https://blog.csdn.net/java_xuetu/article/details/53984613


猜你喜欢

转载自blog.csdn.net/qq_37236241/article/details/80075145