流程
依赖 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <version > 1.18.10</version > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > fastjson</artifactId > <version > 1.2.78</version > </dependency > <dependency > <groupId > io.springfox</groupId > <artifactId > springfox-swagger2</artifactId > <version > 2.9.2</version > </dependency > <dependency > <groupId > io.springfox</groupId > <artifactId > springfox-swagger-ui</artifactId > <version > 2.9.2</version > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > easyexcel</artifactId > <version > 3.0.3</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > </dependency > <dependency > <groupId > com.baomidou</groupId > <artifactId > mybatis-plus-boot-starter</artifactId > <version > 3.4.1</version > </dependency >
结构
搭建 1、配置数据库连接信息 1 2 3 4 5 6 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/core?useUnicode=true&characterEncoding=utf8 username: root password: 123456
2、建立实体类对象 根据 Excel 表建立对应接收的实体类对象
UserExcel
如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @Data public class UserExcel { @ExcelProperty(index = 0) private String username; @ExcelProperty(index = 1) private String password; @ExcelProperty(index = 2) private Integer age; }
根据数据库字段建立对应的实体类对象
User
如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 @Data public class User { @TableId(type = IdType.AUTO) private Integer userId; private String username; private String password; private Integer age; private String habbit; }
3、mapper 1 2 3 public interface UserMapper extends BaseMapper <User > {}
4、service 创建 UserService 接口,继承 IService
1 2 3 4 5 6 7 8 9 10 public interface UserService extends IService <User > { void saveByExcel (MultipartFile file) ; }
实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 @Service public class UserServiceImpl extends ServiceImpl <UserMapper , User > implements UserService { @Resource private UserService userService; @Override public void saveByExcel (MultipartFile file) { try { InputStream in = file.getInputStream(); EasyExcel.read(in, UserExcel.class, new ExcelDataListener(userService)).sheet().doRead(); } catch (Exception e) { e.printStackTrace(); } } }
5、创建 Excel 监听类 按照官网的注释,监听类不能被 spring 管理,每次读取 excel 都要 new,然后监听类里面用到 spring 可以通过构造方法传进去
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 @Slf4j public class ExcelDataListener extends AnalysisEventListener <UserExcel > { private UserService userService; public ExcelDataListener (UserService userService) { this .userService = userService; } @Override public void invoke (UserExcel data, AnalysisContext context) { log.info("解析到一条数据:{}" , JSON.toJSONString(data)); User user = new User(); user.setUsername(data.getUsername()); user.setPassword(data.getPassword()); user.setAge(data.getAge()); user.setHabbit("读书" ); userService.save(user); } @Override public void doAfterAllAnalysed (AnalysisContext context) { log.info("所有数据解析完成!" ); } }
6、controller 1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Api(tags = "将Excel保存到数据库") @RestController @RequestMapping("/excel") public class ExcelController { @Resource private UserService userService; @PostMapping("/save") public String save (MultipartFile file) { userService.saveByExcel(file); return "success" ; } }
7、启动类标上注解 1 2 3 4 5 6 7 8 9 10 11 12 13 @SpringBootApplication @EnableSwagger2 @MapperScan("com.shiguang.excel.mapper") public class ExcelApplication { public static void main (String[] args) { SpringApplication.run(ExcelApplication.class, args); } }
测试 访问默认端口: http://localhost:8080/swagger-ui.html
效果
优化 可以考虑使用 list 集合存储读取到每行的 Excel 数据,达到一个数量批量添加到数据库中,减少数据库的访问 关于 Mybatis Plus 的@TableId 注解 这个注解主要用于对应数据库表的实体类中的主键属性。
字段 说明 value 映射主键字段的名字 type 设置主键类型、如果数据库主键设置了自增建议使用“AUTO”
其中 type 有六种类型,后三种只有插入主键为空时,才会自动填充
type 说明 IdType.AUTO 数据库自增 ID(需要数据库设置对应字段的自动递增) IdType.ASSIGN_ID 如果不设置类型值,默认策略(自 3.3.0 起)。该策略会使用雪花算法自动生成主键 ID,主键类型为长或字符串(分别对应的 MySQL 的表字段为 BIGINT 和 VARCHAR) IdType.ASSIGN_UUID 主键类型为 String,对应 MySQL 的表分段为 VARCHAR IdType.INPUT 必须手动输入,数据库设置了自增也没用 IdType.NONE 数据库未设置主键类型,将会跟随全局(全局的主键策略如果没有设置,默认是雪花算法)
变量策略配置 如果希望全部使用 AUTO 策略,可以在 application.yml
中添加以下配置:
1 2 3 4 mybatis-plus: global-config: db-config: id-type: auto
Spring Boot整合EasyExcel将Excel数据导入数据库