Sub GenerateData()
Dim i As Long
For i = 1 To 200000
Cells(i, 1).Value = i '插入ID
Cells(i, 2).Value = "张三" & i '插入姓名,可以根据需要自定义命名规则
Cells(i, 3).Value = Int((50 - 20 + 1) * Rnd + 20) '插入随机年龄,示例为20-50之间的随机数
Next i
End Sub
在MyBatis中,ExecutorType是一种枚举类型,用于指定SQL语句执行的方式。其中,ExecutorType.BATCH和ExecutorType.SIMPLE是两种常见的执行方式。
ExecutorType.SIMPLE是MyBatis框架中的一种执行器类型,用于执行SQL语句并返回结果集。在这种执行器类型下,每个SQL语句的执行请求都将打开一个新的数据库连接,执行完毕后立即释放连接,适用于小型应用或轻负载的情况。
特点:
dao 层 的代码
@Insert("insert into excel(id,name,age) values (#{id},#{name},#{age})")
void insert(Man man);
public void insert1() {
//用于记录读取数据所需要的时间
long start0 = System.currentTimeMillis();
//ExcelUtil hutool工具类用来读取Excel文件
ExcelReader reader = ExcelUtil.getReader(FileUtil.file("C:\\Users\\26896\\Desktop\\test.xlsx"), "sheet1");
//将读取到的 reader 转化为 List集合
List<Man> mans = reader.readAll(Man.class);
//读取数据的结束时间同时也是写入数据库的开始时间
long start = System.currentTimeMillis();
//sqlSessionFactory是通过ioc容器注入的 设置其SqlSession的执行器格式ExecutorType.SIMPLE(默认)
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
ExcelDao mapper = sqlSession.getMapper(ExcelDao.class);
//循环将List中的数据插入数据库
for (Man man : mans) {
mapper.insert(man);
}
sqlSession.commit();
long end = System.currentTimeMillis();
sqlSession.close();
System.out.println("最终的结果为:" + (start - start0) );
System.out.println("最终的结果为:" + (end - start) );
}
rewriteBatchedStatements=true
当该参数设置为true时,MySQL会对批量操作进行重写,将多个SQL语句合并成一条批量执行的SQL语句。这样可以减少网络传输和数据库连接的开销,从而提高批量操作的效率。
public String insert2() {
long start0 = System.currentTimeMillis();
ExcelReader reader = ExcelUtil.getReader(FileUtil.file("C:\\Users\\26896\\Desktop\\test.xlsx"), "sheet1");
List<Man> mans = reader.readAll(Man.class);
long start = System.currentTimeMillis();
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ExcelDao mapper = sqlSession.getMapper(ExcelDao.class);
for (Man man : mans) {
mapper.insert(man);
}
sqlSession.commit();
long end = System.currentTimeMillis();
sqlSession.close();
System.out.println("读取最终的结果为:" + (start - start0) );
System.out.println("插入数据库最终的结果为:" + (end - start) );
return null;
}
ExecutorType.REUSE 本身不做验证,这里主要比较其它两个区别,但是都到这里啦就写上
public void insert3() {
long start0 = System.currentTimeMillis();
ExcelReader reader = ExcelUtil.getReader(FileUtil.file("C:\\Users\\26896\\Desktop\\test.xlsx"), "sheet1");
List<Man> mans = reader.readAll(Man.class);
long start = System.currentTimeMillis();
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.REUSE);
ExcelDao mapper = sqlSession.getMapper(ExcelDao.class);
for (Man man : mans) {
mapper.insert(man);
}
sqlSession.commit();
long end = System.currentTimeMillis();
sqlSession.close();
System.out.println("读取最终的结果为:" + (start - start0) );
System.out.println("插入数据库最终的结果为:" + (end - start) );
}
吃瓜观众: 歌歌 呀,既然ExecutorType.SIMPLE适合执行一个语句,那就用MyBatis 的,将20w条数据拼接为1条。这样是不是就快了
歌歌:非常好
xml代码
<insert id="add">
insert into excel(id,name,age) values
<foreach collection="list" item="man" separator=",">
(#{man.id},#{man.name},#{man.age})
</foreach>
</insert>
service代码
@Autowired
ExcelDao excelDao;
public String add2() {
long start0 = System.currentTimeMillis();
ExcelReader reader = ExcelUtil.getReader(FileUtil.file("C:\\Users\\26896\\Desktop\\test.xlsx"), "sheet1");
List<Man> mans = reader.readAll(Man.class);
long start = System.currentTimeMillis();
excelDao.add(mans);
long end = System.currentTimeMillis();
System.out.println("最终的结果为:" + (start - start0) );
System.out.println("最终的结果为:" + (end - start) );
return null;
}
在需要将大量数据通过java程序放入数据库时, 可以通过sqlSessionFactory.openSession(ExecutorType.BATCH)方法手动触发批量执行。并且可以通过在链接数据库的时候加上rewriteBatchedStatements=true来开始数据库的批处理操作
点个关注,不迷路
个人主页 个人主页