Mybatis insert 返回主键

我们在使用mysql插入数据的时候,通常会有这样一个需求:
当表的主键id是自增字段的时候,会想要获得插入的id值用以下一个插入操作。
mybatis实际上已经为我们提供了这种功能:以下是我实现订单插入时获取id的实例
主要还是在mapper.xml文件中的insert标签中加入(useGeneratedKeys=”true” keyProperty=”ordertbId”)两个属性值,其中keyProperty的值为你所要取得的字段的对象的属性。
Mybatis insert 返回主键_第1张图片
但是注意传回的数据四存放在你之前传入的对象中的
Mybatis insert 返回主键_第2张图片
sql脚本

create table ordertb(#订单表
  ordertb_id int primary key auto_increment,#订单编号
  usertb_id int,#用户编号
  workertb_id   int,#工人编号
  ordertb_status    int,#订单状态1.未付款2.已付款3.已完成4.已冻结5.正在退单
  ordertb_payment   int,#支付方式1.线下交易2.支付宝3.微信
  ordertb_money double,#支付金额
  ordertb_starttime datetime,#订单开始时间
  ordertb_endtime   datetime,#订单完成时间
  alternative1  varchar(50),
  alternative2  varchar(50),
  alternative3  varchar(50)
);

OrderMapper.xml文件



<mapper namespace="com.craftsman.ssm.mapper.OrderMapper" >
  <resultMap id="BaseResultMap" type="com.craftsman.ssm.entity.Order" >
    <id column="ordertb_id" property="ordertbId" jdbcType="INTEGER" />
    <result column="usertb_id" property="usertbId" jdbcType="INTEGER" />
    <result column="workertb_id" property="workertbId" jdbcType="INTEGER" />
    <result column="ordertb_status" property="ordertbStatus" jdbcType="INTEGER" />
    <result column="ordertb_payment" property="ordertbPayment" jdbcType="INTEGER" />
    <result column="ordertb_money" property="ordertbMoney" jdbcType="DOUBLE" />
    <result column="ordertb_starttime" property="ordertbStarttime" jdbcType="TIMESTAMP" />
    <result column="ordertb_endtime" property="ordertbEndtime" jdbcType="TIMESTAMP" />
  resultMap>
  <insert id="insert" parameterType="com.craftsman.ssm.entity.Order"  useGeneratedKeys="true" keyProperty="ordertbId">
    insert into ordertb (ordertb_id, usertb_id, workertb_id, 
    ordertb_status, ordertb_payment, ordertb_money, 
    ordertb_starttime, ordertb_endtime)
    values (#{ordertbId,jdbcType=INTEGER}, #{usertbId,jdbcType=INTEGER}, #     {workertbId,jdbcType=INTEGER}, 
#{ordertbStatus,jdbcType=INTEGER}, #{ordertbPayment,jdbcType=INTEGER}, #{ordertbMoney,jdbcType=DOUBLE}, 
 now(), #{ordertbEndtime,jdbcType=TIMESTAMP})
  insert>
mapper>

订单实体类

package com.craftsman.ssm.entity;

import java.util.Date;

public class Order {
    private Integer ordertbId;

    private Integer usertbId;

    private Integer workertbId;

    private Integer ordertbStatus;

    private Integer ordertbPayment;

    private Double ordertbMoney;

    private Date ordertbStarttime;

    private Date ordertbEndtime;



    public Integer getOrdertbId() {
        return ordertbId;
    }

    public void setOrdertbId(Integer ordertbId) {
        this.ordertbId = ordertbId;
    }

    public Integer getUsertbId() {
        return usertbId;
    }

    public void setUsertbId(Integer usertbId) {
        this.usertbId = usertbId;
    }

    public Integer getWorkertbId() {
        return workertbId;
    }

    public void setWorkertbId(Integer workertbId) {
        this.workertbId = workertbId;
    }

    public Integer getOrdertbStatus() {
        return ordertbStatus;
    }

    public void setOrdertbStatus(Integer ordertbStatus) {
        this.ordertbStatus = ordertbStatus;
    }

    public Integer getOrdertbPayment() {
        return ordertbPayment;
    }

    public void setOrdertbPayment(Integer ordertbPayment) {
        this.ordertbPayment = ordertbPayment;
    }

    public Double getOrdertbMoney() {
        return ordertbMoney;
    }

    public void setOrdertbMoney(Double ordertbMoney) {
        this.ordertbMoney = ordertbMoney;
    }

    public Date getOrdertbStarttime() {
        return ordertbStarttime;
    }

    public void setOrdertbStarttime(Date ordertbStarttime) {
        this.ordertbStarttime = ordertbStarttime;
    }

    public Date getOrdertbEndtime() {
        return ordertbEndtime;
    }

    public void setOrdertbEndtime(Date ordertbEndtime) {
        this.ordertbEndtime = ordertbEndtime;
    }

    @Override
    public String toString() {
        return "Order [ordertbId=" + ordertbId + ", usertbId=" + usertbId + ", workertbId=" + workertbId
                + ", ordertbStatus=" + ordertbStatus + ", ordertbPayment=" + ordertbPayment + ", ordertbMoney="
                + ordertbMoney + ", ordertbStarttime=" + ordertbStarttime + ", ordertbEndtime=" + ordertbEndtime + "]";
    }


}
package com.craftsman.ssm.mapper;

import com.craftsman.ssm.entity.Order;

public interface OrderMapper {

    int insert(Order record);

}

测试类

package com.craftsman.ssm.mapper;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.craftsman.ssm.entity.Order;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/*.xml")
public class OrderMapperTest {
@Autowired
private OrderMapper OrderMapper;
    @Test
    public void testInsert() {
        Order record=new Order();
        record.setOrdertbMoney(200.0);
        record.setOrdertbPayment(1);
        record.setOrdertbStatus(1);
        OrderMapper.insert(record);
        System.out.println(record.getOrdertbId());
    }



}

你可能感兴趣的:(数据库,mysql,mybatis)