MySql 主键返回使用

方法:1.sql中加入:

<insert id="insertSelective" parameterType="domain.XwRole" useGeneratedKeys="true" keyProperty="id" keyColumn="id" >
    insert into t_xw_role
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="code != null" >
        code,
      </if>
      <if test="level != null" >
        level,
      </if>
      <if test="parent != null" >
        parent,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="code != null" >
        #{code,jdbcType=VARCHAR},
      </if>
      <if test="level != null" >
        #{level,jdbcType=INTEGER},
      </if>
      <if test="parent != null" >
        #{parent,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>

     2.在service 层接收返回的主键

        public int add(XwRoleVO xwRoleVo) {
        xwRoleVo.setCode(getCode(xwRoleVo));
        xwRoleM.insertSelective(xwRoleVo);
        return xwRoleVo.getId();
    }

方法2 通过注解方式实现

    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("INSERT INTO t_all_order (order_no, resident_id, total_amount, deduct_amount, "
            + "payable_amount, address_book_id, pay_status, deliver_status, "
            + "able_status,create_time,total_count,hang_msg,cancel_msg) "
            + "VALUES (#{orderNo}, #{residentId},  #{totalAmount}, #{deductAmount}, #{payableAmount}, "
            + "#{addressBookId}, #{payStatus}, #{deliverStatus}, #{ableStatus},"
            + "#{createTime},#{totalCount},#{hangMsg},#{cancelMsg});")
    public int insert(AllOrder allOrder);

在service 层接收返回的主键

service 层也是绑定到插入的实体类上,同第一种方法里面的获取一样

 

 

转载于:https://my.oschina.net/kuchawyz/blog/3008898

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值