在项目中经常会碰到批量操作的情况,当使用MyBatis批量写入的时候,如果超过一定的条数,会存在严重的性能问题,下面给出一个工具类,希望对大家有所帮助。
1.创建测试类
public class User implements Serializable{
private static final long serialVersionUID = -7929625699473608909L;
private String userName;
private Integer age;
public User(String userName, Integer age) {
this.userName = userName;
this.age = age;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [ userName:"+userName+"\t age:"+age+" ]";
}
2. 创建utils类
import com.google.common.collect.Iterables;
import org.boon.Boon;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.LinkedList;
/**
* Created with IntelliJ IDEA
*/
public class DBBatchUtils<E> implements Iterator<List<E>> {
private int batchSize;
private List<E> origin;
private int index=0;
private List<E> result;
private int size=0;
public DBBatchUtils(List<E> origin,int batchSize) {
if (0 >= batchSize) {
throw new RuntimeException();
}
this.batchSize = batchSize;
this.origin = origin;
this.size =null==origin?0:origin.size();
result = new ArrayList<E>(batchSize);
}
@Override
public boolean hasNext() {
return index<size;
}
@Override
public List<E> next() {
result.clear();
for (int i = 0; i < batchSize && index < size; i++) {
result.add(origin.get(index++));
}
return result;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
/**
* 批量插入ByGuava
* @param origin 需要批量操作的数据信任List
* @param batchSize 分批数
* @param <T>
* @return
*/
public static <T> List<List<T>> batchInsert(List<T> origin,int batchSize){
Iterable<List<T>> subSets = Iterables.partition(origin,batchSize);
Iterator<List<T>> partitions = subSets.iterator();
List<List<T>> list = new LinkedList<>();
while (partitions.hasNext()){
List<T> sub = partitions.next();
list.add(sub);
}
return list;
}
在项目是这样来使用:
//批量插入用户数据.
DBBatchUtils batchUtils = new DBBatchUtils(list,2000);//list是需要批量操作的数据集合
while (batchUtils.hasNext()){
List<User> userList = batchUtils.next();
//数据库插入操作语句;
}
guava的方式直接遍历即可。