0


MyBatis流式查询

MyBatis流式查询

1.应用场景说明

  • MyBatis preview: JDBC三种读取方式: 1.一次全部(默认):一次获取全部。 2.流式:多次获取,一次一行。 3.游标:多次获取,一次多行。

在开发中我们经常需要会遇到统计数据,将数据导出到excel表格中。由于生成报表逻辑要从数据库读取大量数据并在内存中加工处理后再生成Excel返回给客户端。如果数据量过大,采用默认的读取方式(一次性获取全部)会导致内存飙升,甚至是内存溢出。而导出数据又需要查询大量的数据,因此采用流式查询就比较合适了。

2.模拟excel导出场景

1.创建海量数据的sql脚本

  1. CREATETABLE dept(/*部门表*/
  2. deptno MEDIUMINTUNSIGNEDNOTNULLDEFAULT0,
  3. dname VARCHAR(20)NOTNULLDEFAULT"",
  4. loc VARCHAR(13)NOTNULLDEFAULT"");#创建表EMP雇员CREATETABLE emp
  5. (empno MEDIUMINTUNSIGNEDNOTNULLDEFAULT0,/*编号*/
  6. ename VARCHAR(20)NOTNULLDEFAULT"",/*名字*/
  7. job VARCHAR(9)NOTNULLDEFAULT"",/*工作*/
  8. mgr MEDIUMINTUNSIGNEDNOTNULLDEFAULT0,/*上级编号*/
  9. hiredate DATENOTNULL,/*入职时间*/
  10. sal DECIMAL(7,2)NOTNULL,/*薪水*/
  11. comm DECIMAL(7,2)NOTNULL,/*红利*/
  12. deptno MEDIUMINTUNSIGNEDNOTNULLDEFAULT0/*部门编号*/);#工资级别表CREATETABLE salgrade
  13. (
  14. grade MEDIUMINTUNSIGNEDNOTNULLDEFAULT0,
  15. losal DECIMAL(17,2)NOTNULL,
  16. hisal DECIMAL(17,2)NOTNULL);#测试数据INSERTINTO salgrade VALUES(1,700,1200);INSERTINTO salgrade VALUES(2,1201,1400);INSERTINTO salgrade VALUES(3,1401,2000);INSERTINTO salgrade VALUES(4,2001,3000);INSERTINTO salgrade VALUES(5,3001,9999);delimiter $$
  17. #创建一个函数,名字 rand_string,可以随机返回我指定的个数字符串createfunction rand_string(n INT)returnsvarchar(255)#该函数会返回一个字符串begin#定义了一个变量 chars_str, 类型 varchar(100)#默认给 chars_str 初始值 'abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ'declare chars_str varchar(100)default'abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ';declare return_str varchar(255)default'';declare i intdefault0;while i < n do# concat 函数 : 连接函数mysql函数set return_str =concat(return_str,substring(chars_str,floor(1+rand()*52),1));set i = i +1;endwhile;return return_str;end $$
  18. #这里我们又自定了一个函数,返回一个随机的部门号createfunction rand_num()returnsint(5)begindeclare i intdefault0;set i = floor(10+rand()*500);return i;end $$
  19. #创建一个存储过程, 可以添加雇员createprocedure insert_emp(instartint(10),in max_num int(10))begindeclare i intdefault0;#set autocommit =0 把autocommit设置成0#autocommit = 0 含义: 不要自动提交set autocommit =0;#默认不提交sql语句repeatset i = i +1;#通过前面写的函数随机产生字符串和部门编号,然后加入到emp表insertinto emp values((start+i),rand_string(6),'SALESMAN',0001,curdate(),2000,400,rand_num());
  20. until i = max_num
  21. endrepeat;#commit整体提交所有sql语句,提高效率commit;end $$
  22. #添加8000000数据call insert_emp(100001,8000000)$$
  23. #命令结束符,再重新设置为;delimiter;

2.MyBatis流式查询

1.创建src\main\java\com\llp\llpmybatis\entity\Emp.java

  1. @DatapublicclassEmp{privateInteger empno;privateString ename;privateString job;privateInteger mgr;privateDate hiredate;privateBigDecimal sal;privateBigDecimal comm;privateInteger deptno;}

2.创建src\main\java\com\llp\llpmybatis\vo\EmpVo.java

  1. @DatapublicclassEmpVo{@ExcelProperty("员工编号")privateInteger empno;@ExcelProperty("员工姓名")privateString ename;@ExcelProperty("员工工种")privateString job;@ExcelProperty("主管编号")privateInteger mgr;@ExcelProperty("入职日期")privateDate hiredate;@ExcelProperty("工资")privateBigDecimal sal;@ExcelProperty("通讯")privateBigDecimal comm;@ExcelProperty("部门编号")privateInteger deptno;}

3.创建src\main\java\com\llp\llpmybatis\controller\EmpController.java

  1. @RestControllerpublicclassEmpController{@AutowiredprivateEmpService empService;/**
  2. * 导出员工数据到excel
  3. */@RequestMapping("/export")publicvoidexportEmp(){StopWatch watch =newStopWatch();
  4. watch.start();List<EmpVo> empList = empService.exportEmp();//将数据分sheet进行导出EasyExcleUtil.excelExportDivisionBySheet(EmpVo.class,"员工信息_"+System.currentTimeMillis(), empList);
  5. watch.stop();long totalTimeMillis = watch.getTotalTimeMillis();System.out.println("共计耗时:"+totalTimeMillis+"毫秒");}/**
  6. * 导入excel数据到员工表
  7. * @param file
  8. */@RequestMapping("/import")publicvoidimportEmp(@RequestParam(name ="file")MultipartFile file){//这里我们在导入时传入回调接口的匿名内部类实现,在ExcleDataListener easyExcel读取监听器中对接口进行赋值//在监听器中doAfterAllAnalysed,在所有数据解析完之后回调用这个方法,我们在方法中对导出的数据集进行赋值EasyExcleUtil.importExcel(file,EmpVo.class,newExcleFinshCallBack(){@OverridepublicvoiddoAfterAllAnalysed(List<Object> result){
  9. empService.exportEmp();}});}}

4.创建src\main\java\com\llp\llpmybatis\service\EmpService.java

  1. publicinterfaceEmpService{List<EmpVo>exportEmp();}

5.创建src\main\java\com\llp\llpmybatis\service\impl\EmpServiceImpl.java(重点)

  1. @ServicepublicclassEmpServiceImplimplementsEmpService{@ResourceprivateEmpDao empdao;/**
  2. * mybatis流式查询导出员工数据
  3. * @return
  4. */@OverridepublicList<EmpVo>exportEmp(){StopWatch stopWatch =newStopWatch();
  5. stopWatch.start();List<EmpVo> empList =newArrayList<>();
  6. empdao.getAll(newResultHandler<EmpVo>(){/**
  7. * mybatis流失查询会回调处理逻辑
  8. * @param resultContext
  9. */@OverridepublicvoidhandleResult(ResultContext<?extendsEmpVo> resultContext){
  10. empList.add(resultContext.getResultObject());}});
  11. stopWatch.stop();System.out.println("查询共计耗费"+stopWatch.getTotalTimeMillis()+"毫秒");return empList;}}

6.创建src\main\java\com\llp\llpmybatis\dao\EmpDao.java(重点)

  1. @RepositorypublicinterfaceEmpDao{voidgetAll(ResultHandler<EmpVo> handler);}

这里dao层没有返回值,但是在还是需要指定resultMap,因为查询的数据要映射到回调函数的resultContext中,此外还需要设置:resultSetType=“FORWARD_ONLY” 、fetchSize=“-2147483648”

EmpDao.xml

  1. <mappernamespace="com.llp.llpmybatis.dao.EmpDao"><resultMapid="empResultMap"type="com.llp.llpmybatis.vo.EmpVo"><resultcolumn="empno"property="empno"/><resultcolumn="ename"property="ename"/><resultcolumn="job"property="job"/><resultcolumn="mgr"property="mgr"/><resultcolumn="hiredate"property="hiredate"/><resultcolumn="sal"property="sal"/><resultcolumn="comm"property="comm"/><resultcolumn="deptno"property="deptno"/></resultMap><selectid="getAll"resultMap="empResultMap"resultSetType="FORWARD_ONLY"fetchSize="-2147483648">
  2. select *
  3. from emp;
  4. </select></mapper>

至此mybatis流式查询就完成了

3.Excel通用导出工具类

1.Excel导入导出工具类

  1. publicclassEasyExcleUtil{privatestaticfinalint MAXROWS =500000;/**
  2. * excel读取
  3. *
  4. * @param file excel文件
  5. * @param head 列名
  6. * @param callBack 回调接口的实现类
  7. */publicstaticvoidimportExcel(MultipartFile file,Class head,ExcleFinshCallBack callBack){try{EasyExcel.read(file.getInputStream(), head,newExcleDataListener(callBack)).sheet().doRead();}catch(IOException e){
  8. e.printStackTrace();}}/**
  9. * 导出数据
  10. *
  11. * @param head 类名
  12. * @param excelname excel名字
  13. * @param data 数据
  14. * java.lang.IllegalArgumentException: Invalid row number (1048576) outside allowa
  15. * 这是由于Excel的一张sheet允许的最大行数是1048575,由于导出的数据比较大,超出了一张sheet所能容纳的最大行数,导致无法继续创建新的行
  16. * 1048575
  17. * 1000000
  18. */publicstaticvoidexcelExport(Class head,String excelname,List data){ServletRequestAttributes requestAttributes =(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();HttpServletResponse response = requestAttributes.getResponse();// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman//response.setContentType("application/vnd.ms-excel");
  19. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  20. response.setCharacterEncoding("utf-8");try{// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系String fileName =URLEncoder.encode(excelname,"UTF-8").replaceAll("\\+","%20");
  21. response.setHeader("Content-disposition","attachment;filename*=utf-8''"+ fileName +".xlsx");EasyExcel.write(response.getOutputStream(), head).sheet("Sheet1").doWrite(data);}catch(UnsupportedEncodingException e){
  22. e.printStackTrace();}catch(IOException e){
  23. e.printStackTrace();}}/**
  24. * 获取默认表头内容的样式
  25. *
  26. * @return
  27. */privatestaticHorizontalCellStyleStrategygetDefaultHorizontalCellStyleStrategy(){/** 表头样式 **/WriteCellStyle headWriteCellStyle =newWriteCellStyle();// 背景色(浅灰色)// 可以参考:https://www.cnblogs.com/vofill/p/11230387.html
  28. headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());// 字体大小WriteFont headWriteFont =newWriteFont();
  29. headWriteFont.setFontHeightInPoints((short)10);
  30. headWriteCellStyle.setWriteFont(headWriteFont);//设置表头居中对齐
  31. headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);/** 内容样式 **/WriteCellStyle contentWriteCellStyle =newWriteCellStyle();// 内容字体样式(名称、大小)WriteFont contentWriteFont =newWriteFont();
  32. contentWriteFont.setFontName("宋体");
  33. contentWriteFont.setFontHeightInPoints((short)10);
  34. contentWriteCellStyle.setWriteFont(contentWriteFont);//设置内容垂直居中对齐
  35. contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//设置内容水平居中对齐
  36. contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//设置边框样式
  37. contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
  38. contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
  39. contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
  40. contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);// 头样式与内容样式合并returnnewHorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);}/**
  41. * 将数据分sheet进行导出
  42. * @param data 查询结果
  43. * @param fileName 导出文件名称
  44. * @param clazz 映射实体class
  45. * @param <T> 查询结果类型
  46. * @throws Exception
  47. */publicstatic<T>voidexcelExportDivisionBySheet(Class clazz,String fileName,List<T> data){OutputStream out =null;ExcelWriter excelWriter =null;try{ServletRequestAttributes requestAttributes =(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();HttpServletResponse response = requestAttributes.getResponse();// 分割的集合List<List<T>> lists =SplitList.splitList(data, MAXROWS);
  48. out =getOutputStream(fileName, response);ExcelWriterBuilder excelWriterBuilder =EasyExcel.write(out, clazz).excelType(ExcelTypeEnum.XLSX).registerWriteHandler(getDefaultHorizontalCellStyleStrategy());
  49. excelWriter = excelWriterBuilder.build();ExcelWriterSheetBuilder excelWriterSheetBuilder;WriteSheet writeSheet;for(int i =1; i <= lists.size(); i++){
  50. excelWriterSheetBuilder =newExcelWriterSheetBuilder();
  51. excelWriterSheetBuilder.sheetNo(i);
  52. excelWriterSheetBuilder.sheetName("sheet"+ i);
  53. writeSheet = excelWriterSheetBuilder.build();
  54. excelWriter.write(lists.get(i -1), writeSheet);}}catch(IOException e){
  55. e.printStackTrace();}finally{if(excelWriter !=null){
  56. excelWriter.finish();}if(out !=null){try{
  57. out.close();}catch(IOException e){
  58. e.printStackTrace();}}}}privatestaticOutputStreamgetOutputStream(String fileName,HttpServletResponse response)throwsIOException{
  59. fileName =URLEncoder.encode(fileName,"UTF-8").replaceAll("\\+","%20");// response.setContentType("application/vnd.ms-excel"); // .xls
  60. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");// .xlsx
  61. response.setCharacterEncoding("utf-8");
  62. response.setHeader("Content-disposition","attachment;filename*=utf-8''"+ fileName +".xlsx");return response.getOutputStream();}}

2.Excel数据读取监听器

  1. /**
  2. * excel读取监听器
  3. */publicclassExcleDataListenerextendsAnalysisEventListener{//定义一个保存Excel所有记录的集合privateList<Object> list =newLinkedList<>();//回调接口privateExcleFinshCallBack callBack;/**
  4. * 构造注入ExcleFinshCallBack
  5. * @param callBack
  6. */publicExcleDataListener(ExcleFinshCallBack callBack){this.callBack = callBack;}/**
  7. * 这个每一条数据解析都会来调用
  8. * 我们将每一条数据都保存到list集合中
  9. * @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()}
  10. * @param context
  11. */@Overridepublicvoidinvoke(Object data,AnalysisContext context){
  12. list.add(data);}/**
  13. * 所有数据解析完成了 都会来调用这个方法
  14. * 在
  15. * @param context
  16. */@OverridepublicvoiddoAfterAllAnalysed(AnalysisContext context){this.callBack.doAfterAllAnalysed(this.list);}}

4.Excel读取数据完成回调接口

  1. /**
  2. * excel读取数据完成回调接口
  3. */publicinterfaceExcleFinshCallBack{voiddoAfterAllAnalysed(List<Object> result);}

5.拆分List集合工具类

  1. /**
  2. * 拆分List集合
  3. */publicclassSplitListUtil{/**
  4. *
  5. * @param list 待切割集合
  6. * @param len 集合按照多大size来切割
  7. * @param <T>
  8. * @return
  9. */publicstatic<T>List<List<T>>splitList(List<T> list,int len){if(list ==null|| list.size()==0|| len <1){returnnull;}List<List<T>> result =newArrayList<List<T>>();int size = list.size();int count =(size + len -1)/ len;for(int i =0; i < count; i++){List<T> subList = list.subList(i * len,((i +1)* len > size ? size : len *(i +1)));
  10. result.add(subList);}return result;}/**
  11. * @param source 源集合
  12. * @param n 分成n个集合
  13. * @param <T> 集合类型
  14. * @return
  15. * @description 集合平均分组
  16. */publicstatic<T>List<List<T>>groupList(List<T> source,int n){if(source ==null|| source.size()==0|| n <1){returnnull;}if(source.size()< n){returnArrays.asList(source);}List<List<T>> result =newArrayList<List<T>>();int number = source.size()/ n;int remaider = source.size()% n;// 偏移量,每有一个余数分配,就要往右偏移一位int offset =0;for(int i =0; i < n; i++){List<T> list1 =null;if(remaider >0){
  17. list1 = source.subList(i * number + offset,(i +1)* number + offset +1);
  18. remaider--;
  19. offset++;}else{
  20. list1 = source.subList(i * number + offset,(i +1)* number + offset);}
  21. result.add(list1);}return result;}}

4.测试结果

sheet1

image-20220815173002686

sheet2

image-20220815173019131

sheet3

image-20220815173053637

5.遗留问题,待处理

这个问题时由于

  1. excelWriter.finish();

去关闭连接时,发现连接已经被终止了导致的,对数据导出的完整性并没有影响

image-20220815173144668

标签: mybatis java 数据库

本文转载自: https://blog.csdn.net/qq_44981526/article/details/126355254
版权归原作者 llp1110 所有, 如有侵权,请联系我们删除。

“MyBatis流式查询”的评论:

还没有评论