mybatis插入insert操作,返回自增id

通过map插入

转自:https://www.cnblogs.com/eternityz/p/12284732.html

controller

@GetMapping("insertOrder")
public String insertOrder(String params) {
    RegCheck.isNull(params);
    String[] arr = params.split("@");
    //订单号@备注备注备注备注备注备注6
    if (arr.length != 7) {
        throw new ServiceException(ResultEnum.LOSTPARAMS);
    }
    Map<String, Object> map = Maps.newHashMap();
    map.put("ticket", arr[0]);
    map.put("remark1", arr[1]);
    map.put("remark2", arr[2]);
    map.put("remark3", arr[3]);
    map.put("remark4", arr[4]);
    map.put("remark5", arr[5]);
    map.put("remark6", arr[6]);
    String id = orderService.insertOrder(map);
    return id;
}

serviceImpl

@Override
public String insertOrder(Map<String, Object> map) {
    int num = orderMapper.insertOrder(map);
    if (num != 1) {
        throw new ServiceException(1000, "插入数据失败!");
    }
    return map.get("ticket").toString();
}

通过插入操作,插入返回的自增id写入到ticket这个key得value中,为什么写在ticket这个key中,是xml文件中配置的

service

String insertOrder(Map<String, Object> map);

mapper

int insertOrder(Map<String, Object> map);

此时返回的int值,插入成功返回1

xml

<insert id="insertOrder" useGeneratedKeys="true" keyProperty="ticket">
    insert into order_order_record
    (id,ticket,remark1,remark2,remark3,remark4,remark5,remark6,create_time)
    values
    (null,#{ticket},#{remark1},#{remark2},#{remark3},#{remark4},#{remark5},#{remark6},now())
</insert>

此时自增主键返回到map中ticket这个key中

Mybatis 配置文件 useGeneratedKeys 参数只针对 insert 语句生效,默认为 false。当设置为 true 时,表示如果插入的表以自增列为主键,则允许 JDBC 支持自动生成主键,并可将自动生成的主键返回。

keyColumn:数据库表中的自增字段名。

keyProperty:数据库自增字段在pojo中的对应属性或者对应的map中的key

通过实体类插入

controller

@GetMapping("insertOrders")
public int insertOrders(String params) {
    RegCheck.isNull(params);
    String[] arr = params.split("@");
    //订单号@备注备注备注备注备注备注6
    if (arr.length != 7) {
        throw new ServiceException(ResultEnum.LOSTPARAMS);
    }
    Order order = new Order();
    order.setTicket(arr[0]);
    order.setRemark1(arr[1]);
    order.setRemark2(arr[2]);
    order.setRemark3(arr[3]);
    order.setRemark4(arr[4]);
    order.setRemark5(arr[5]);
    order.setRemark6(arr[6]);
    int id = orderService.insertOrders(order);
    return id;
}

Order

@Data
public class Order {
    public int id;
    public String ticket;
    public String remark1;
    public String remark2;
    public String remark3;
    public String remark4;
    public String remark5;
    public String remark6;
    public Date createTime;
}

serviceImpl

@Override
public int insertOrders(Order order) {
    int num = orderMapper.insertOrders(order);
    if (num != 1) {
        throw new ServiceException(1002, "插入数据失败!");
    }
    return order.getId();
}

service

int insertOrders(Order order);

mapper

int insertOrders(Order order);

xml

<insert id="insertOrders" useGeneratedKeys="true" keyProperty="id">
    insert into order_order_record
    (id,ticket,remark1,remark2,remark3,remark4,remark5,remark6,create_time)
    values
    (null,#{ticket},#{remark1},#{remark2},#{remark3},#{remark4},#{remark5},#{remark6},now())
</insert>

此时自增主键返回到Order的id字段中,serviceImpl通过获取id可以获得最新插入的id

selectKey

<insert id="insert">
 <selectKey keyProperty="id" resultType="int" order="BEFORE">
  <if test="_databaseId == ‘oracle‘">
   select seq_users.nextval from dual
  </if>
  <if test="_databaseId == ‘db2‘">
   select nextval for seq_users from sysibm.sysdummy1"
  </if>
 </selectKey>
 insert into users values (#{id}, #{name})
</insert>

order属性可以控制

selectKey在插入操作前或者操作后获取key值,做为字段插入或返回字段。(此段代码获取的序列值id作为字段值插入到users表中)

<selectKey keyProperty="id" resultType="int" order="AFTER">
        select LAST_INSERT_ID()
</selectKey>

如果数据库支持自增长主键字段(比如mysql、sql server)设置useGeneratedKeys=”true”和keyProperty,这样就可以插入主键id值
oracle则不支持自增长id,设置useGeneratedKey=”false”,如果设置true则会有报错信息。通过nextval函数,如SEQ_table.Nextval生成id

插入更新一条数据时,可以使用selectKey获取id操作。当做多条数据插入更新时,而selectKey只能使用一次,此时应该使用useGeneratedKeys操作。

站在巨人的肩膀上摘苹果:

原文链接:https://blog.csdn.net/qq_33488642/article/details/82189742

相关推荐