马府强
2 years ago
29 changed files with 1068 additions and 0 deletions
@ -0,0 +1,143 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> |
|||
<component name="FacetManager"> |
|||
<facet type="Spring" name="Spring"> |
|||
<configuration /> |
|||
</facet> |
|||
<facet type="web" name="Web"> |
|||
<configuration> |
|||
<webroots /> |
|||
<sourceRoots> |
|||
<root url="file://$MODULE_DIR$/src/main/java" /> |
|||
<root url="file://$MODULE_DIR$/src/main/resources" /> |
|||
</sourceRoots> |
|||
</configuration> |
|||
</facet> |
|||
</component> |
|||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> |
|||
<output url="file://$MODULE_DIR$/target/classes" /> |
|||
<output-test url="file://$MODULE_DIR$/target/test-classes" /> |
|||
<content url="file://$MODULE_DIR$"> |
|||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> |
|||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> |
|||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> |
|||
<excludeFolder url="file://$MODULE_DIR$/target" /> |
|||
</content> |
|||
<orderEntry type="inheritedJdk" /> |
|||
<orderEntry type="sourceFolder" forTests="false" /> |
|||
<orderEntry type="library" name="Maven: cn.afterturn:easypoi-base:4.1.0" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.apache.poi:poi:4.1.0" level="project" /> |
|||
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.13" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.apache.commons:commons-collections4:4.3" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.apache.commons:commons-math3:3.6.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.apache.poi:poi-ooxml:4.1.0" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.apache.commons:commons-compress:1.18" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.github.virtuald:curvesapi:1.06" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.apache.poi:poi-ooxml-schemas:4.1.0" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.apache.xmlbeans:xmlbeans:3.1.0" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.google.guava:guava:16.0.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.9" level="project" /> |
|||
<orderEntry type="library" name="Maven: ognl:ognl:3.2.6" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.javassist:javassist:3.20.0-GA" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.30" level="project" /> |
|||
<orderEntry type="library" name="Maven: javax.validation:validation-api:2.0.1.Final" level="project" /> |
|||
<orderEntry type="library" name="Maven: cn.afterturn:easypoi-web:4.1.0" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.2.5.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.2.5.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.2.5.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.2.5.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.2.5.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.2.5.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.2.5.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.2.5.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: cn.afterturn:easypoi-annotation:4.1.0" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.2.6.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.2.6.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.2.6.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.2.6.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" /> |
|||
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.12.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.12.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.30" level="project" /> |
|||
<orderEntry type="library" name="Maven: jakarta.annotation:jakarta.annotation-api:1.3.5" level="project" /> |
|||
<orderEntry type="library" scope="RUNTIME" name="Maven: org.yaml:snakeyaml:1.25" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.2.6.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.10.3" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.10.3" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.10.3" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.10.3" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.3" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.10.3" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.2.6.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.33" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:9.0.33" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.33" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-validation:2.2.6.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: jakarta.validation:jakarta.validation-api:2.0.2" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.hibernate.validator:hibernate-validator:6.0.18.Final" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.4.1.Final" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.5.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.24" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-boot-starter:3.4.3.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.baomidou:mybatis-plus:3.4.3.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-extension:3.4.3.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-core:3.4.3.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-annotation:3.4.3.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.mybatis:mybatis:3.5.7" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:2.0.6" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.2.6.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.19" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.2.6.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.4.2" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.2.5.RELEASE" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.2.5.RELEASE" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:2.2.6.RELEASE" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:2.2.6.RELEASE" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test-autoconfigure:2.2.6.RELEASE" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: com.jayway.jsonpath:json-path:2.4.0" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:json-smart:2.3" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:accessors-smart:1.2" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm:5.0.4" level="project" /> |
|||
<orderEntry type="library" name="Maven: jakarta.xml.bind:jakarta.xml.bind-api:2.3.3" level="project" /> |
|||
<orderEntry type="library" name="Maven: jakarta.activation:jakarta.activation-api:1.2.2" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.5.2" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.5.2" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.5.2" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.5.2" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.5.2" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.vintage:junit-vintage-engine:5.5.2" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.5.2" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-junit-jupiter:3.1.0" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.assertj:assertj-core:3.13.2" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest:2.1" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:3.1.0" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy:1.10.8" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.10.8" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.6" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.skyscreamer:jsonassert:1.5.0" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: com.vaadin.external.google:android-json:0.0.20131108.vaadin1" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.2.5.RELEASE" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-core:2.6.4" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13.1" level="project" /> |
|||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:2.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.github.pagehelper:pagehelper-spring-boot-starter:1.2.12" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:2.0.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.github.pagehelper:pagehelper-spring-boot-autoconfigure:1.2.12" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.github.pagehelper:pagehelper:5.1.10" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.github.jsqlparser:jsqlparser:2.0" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.alibaba:easyexcel:3.1.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.alibaba:easyexcel-core:3.1.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.alibaba:easyexcel-support:3.1.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.apache.commons:commons-csv:1.8" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.ehcache:ehcache:3.8.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.glassfish.jaxb:jaxb-runtime:2.3.2" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.glassfish.jaxb:txw2:2.3.2" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.sun.istack:istack-commons-runtime:3.0.8" level="project" /> |
|||
<orderEntry type="library" name="Maven: org.jvnet.staxex:stax-ex:1.8.1" level="project" /> |
|||
<orderEntry type="library" name="Maven: com.sun.xml.fastinfoset:FastInfoset:1.2.16" level="project" /> |
|||
</component> |
|||
</module> |
@ -0,0 +1,46 @@ |
|||
# 多线程实现百万级数据导出到excel |
|||
|
|||
## **考虑前提:** |
|||
|
|||
大数据量导出到文件,首先需要考虑的是内存溢出的场景:数据库读取数据到内存中、将数据写入到excel进行大量的IO操作。然后考虑到一个文件数据过大,用户打开慢,体验不好。针对这些问题的考虑,采用多线程的方式多个线程同时处理查询数据,一个线程生成一个excel,最后在合并数据返回,以达到提高效率的目的。 |
|||
|
|||
## **实现思路:** |
|||
1.使用easyExecl结合批量线程池执行,结合线程分批处理数据缩短查询数据库时间和导出数据时间,查询数据速度比较理想 |
|||
测试接口:http://localhost:8080/exportEasyExcel?pageNum=0&pageSize=100000&limit=4000 |
|||
2.使用easyExecl结合循环线程池执行,结合线程分批处理数据缩短查询数据库时间和导出数据时间,查询数据速度不理想 |
|||
测试接口:http://localhost:8080/exportEasyExcelV1?pageNum=0&pageSize=60000&limit=2000 |
|||
3.使用SXSSFWorkbook结合批量线程池执行,结合线程分批处理数据缩短查询数据库时间和导出数据时间,效果最优最理想 |
|||
http://localhost:8080/exportEasyExcelV4?pageNum=0&pageSize=30000&limit=2000 |
|||
|
|||
模拟sql可以结合测试类本地造数据: |
|||
CREATE TABLE `user` ( |
|||
`id` bigint(20) NOT NULL AUTO_INCREMENT, |
|||
`name` varchar(255) DEFAULT NULL, |
|||
`phone` varchar(255) DEFAULT NULL, |
|||
`ceate_by` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL, |
|||
`remark` varchar(255) DEFAULT NULL, |
|||
`birthday` datetime DEFAULT NULL, |
|||
`test1` varchar(255) DEFAULT NULL, |
|||
`test2` varchar(255) DEFAULT NULL, |
|||
`test3` varchar(255) DEFAULT NULL, |
|||
`test4` varchar(255) DEFAULT NULL, |
|||
`test5` varchar(255) DEFAULT NULL, |
|||
`test6` varchar(255) DEFAULT NULL, |
|||
`test7` varchar(255) DEFAULT NULL, |
|||
`test8` varchar(255) DEFAULT NULL, |
|||
`test9` varchar(255) DEFAULT NULL, |
|||
`test10` varchar(255) DEFAULT NULL, |
|||
`test11` varchar(255) DEFAULT NULL, |
|||
`test12` varchar(255) DEFAULT NULL, |
|||
`test13` varchar(255) DEFAULT NULL, |
|||
`test14` varchar(255) DEFAULT NULL, |
|||
`test15` varchar(255) DEFAULT NULL, |
|||
`test16` varchar(255) DEFAULT NULL, |
|||
`test17` varchar(255) DEFAULT NULL, |
|||
`test18` varchar(255) DEFAULT NULL, |
|||
`test19` varchar(255) DEFAULT NULL, |
|||
`test20` varchar(255) DEFAULT NULL, |
|||
`test21` varchar(255) DEFAULT NULL, |
|||
PRIMARY KEY (`id`) |
|||
) ENGINE=InnoDB AUTO_INCREMENT=600001 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; |
|||
|
@ -0,0 +1,100 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|||
<modelVersion>4.0.0</modelVersion> |
|||
|
|||
<parent> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-parent</artifactId> |
|||
<version>2.2.6.RELEASE</version> |
|||
<relativePath/> <!-- lookup parent from repository --> |
|||
</parent> |
|||
|
|||
<groupId>com.wm.file</groupId> |
|||
<artifactId>ExportExcelData</artifactId> |
|||
<version>1.0-SNAPSHOT</version> |
|||
|
|||
<dependencies> |
|||
<!--Excel读取、写入、导出工具--> |
|||
<dependency> |
|||
<groupId>cn.afterturn</groupId> |
|||
<artifactId>easypoi-base</artifactId> |
|||
<version>4.1.0</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>cn.afterturn</groupId> |
|||
<artifactId>easypoi-web</artifactId> |
|||
<version>4.1.0</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>cn.afterturn</groupId> |
|||
<artifactId>easypoi-annotation</artifactId> |
|||
<version>4.1.0</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-web</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.projectlombok</groupId> |
|||
<artifactId>lombok</artifactId> |
|||
<version>1.18.24</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>com.baomidou</groupId> |
|||
<artifactId>mybatis-plus-boot-starter</artifactId> |
|||
<version>3.4.3.1</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>mysql</groupId> |
|||
<artifactId>mysql-connector-java</artifactId> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-jdbc</artifactId> |
|||
</dependency> |
|||
<!--测试--> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-test</artifactId> |
|||
<scope>test</scope> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>junit</groupId> |
|||
<artifactId>junit</artifactId> |
|||
<version>4.13.1</version> |
|||
<scope>test</scope> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>com.github.pagehelper</groupId> |
|||
<artifactId>pagehelper-spring-boot-starter</artifactId> |
|||
<version>1.2.12</version> |
|||
<exclusions> |
|||
<exclusion> |
|||
<artifactId>mybatis-spring</artifactId> |
|||
<groupId>org.mybatis</groupId> |
|||
</exclusion> |
|||
<exclusion> |
|||
<artifactId>mybatis</artifactId> |
|||
<groupId>org.mybatis</groupId> |
|||
</exclusion> |
|||
</exclusions> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>com.alibaba</groupId> |
|||
<artifactId>easyexcel</artifactId> |
|||
<version>3.1.1</version> |
|||
</dependency> |
|||
</dependencies> |
|||
|
|||
<build> |
|||
<plugins> |
|||
<plugin> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-maven-plugin</artifactId> |
|||
</plugin> |
|||
</plugins> |
|||
</build> |
|||
</project> |
@ -0,0 +1,25 @@ |
|||
package com.wm.file; |
|||
|
|||
import org.mybatis.spring.annotation.MapperScan; |
|||
import org.springframework.boot.SpringApplication; |
|||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
|||
import org.springframework.cache.annotation.EnableCaching; |
|||
import org.springframework.scheduling.annotation.EnableAsync; |
|||
import org.springframework.scheduling.annotation.EnableScheduling; |
|||
|
|||
/** |
|||
* @ClassName: ExportExcelDataApplication |
|||
* @Description: 启动类 |
|||
* @Author: xiaomage |
|||
* @Date: 2022/5/3 19:01 |
|||
**/ |
|||
@EnableCaching |
|||
@EnableScheduling |
|||
@SpringBootApplication |
|||
@EnableAsync |
|||
@MapperScan("com.wm.file.mapper")//扫描mapper接口所在的包
|
|||
public class ExportExcelDataApplication { |
|||
public static void main(String[] args) { |
|||
SpringApplication.run(ExportExcelDataApplication.class, args); |
|||
} |
|||
} |
@ -0,0 +1,27 @@ |
|||
package com.wm.file.config; |
|||
|
|||
import org.apache.poi.ss.formula.functions.Na; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.scheduling.annotation.EnableAsync; |
|||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
|||
|
|||
import java.util.concurrent.Executor; |
|||
import java.util.concurrent.LinkedBlockingDeque; |
|||
import java.util.concurrent.ThreadPoolExecutor; |
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
@Configuration |
|||
@EnableAsync |
|||
public class AsyncTaskPoolConfig { |
|||
|
|||
@Bean("taskExecutor") |
|||
public ThreadPoolExecutor taskExecutor() { |
|||
int i = Runtime.getRuntime().availableProcessors(); |
|||
System.out.println("系统最大线程数:" + i); |
|||
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(i, |
|||
i+1, 5, TimeUnit.SECONDS, new LinkedBlockingDeque<>(200),new NamedThreadFactory("execl导出线程池")); |
|||
System.out.println("execl导出线程池初始化完毕-------------"); |
|||
return threadPoolExecutor; |
|||
} |
|||
} |
@ -0,0 +1,39 @@ |
|||
package com.wm.file.config; |
|||
|
|||
import java.util.concurrent.ThreadFactory; |
|||
import java.util.concurrent.atomic.AtomicInteger; |
|||
|
|||
public class NamedThreadFactory implements ThreadFactory { |
|||
|
|||
private final AtomicInteger poolNumber = new AtomicInteger(1); |
|||
|
|||
private final ThreadGroup threadGroup; |
|||
|
|||
private final AtomicInteger threadNumber = new AtomicInteger(1); |
|||
|
|||
public final String namePrefix; |
|||
|
|||
NamedThreadFactory(String name){ |
|||
SecurityManager s = System.getSecurityManager(); |
|||
threadGroup = (s != null) ? s.getThreadGroup() : |
|||
Thread.currentThread().getThreadGroup(); |
|||
if (null==name || "".equals(name.trim())){ |
|||
name = "pool"; |
|||
} |
|||
namePrefix = name +"-"+ |
|||
poolNumber.getAndIncrement() + |
|||
"-thread-"; |
|||
} |
|||
|
|||
@Override |
|||
public Thread newThread(Runnable r) { |
|||
Thread t = new Thread(threadGroup, r, |
|||
namePrefix + threadNumber.getAndIncrement(), |
|||
0); |
|||
if (t.isDaemon()) |
|||
t.setDaemon(false); |
|||
if (t.getPriority() != Thread.NORM_PRIORITY) |
|||
t.setPriority(Thread.NORM_PRIORITY); |
|||
return t; |
|||
} |
|||
} |
@ -0,0 +1,153 @@ |
|||
package com.wm.file; |
|||
|
|||
import com.alibaba.excel.EasyExcel; |
|||
import com.alibaba.excel.ExcelWriter; |
|||
import com.alibaba.excel.write.metadata.WriteSheet; |
|||
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; |
|||
import com.wm.file.entity.UserEntity; |
|||
import com.wm.file.mapper.UserMapper; |
|||
import com.wm.file.service.UserService; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.util.CollectionUtils; |
|||
import org.springframework.web.bind.annotation.GetMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
import javax.annotation.Resource; |
|||
import javax.servlet.http.HttpServletResponse; |
|||
import java.io.IOException; |
|||
import java.io.OutputStream; |
|||
import java.net.URLEncoder; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.concurrent.CompletableFuture; |
|||
import java.util.concurrent.ThreadPoolExecutor; |
|||
|
|||
/** |
|||
* @ClassName:FileController |
|||
* @Description: |
|||
* @Author:xiaomage |
|||
* @Date:2020/6/15 22:42 |
|||
**/ |
|||
@RestController |
|||
public class FileController { |
|||
|
|||
|
|||
|
|||
@Autowired |
|||
private UserMapper userMapper; |
|||
|
|||
@Autowired |
|||
private ThreadPoolExecutor threadPoolExecutor; |
|||
|
|||
@Resource |
|||
private UserService userService; |
|||
|
|||
/** |
|||
* 多线程实现百万导出到excel 50s优化到11s |
|||
* @param response |
|||
* @throws IOException |
|||
*/ |
|||
@GetMapping("/export") |
|||
public void export(HttpServletResponse response) throws IOException { |
|||
OutputStream outputStream = response.getOutputStream(); |
|||
long start = System.currentTimeMillis(); |
|||
System.out.println("-----------任务执行开始-----------"); |
|||
// 设置响应内容
|
|||
response.setContentType("application/vnd.ms-excel"); |
|||
response.setCharacterEncoding("UTF-8");// 防止下载的文件名字乱码
|
|||
try { |
|||
// 文件以附件形式下载
|
|||
response.setHeader("Content-disposition", |
|||
"attachment;filename=down_" + URLEncoder.encode(System.currentTimeMillis() + ".xlsx", "utf-8")); |
|||
//获取总数据量
|
|||
int count = userService.count(); |
|||
//每页多少个
|
|||
int pageSize = 10000; |
|||
//必须放到循环外,否则会刷新流
|
|||
ExcelWriter excelWriter = EasyExcel.write(outputStream).build(); |
|||
List<CompletableFuture> completableFutures = new ArrayList<>(); |
|||
for (int i = 0; i < (count / pageSize) + 1; i++) { |
|||
int finalI = i; |
|||
CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> { |
|||
List<UserEntity> exportList = userService.findLimit(finalI * pageSize, pageSize); |
|||
if (!CollectionUtils.isEmpty(exportList)) { |
|||
WriteSheet writeSheet = EasyExcel. |
|||
writerSheet(finalI, "用户" + (finalI + 1)).head(UserEntity.class) |
|||
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build(); |
|||
synchronized (excelWriter){ |
|||
excelWriter.write(exportList, writeSheet); |
|||
} |
|||
} |
|||
},threadPoolExecutor); |
|||
completableFutures.add(completableFuture); |
|||
} |
|||
for (CompletableFuture completableFuture : completableFutures) { |
|||
completableFuture.join(); |
|||
} |
|||
//刷新流
|
|||
excelWriter.finish(); |
|||
|
|||
System.out.println("本次导出execl任务执行完毕,共消耗: " + (System.currentTimeMillis() - start) + "ms"); |
|||
} catch (Exception e) { |
|||
throw new RuntimeException(e); |
|||
} |
|||
outputStream.flush(); |
|||
response.getOutputStream().close(); |
|||
} |
|||
|
|||
|
|||
@GetMapping("/newExport") |
|||
public void newExport(HttpServletResponse response) throws IOException { |
|||
OutputStream outputStream = response.getOutputStream(); |
|||
long start = System.currentTimeMillis(); |
|||
System.out.println("-----------任务执行开始-----------"); |
|||
// 设置响应内容
|
|||
response.setContentType("application/vnd.ms-excel"); |
|||
response.setCharacterEncoding("UTF-8");// 防止下载的文件名字乱码
|
|||
try { |
|||
// 文件以附件形式下载
|
|||
response.setHeader("Content-disposition", |
|||
"attachment;filename=down_" + URLEncoder.encode(System.currentTimeMillis() + ".xlsx", "utf-8")); |
|||
//获取总数据量
|
|||
int count = userService.count(); |
|||
//每页多少个
|
|||
int pageSize = 10000; |
|||
//必须放到循环外,否则会刷新流
|
|||
ExcelWriter excelWriter = EasyExcel.write(outputStream).build(); |
|||
//先查询第一页
|
|||
List<UserEntity> exportList = userService.findNewPage(null, pageSize); |
|||
if (CollectionUtils.isEmpty(exportList)) { |
|||
throw new RuntimeException("数据库没有数据"); |
|||
} |
|||
WriteSheet writeSheet = EasyExcel.writerSheet(0, "用户" + 1).head(UserEntity.class) |
|||
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build(); |
|||
excelWriter.write(exportList, writeSheet); |
|||
|
|||
UserEntity userEntity = exportList.get(exportList.size() - 1); |
|||
for (int i = 1; i < (count / pageSize) + 1; i++) { |
|||
exportList = userService.findNewPage(userEntity.getId(), pageSize); |
|||
if (CollectionUtils.isEmpty(exportList)) { |
|||
break; |
|||
} |
|||
writeSheet = EasyExcel.writerSheet(i, "用户" + (i + 1)).head(UserEntity.class) |
|||
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build(); |
|||
excelWriter.write(exportList, writeSheet); |
|||
userEntity = exportList.get(exportList.size() - 1); |
|||
} |
|||
//刷新流
|
|||
excelWriter.finish(); |
|||
|
|||
System.out.println("本次导出execl任务执行完毕,共消耗: " + (System.currentTimeMillis() - start) + "ms"); |
|||
} catch (Exception e) { |
|||
throw new RuntimeException(e); |
|||
} |
|||
outputStream.flush(); |
|||
response.getOutputStream().close(); |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
} |
@ -0,0 +1,41 @@ |
|||
package com.wm.file.entity; |
|||
|
|||
import cn.afterturn.easypoi.excel.annotation.Excel; |
|||
import com.alibaba.excel.annotation.ExcelProperty; |
|||
import com.baomidou.mybatisplus.annotation.IdType; |
|||
import com.baomidou.mybatisplus.annotation.TableField; |
|||
import com.baomidou.mybatisplus.annotation.TableId; |
|||
import com.baomidou.mybatisplus.annotation.TableName; |
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
|
|||
import java.io.Serializable; |
|||
import java.util.Date; |
|||
|
|||
@Data |
|||
@TableName(value = "user")//指定表名
|
|||
public class UserEntity implements Serializable { |
|||
|
|||
private static final long serialVersionUID = -5644799954031156649L; |
|||
|
|||
@TableId(value = "id", type = IdType.AUTO)//指定自增策略
|
|||
@ExcelProperty(value = "主键ID" ,index = 0) |
|||
private String id; |
|||
|
|||
@TableField("name") |
|||
@ExcelProperty(value = "姓名" ,index = 1) |
|||
private String name; |
|||
|
|||
@TableField("phone") |
|||
@ExcelProperty(value = "手机" ,index = 2) |
|||
private String phone; |
|||
|
|||
@TableField("ceate_by") |
|||
@ExcelProperty(value = "创建人" ,index = 3) |
|||
private String ceateBy; |
|||
|
|||
@TableField("remark") |
|||
@ExcelProperty(value = "备注" ,index = 4) |
|||
private String remark; |
|||
|
|||
} |
@ -0,0 +1,22 @@ |
|||
package com.wm.file.mapper; |
|||
|
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|||
import com.wm.file.entity.UserEntity; |
|||
import org.apache.catalina.User; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
import org.apache.ibatis.annotations.Param; |
|||
import org.apache.ibatis.annotations.Select; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Mapper |
|||
public interface UserMapper extends BaseMapper<UserEntity> { |
|||
|
|||
@Select("select * from user") |
|||
List<UserEntity> selectAll(); |
|||
|
|||
List<UserEntity> findLimit(@Param("index") int index, @Param("pageSize") int pageSize); |
|||
|
|||
List<UserEntity> findNewPage(@Param("id") String id, @Param("pageSize") int pageSize); |
|||
|
|||
} |
@ -0,0 +1,15 @@ |
|||
package com.wm.file.service; |
|||
|
|||
import com.baomidou.mybatisplus.extension.service.IService; |
|||
import com.wm.file.entity.UserEntity; |
|||
|
|||
import java.io.OutputStream; |
|||
import java.util.List; |
|||
|
|||
public interface UserService extends IService<UserEntity> { |
|||
|
|||
|
|||
List<UserEntity> findLimit(int i, int pageSize); |
|||
|
|||
List<UserEntity> findNewPage(String o, int pageSize); |
|||
} |
@ -0,0 +1,28 @@ |
|||
package com.wm.file.service.impl; |
|||
|
|||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|||
import com.wm.file.entity.UserEntity; |
|||
import com.wm.file.mapper.UserMapper; |
|||
import com.wm.file.service.UserService; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.util.*; |
|||
|
|||
@Service |
|||
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService { |
|||
|
|||
@Override |
|||
public List<UserEntity> findLimit(int index, int pageSize) { |
|||
|
|||
return baseMapper.findLimit(index,pageSize); |
|||
} |
|||
|
|||
@Override |
|||
public List<UserEntity> findNewPage(String id, int pageSize) { |
|||
|
|||
return baseMapper.findNewPage(id,pageSize); |
|||
} |
|||
|
|||
|
|||
|
|||
} |
@ -0,0 +1,193 @@ |
|||
package com.wm.file.util.easypoi; |
|||
|
|||
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; |
|||
import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams; |
|||
import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; |
|||
import org.apache.poi.ss.usermodel.*; |
|||
|
|||
/** |
|||
* @ClassName: ExcelStyleUtil |
|||
* @Description: Excel样式设置 |
|||
* @Author: WM |
|||
* @Date: 2021-07-24 18:23 |
|||
**/ |
|||
public class ExcelStyleUtil implements IExcelExportStyler { |
|||
|
|||
private static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT"); |
|||
private static final short FONT_SIZE_TEN = 10; |
|||
private static final short FONT_SIZE_ELEVEN = 11; |
|||
private static final short FONT_SIZE_TWELVE = 12; |
|||
|
|||
// 标题头样式
|
|||
private CellStyle headerStyle; |
|||
// 数据列标题样式
|
|||
private CellStyle titleStyle; |
|||
// 数据行样式
|
|||
private CellStyle styles; |
|||
|
|||
public ExcelStyleUtil(Workbook workbook) { |
|||
this.init(workbook); |
|||
} |
|||
|
|||
/** |
|||
* 初始化样式 |
|||
* |
|||
* @param workbook |
|||
*/ |
|||
private void init(Workbook workbook) { |
|||
this.headerStyle = initHeaderStyle(workbook); |
|||
this.titleStyle = initTitleStyle(workbook); |
|||
this.styles = initStyles(workbook); |
|||
} |
|||
|
|||
/** |
|||
* 标题头样式 |
|||
* |
|||
* @param color |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public CellStyle getHeaderStyle(short color) { |
|||
return headerStyle; |
|||
} |
|||
|
|||
/** |
|||
* 数据列标题样式 |
|||
* |
|||
* @param color |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public CellStyle getTitleStyle(short color) { |
|||
return titleStyle; |
|||
} |
|||
|
|||
/** |
|||
* 数据行样式 |
|||
* |
|||
* @param parity 表示奇偶行 |
|||
* @param excelExportEntity 数据内容 |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public CellStyle getStyles(boolean parity, ExcelExportEntity excelExportEntity) { |
|||
return styles; |
|||
} |
|||
|
|||
/** |
|||
* 数据行样式 |
|||
* |
|||
* @param cell 单元格 |
|||
* @param dataRow 数据行 |
|||
* @param excelExportEntity 数据内容 |
|||
* @param obj 对象 |
|||
* @param data 数据 |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity excelExportEntity, Object obj, Object data) { |
|||
return getStyles(true, excelExportEntity); |
|||
} |
|||
|
|||
/** |
|||
* 模板使用的样式设置 |
|||
* |
|||
* @param b |
|||
* @param excelForEachParams |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public CellStyle getTemplateStyles(boolean b, ExcelForEachParams excelForEachParams) { |
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* 初始化--标题头样式 |
|||
* |
|||
* @param workbook |
|||
* @return |
|||
*/ |
|||
private CellStyle initHeaderStyle(Workbook workbook) { |
|||
CellStyle style = getBaseCellStyle(workbook); |
|||
style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true)); |
|||
return style; |
|||
} |
|||
|
|||
/** |
|||
* 初始化--数据列标题样式 |
|||
* |
|||
* @param workbook |
|||
* @return |
|||
*/ |
|||
private CellStyle initTitleStyle(Workbook workbook) { |
|||
CellStyle style = getBaseCellStyle(workbook); |
|||
style.setFont(getFont(workbook, FONT_SIZE_ELEVEN, false)); |
|||
// 背景色
|
|||
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); |
|||
style.setFillPattern(FillPatternType.SOLID_FOREGROUND); |
|||
return style; |
|||
} |
|||
|
|||
/** |
|||
* 初始化--数据行样式 |
|||
* |
|||
* @param workbook |
|||
* @return |
|||
*/ |
|||
private CellStyle initStyles(Workbook workbook) { |
|||
CellStyle style = getBaseCellStyle(workbook); |
|||
style.setFont(getFont(workbook, FONT_SIZE_TEN, false)); |
|||
style.setDataFormat(STRING_FORMAT); |
|||
return style; |
|||
} |
|||
|
|||
/** |
|||
* 基础样式 |
|||
* |
|||
* @return |
|||
*/ |
|||
private CellStyle getBaseCellStyle(Workbook workbook) { |
|||
CellStyle style = workbook.createCellStyle(); |
|||
// 下边框
|
|||
style.setBorderBottom(BorderStyle.THIN); |
|||
// 左边框
|
|||
style.setBorderLeft(BorderStyle.THIN); |
|||
// 上边框
|
|||
style.setBorderTop(BorderStyle.THIN); |
|||
// 右边框
|
|||
style.setBorderRight(BorderStyle.THIN); |
|||
// 水平居中
|
|||
style.setAlignment(HorizontalAlignment.CENTER); |
|||
// 上下居中
|
|||
style.setVerticalAlignment(VerticalAlignment.CENTER); |
|||
// 设置自动换行
|
|||
style.setWrapText(true); |
|||
|
|||
return style; |
|||
} |
|||
|
|||
/** |
|||
* 字体样式 |
|||
* |
|||
* @param size 字体大小 |
|||
* @param isBold 是否加粗 |
|||
* @return |
|||
*/ |
|||
private Font getFont(Workbook workbook, short size, boolean isBold) { |
|||
Font font = workbook.createFont(); |
|||
// 字体样式
|
|||
font.setFontName("宋体"); |
|||
// 是否加粗
|
|||
font.setBold(isBold); |
|||
// 字体大小
|
|||
font.setFontHeightInPoints(size); |
|||
return font; |
|||
} |
|||
|
|||
// private void setSheet(Workbook workbook, int width) {
|
|||
// Sheet sheet = workbook.createSheet();
|
|||
// //设置单元格列宽度
|
|||
// sheet.autoSizeColumn(0);
|
|||
// sheet.setColumnWidth(0, 7000);
|
|||
// }
|
|||
} |
@ -0,0 +1,55 @@ |
|||
package com.wm.file.util.easypoi; |
|||
|
|||
import cn.afterturn.easypoi.excel.annotation.ExcelEntity; |
|||
import cn.afterturn.easypoi.excel.annotation.ExcelTarget; |
|||
import cn.afterturn.easypoi.excel.entity.ExportParams; |
|||
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; |
|||
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; |
|||
import cn.afterturn.easypoi.excel.export.ExcelExportService; |
|||
import cn.afterturn.easypoi.exception.excel.ExcelExportException; |
|||
import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum; |
|||
import cn.afterturn.easypoi.util.PoiPublicUtil; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.poi.ss.usermodel.Workbook; |
|||
|
|||
import java.lang.reflect.Field; |
|||
import java.util.ArrayList; |
|||
import java.util.Collection; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* @ClassName: MyExcelExportService |
|||
* @Description: |
|||
* @Author: WM |
|||
* @Date: 2021-07-30 16:54 |
|||
**/ |
|||
@Slf4j |
|||
public class MyExcelExportService { |
|||
|
|||
private static double EXCEL_WIDTH = 25.0D; |
|||
|
|||
public void createSheet(Workbook workbook, ExportParams entity, Class<?> pojoClass, Collection<?> dataSet) { |
|||
if (log.isDebugEnabled()) { |
|||
log.debug("Excel export start ,class is {}", pojoClass); |
|||
log.debug("Excel version is {}", entity.getType().equals(ExcelType.HSSF) ? "03" : "07"); |
|||
} |
|||
|
|||
if (workbook != null && entity != null && pojoClass != null && dataSet != null) { |
|||
try { |
|||
List<ExcelExportEntity> excelParams = new ArrayList(); |
|||
Field[] fileds = PoiPublicUtil.getClassFields(pojoClass); |
|||
ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); |
|||
String targetId = etarget == null ? null : etarget.value(); |
|||
ExcelExportService excelExportService = new ExcelExportService(); |
|||
excelExportService.getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, null, (ExcelEntity) null); |
|||
excelParams.stream().forEach(x -> x.setWidth(EXCEL_WIDTH)); |
|||
excelExportService.createSheetForMap(workbook, entity, excelParams, dataSet); |
|||
} catch (Exception var9) { |
|||
log.error(var9.getMessage(), var9); |
|||
throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, var9.getCause()); |
|||
} |
|||
} else { |
|||
throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,13 @@ |
|||
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&allowMultiQueries=true |
|||
spring.datasource.username=root |
|||
spring.datasource.password=123456 |
|||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver |
|||
|
|||
|
|||
#mybatis-plus.configuration.log-impl: org.apache.ibatis.logging.stdout.StdOutImpl |
|||
mybatis-plus.configuration.map-underscore-to-camel-case: true |
|||
|
|||
mybatis-plus.mapper-locations=classpath*:/mapper/*.xml |
|||
|
|||
|
|||
# 该配置就是将带有下划线的表字段映射为驼峰格式的实体类属性 |
@ -0,0 +1,30 @@ |
|||
<?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.wm.file.mapper.UserMapper"> |
|||
|
|||
<select id="findLimit" resultType="com.wm.file.entity.UserEntity"> |
|||
select id, |
|||
name, |
|||
phone, |
|||
ceate_by as ceateBy |
|||
from user |
|||
order by id asc |
|||
limit #{index},#{pageSize} |
|||
</select> |
|||
|
|||
<select id="findNewPage" resultType="com.wm.file.entity.UserEntity"> |
|||
SELECT id,NAME, |
|||
phone,ceate_by AS ceateBy |
|||
FROM USER |
|||
<if test="id!=null and id!=''"> |
|||
WHERE id > #{id} |
|||
</if> |
|||
ORDER BY id ASC |
|||
LIMIT #{pageSize} |
|||
</select> |
|||
|
|||
</mapper> |
@ -0,0 +1,95 @@ |
|||
package com.wm.file; |
|||
|
|||
import com.github.pagehelper.PageHelper; |
|||
import com.github.pagehelper.PageInfo; |
|||
import com.wm.file.entity.UserEntity; |
|||
import com.wm.file.mapper.UserMapper; |
|||
import com.wm.file.service.UserService; |
|||
import org.junit.Test; |
|||
import org.junit.runner.RunWith; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.boot.test.context.SpringBootTest; |
|||
import org.springframework.test.context.junit4.SpringRunner; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
@RunWith(SpringRunner.class) |
|||
@SpringBootTest(classes = ExportExcelDataApplication.class)//springboot注解
|
|||
public class TestDemo { |
|||
@Autowired |
|||
private UserService userService; |
|||
|
|||
@Autowired |
|||
private UserMapper userMapper; |
|||
@Test |
|||
public void test1(){ |
|||
List<UserEntity> userEntityList = new ArrayList<>(); |
|||
for (int i = 0; i < 200000; i++) { //测试数据
|
|||
UserEntity client = new UserEntity(); |
|||
//client.setBirthday(new Date());
|
|||
client.setName("小明xxxsxsxsxsxsxsxsxsxsx" + i); |
|||
client.setPhone("18797" + i); |
|||
client.setCeateBy("JueYue"); |
|||
client.setRemark("测试" + i); |
|||
|
|||
userEntityList.add(client); |
|||
} |
|||
userService.saveBatch(userEntityList); |
|||
} |
|||
|
|||
@Test |
|||
public void test(){ |
|||
List<UserEntity> userEntityList = new ArrayList<>(); |
|||
UserEntity client = new UserEntity(); |
|||
//client.setBirthday(new Date());
|
|||
client.setName("小明xxxsxsxsxsxsxsxsxsxsx"); |
|||
client.setPhone("18797"); |
|||
client.setCeateBy("JueYue"); |
|||
userEntityList.add(client); |
|||
userService.saveBatch(userEntityList); |
|||
} |
|||
|
|||
@Test |
|||
public void test2 (){ |
|||
PageHelper.startPage(1,100000); |
|||
List<UserEntity> userEntities = userMapper.selectAll(); |
|||
PageInfo<UserEntity> userEntityPageInfo = new PageInfo<>(userEntities); |
|||
System.out.println(userEntityPageInfo.getList()); |
|||
System.out.println(userEntityPageInfo.getPages()); |
|||
} |
|||
} |
|||
|
|||
|
|||
/* CREATE TABLE `user` ( |
|||
`id` bigint(20) NOT NULL AUTO_INCREMENT, |
|||
`name` varchar(255) DEFAULT NULL, |
|||
`phone` varchar(255) DEFAULT NULL, |
|||
`ceate_by` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL, |
|||
`remark` varchar(255) DEFAULT NULL, |
|||
`birthday` datetime DEFAULT NULL, |
|||
`test1` varchar(255) DEFAULT NULL, |
|||
`test2` varchar(255) DEFAULT NULL, |
|||
`test3` varchar(255) DEFAULT NULL, |
|||
`test4` varchar(255) DEFAULT NULL, |
|||
`test5` varchar(255) DEFAULT NULL, |
|||
`test6` varchar(255) DEFAULT NULL, |
|||
`test7` varchar(255) DEFAULT NULL, |
|||
`test8` varchar(255) DEFAULT NULL, |
|||
`test9` varchar(255) DEFAULT NULL, |
|||
`test10` varchar(255) DEFAULT NULL, |
|||
`test11` varchar(255) DEFAULT NULL, |
|||
`test12` varchar(255) DEFAULT NULL, |
|||
`test13` varchar(255) DEFAULT NULL, |
|||
`test14` varchar(255) DEFAULT NULL, |
|||
`test15` varchar(255) DEFAULT NULL, |
|||
`test16` varchar(255) DEFAULT NULL, |
|||
`test17` varchar(255) DEFAULT NULL, |
|||
`test18` varchar(255) DEFAULT NULL, |
|||
`test19` varchar(255) DEFAULT NULL, |
|||
`test20` varchar(255) DEFAULT NULL, |
|||
`test21` varchar(255) DEFAULT NULL, |
|||
PRIMARY KEY (`id`) |
|||
) ENGINE=InnoDB AUTO_INCREMENT=400001 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;*/ |
|||
|
|||
|
@ -0,0 +1,13 @@ |
|||
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&allowMultiQueries=true |
|||
spring.datasource.username=root |
|||
spring.datasource.password=123456 |
|||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver |
|||
|
|||
|
|||
#mybatis-plus.configuration.log-impl: org.apache.ibatis.logging.stdout.StdOutImpl |
|||
mybatis-plus.configuration.map-underscore-to-camel-case: true |
|||
|
|||
mybatis-plus.mapper-locations=classpath*:/mapper/*.xml |
|||
|
|||
|
|||
# �����þ��ǽ������»��ߵı��ֶ�ӳ��Ϊ�շ��ʽ��ʵ�������� |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,30 @@ |
|||
<?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.wm.file.mapper.UserMapper"> |
|||
|
|||
<select id="findLimit" resultType="com.wm.file.entity.UserEntity"> |
|||
select id, |
|||
name, |
|||
phone, |
|||
ceate_by as ceateBy |
|||
from user |
|||
order by id asc |
|||
limit #{index},#{pageSize} |
|||
</select> |
|||
|
|||
<select id="findNewPage" resultType="com.wm.file.entity.UserEntity"> |
|||
SELECT id,NAME, |
|||
phone,ceate_by AS ceateBy |
|||
FROM USER |
|||
<if test="id!=null and id!=''"> |
|||
WHERE id > #{id} |
|||
</if> |
|||
ORDER BY id ASC |
|||
LIMIT #{pageSize} |
|||
</select> |
|||
|
|||
</mapper> |
Binary file not shown.
Loading…
Reference in new issue