myibatis批量插入数据和通过循环批量插入数据对比
程序员文章站
2022-06-21 21:13:21
...
1. 配置文件:
<!-- 批量保存系统设置 -->
<insert id="saveBatch" param<?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.leimingtech.service.module.setting.dao.mapper.SettingMapper">eterType="java.util.List" >
insert into shop_setting(name,value)values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.name},#{item.value})
</foreach>
</insert>
<!-- 保存系统设置 -->
<insert id="save" parameterType="com.leimingtech.core.entity.base.Setting">
insert into shop_setting(name,value)values(#{name},#{value})
</insert>
</mapper>
@SqlMapper
public interface SettingMapper{
/**
* 批量保存系统设置
* @param setting
*/
void saveBatch(List<Setting> setting);
/**
* 保存系统设置
* @param setting
*/
void save(Setting setting);
}
package com.leimingtech.service.module.setting.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.leimingtech.core.entity.base.Setting;
public interface SettingDao {
/**
* 保存系统设置
* @param setting
*/
void save(Setting setting);
/**
* 保存系统设置
* @param setting
*/
void saveBatch(List<Setting> setting);
}
package com.leimingtech.service.module.setting.dao.impl;
import com.leimingtech.core.entity.base.Setting;
import com.leimingtech.service.module.setting.dao.SettingDao;
import com.leimingtech.service.module.setting.dao.mapper.SettingMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Service("settingDao")
public class SettingDaoImpl implements SettingDao {
@Resource
private SettingMapper settingMapper;
/**
* 保存系统设置
* @param setting
*/
@Override
public void save(Setting setting){
settingMapper.save(setting);
}
/**
* 保存系统设置
* @param setting
*/
@Override
public void saveBatch(List<Setting> setting){
settingMapper.saveBatch(setting);
}
}
测试类:
package sgnctest;
import com.leimingtech.core.entity.base.Setting;
import com.leimingtech.core.filter.XSSSecurityManager;
import com.leimingtech.service.module.frontorganization.service.FrontOrganizationService;
import com.leimingtech.service.module.ncorg.service.CreNcOrgService;
import com.leimingtech.service.module.projectinfo.service.CreProjectInfoService;
import com.leimingtech.service.module.setting.service.SettingService;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;
public class nctest {
private ClassPathXmlApplicationContext context = null;
@Before
public void before(){
context = new ClassPathXmlApplicationContext("context/applicationContext.xml");
// context.start();
}
/**
* 测试批量插入
*/
@Test
public void insertListBatch(){
System.out.println(context);
SettingService settingService = (SettingService)context.getBean("settingService");
// settingService.save("12345","45556");
List<Setting> list = new ArrayList<Setting>();
for (int i = 0; i <2000 ; i++) {
Setting setting = new Setting();
setting.setName("name"+i);
setting.setValue("value"+i);
list.add(setting);
}
long start = Calendar.getInstance().getTimeInMillis();
settingService.saveBatch(list);
long end = Calendar.getInstance().getTimeInMillis();
System.out.println("runtime:"+(end - start));
}
@Test
public void insertList(){
SettingService settingService = (SettingService)context.getBean("settingService");
List<Setting> list = new ArrayList<Setting>();
for (int i = 0; i <2000 ; i++) {
Setting setting = new Setting();
setting.setName("name"+i);
setting.setValue("value"+i);
list.add(setting);
}
long start = Calendar.getInstance().getTimeInMillis();
for(Setting setting:list){
settingService.save(setting.getName(),setting.getValue());
}
long end = Calendar.getInstance().getTimeInMillis();
System.out.println("runtime:"+(end - start));
}
}
结果比较:
测试结果:
批量插入2000条数据方法执行时间:455
循环调用插入2000条数据执行时间:102240
通过时间比较性能相差200多倍