1. Background
More and more bulk insert and modify batch used in the project scenario, bulk operations to achieve more and more plug-ins.
Personal learned roughly three ways:
Plugin name | Bulk Insert | Batch Edit |
mybatis | √ | √ |
General mapper | √ | × |
mybatis plus | √ | √ |
2. mybatils bulk insert and modify
2.1 pom.xml
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
About configuration is explained, however, to see history blog to see how mybatils configuration.
2.2 dao class
package com.cloudtech.web.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.cloudtech.web.entity.StationTiming;
import com.cloudtech.web.vo.StationTimingVo;
import tk.mybatis.mapper.common.Mapper;
public interface StationTimingMapper extends Mapper<StationTiming> {
/**
* 批量插入
*
* @param insertLsit
*/
void batchInsert(@Param("list") ArrayList<StationTiming> list);
/**
* 批量修改
*
* @param updateList
*/
void batchUpdate(@Param("list") List<StationTiming> list);
}
Causes extends Mapper <StationTiming> is used mapper general, but not universal mapper bulk edit operation, therefore, MyBatis bulk insert and use a modified manner.
2.2 mapper class
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cloudtech.web.dao.StationTimingMapper">
<!-- 批量插入站点校时信息 -->
<insert id="batchInsert" parameterType="java.util.List">
insert into station_timing
(station_id,send_time,status,create_time)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.stationId},
#{item.sendTime},
#{item.status},
now()
)
</foreach>
</insert>
<update id="batchUpdate" parameterType="java.util.List">
update station_timing
<trim prefix="set" suffixOverrides=",">
<trim prefix="send_time=case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.sendTime!=null">
when id=#{item.id} then #{item.sendTime}
</if>
</foreach>
</trim>
<trim prefix="status =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.status!=null">
when id=#{item.id} then #{item.status}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="item" index="index">
id=#{item.id}
</foreach>
</update>
</mapper>
Attributes | description |
---|---|
collection | That the iteration is the name of a collection, you can use annotations to specify @Param, remember mass participation dao class must increase @Param Mandatory |
item | Elements required output, such as for the List, was the object required |
index | Day iteration modify the index position, optional |
separator | Iteration requires additional content, optional |
open | Iteration begins with what sql |
close | Sql iteration to what end |
2.3 Entity Class
package com.cloudtech.web.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;
public class StationTiming implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Integer stationId;
@Transient
private String number;
private Date responseTime;
private String replyContent;
private Integer status;
private Date createTime;
private Date sendTime;
public Date getSendTime() {
return sendTime;
}
public void setSendTime(Date sendTime) {
this.sendTime = sendTime;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getStationId() {
return stationId;
}
public void setStationId(Integer stationId) {
this.stationId = stationId;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Date getResponseTime() {
return responseTime;
}
public void setResponseTime(Date responseTime) {
this.responseTime = responseTime;
}
public String getReplyContent() {
return replyContent;
}
public void setReplyContent(String replyContent) {
this.replyContent = replyContent;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
如果你热衷技术,喜欢交流,欢迎加入我们!