Mybatis批量插入方式比较

效率:普通for循环 < BATCH方式获取mapper < foreach标签拼接sql

主函数代码:

package com.william;

import com.william.mapper.Student;
import com.william.mapper.StudentExample;
import com.william.mapper.StudentMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Data
public class Main {

    static public void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        System.out.println(inputStream.toString());
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession(true);
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        long start, end;
        start = System.currentTimeMillis();
        Student student=null;
        int k=4000;
        for (int i=0; i<500; i++){
            student = new Student();
            student.setId(k+i);
            student.setName("Test Name");
            student.setAge(99);
            student.setPassword("1231245");
            student.setSalary(50000);
            student.setAddress("Peking");
            mapper.insert(student);
        }
        k+=500;
        System.out.printf("普通for循环耗时: %d\n", System.currentTimeMillis()-start);
        session = sqlSessionFactory.openSession(ExecutorType.BATCH, true);
        mapper = session.getMapper(StudentMapper.class);

        start = System.currentTimeMillis();
        for (int i=0; i<500; i++){
            student = new Student();
            student.setName("Test Name");
            student.setAge(99);
            student.setPassword("1231245");
            student.setSalary(50000);
            student.setAddress("Peking");
            student.setId(k+i);
            mapper.insert(student);

        }
        k+=500;
        System.out.printf("Batch for循环耗时: %d\n", System.currentTimeMillis()-start);
        List<Student> list = new ArrayList<Student>(500);
        start = System.currentTimeMillis();
        for (int i=0; i<500; i++){
            student = new Student();
            student.setName("Test Name");
            student.setAge(99);
            student.setPassword("1231245");
            student.setSalary(50000);
            student.setAddress("Peking");
            list.add(i, student);
        }
        mapper.insertAllStudents(list);
        System.out.printf("Each标签 for循环耗时: %d\n", System.currentTimeMillis()-start);
    }
}

mapper.xml文件

<insert id="insertAllStudents" parameterType="Student">
        insert into students values
        <foreach collection="list" separator="," item="item">
          (item.id, item.password, item.age, item.salary, item.address, item.name)
        </foreach>
  </insert>

结果为:

普通for循环耗时: 1340
Batch for循环耗时: 30
Each标签 for循环耗时: 10

可见,forEach标签的效率是最高的

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值