MyBatis 批量插入的 3 种方式!还有谁不会?
作者:楼主楼主
链接:www.jianshu.com/p/cce617be9f9e
反复执行单条插入语句 xml拼接sql 批处理执行
代码
<insert id="insertByBatch" parameterType="java.util.List">
INSERT INTO tb_item VALUES
<foreach collection="list" item="item" index="index" separator=",">
(newId(),#{item.uniqueCode},#{item.projectId},#{item.name},#{item.type},#{item.packageUnique},
#{item.isPackage},#{item.factoryId},#{item.projectName},#{item.spec},#{item.length},#{item.weight},
#{item.material},#{item.setupPosition},#{item.areaPosition},#{item.bottomHeight},#{item.topHeight},
#{item.serialNumber},#{item.createTime}</foreach>
</insert>
public interface ItemMapper extends Mapper<Item> {
int insertByBatch(List<Item> itemList);
}
@Service
public class ItemService {
@Autowired
private ItemMapper itemMapper;
@Autowired
private SqlSessionFactory sqlSessionFactory;
//批处理
@Transactional
public void add(List<Item> itemList) {
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
ItemMapper mapper = session.getMapper(ItemMapper.class);
for (int i = ; i < itemList.size(); i++) {
mapper.insertSelective(itemList.get(i));
if(i%1000==999){//每1000条提交一次防止内存溢出
session.commit();
session.clearCache();
}
}
session.commit();
session.clearCache();
}
//拼接sql
@Transactional
public void add1(List<Item> itemList) {
itemList.insertByBatch(itemMapper::insertSelective);
}
//循环插入
@Transactional
public void add2(List<Item> itemList) {
itemList.forEach(itemMapper::insertSelective);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = ApplicationBoot.class)
public class ItemServiceTest {
@Autowired
ItemService itemService;
private List<Item> itemList = new ArrayList<>();
//生成测试List
@Before
public void createList(){
String json ="{\n" +
" \"areaPosition\": \"TEST\",\n" +
" \"bottomHeight\": 5,\n" +
" \"factoryId\": \"0\",\n" +
" \"length\": 233.233,\n" +
" \"material\": \"Q345B\",\n" +
" \"name\": \"TEST\",\n" +
" \"package\": false,\n" +
" \"packageUnique\": \"45f8a0ba0bf048839df85f32ebe5bb81\",\n" +
" \"projectId\": \"094b5eb5e0384bb1aaa822880a428b6d\",\n" +
" \"projectName\": \"项目_TEST1\",\n" +
" \"serialNumber\": \"1/2\",\n" +
" \"setupPosition\": \"1B柱\",\n" +
" \"spec\": \"200X200X200\",\n" +
" \"topHeight\": 10,\n" +
" \"type\": \"Steel\",\n" +
" \"uniqueCode\": \"12344312\",\n" +
" \"weight\": 100\n" +
" }";
Item test1 = JSON.parseObject(json,Item.class);
test1.setCreateTime(new Date());
for (int i = ; i < 1000; i++) {//测试会修改此数量
itemList.add(test1);
}
}
//批处理
@Test
@Transactional
public void tesInsert() {
itemService.add(itemList);
}
//拼接字符串
@Test
@Transactional
public void testInsert1(){
itemService.add1(itemList);
}
//循环插入
@Test
@Transactional
public void testInsert2(){
itemService.add2(itemList);
}
}
测试结果:
com.microsoft.sqlserver.jdbc.SQLServerException: 传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确。此 RPC 请求中提供了过多的参数。多应为 2100
循环插入的时间复杂度是 O(n),并且常数C很大 拼接SQL插入的时间复杂度(应该)是 O(logn),但是成功完成次数不多,不确定 批处理的效率的时间复杂度是 O(logn),并且常数C也比较小
结论
@Transactional
public void add1(List<Item> itemList) {
itemList.forEach(itemMapper::insertSelective);
}
<END>
相关文章