使用mybatis 实现量表关联,并且统计数据量
我们可以使用 Spring Boot + MyBatis + EasyExcel 技术栈来完成。以下是详细的实现步骤和代码:
步骤1:创建项目并添加依赖
在 pom.xml 中添加以下依赖:
<dependencies><!-- Spring Boot 基础 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- MyBatis 整合 Spring Boot --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.2</version></dependency><!-- MySQL 驱动 --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><!-- EasyExcel 用于生成 Excel --><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.3.2</version></dependency><!-- Lombok 简化代码 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!-- 测试依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>
步骤2:配置数据库连接
在 application.yml 中配置 MySQL 连接:
spring:datasource:url: jdbc:mysql://localhost:3306/test_db?useSSL=false&serverTimezone=UTC&characterEncoding=utf8username: 你的数据库用户名password: 你的数据库密码driver-class-name: com.mysql.cj.jdbc.Drivermybatis:mapper-locations: classpath:mapper/*.xml
步骤3:创建实体类
3.1 数据库表映射实体类
-
A.java(对应表a):package com.example.entity;import lombok.Data;@Data public class A {private String tableName;private String count;private String id; } -
B.java(对应表b):package com.example.entity;import lombok.Data;@Data public class B {private String tableNameDcif;private String tableNameSou;private String id; }
3.2 Excel 导出实体类
ExcelVO.java(用于封装 Excel 行数据):
package com.example.vo;import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;@Data
public class ExcelVO {@ExcelProperty("table_name_dcif")private String tableNameDcif;@ExcelProperty("count(dcif开头表数量)")private String dcifCount;@ExcelProperty("table_name_sou")private String tableNameSou;@ExcelProperty("count(sou_开头表数量)")private String souCount;
}
步骤4:创建 Mapper 接口和 XML
4.1 Mapper 接口
-
AMapper.java:package com.example.mapper;import com.example.entity.A; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select;import java.util.List;@Mapper public interface AMapper {@Select("SELECT table_name, `count` FROM a WHERE table_name LIKE 'dcif_%'")List<A> selectDcifTables();@Select("SELECT `count` FROM a WHERE table_name = #{tableName}")String selectCountByTableName(String tableName); } -
BMapper.java:package com.example.mapper;import com.example.entity.B; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select;import java.util.List;@Mapper public interface BMapper {@Select("SELECT table_name_sou FROM b WHERE table_name_dcif = #{tableNameDcif}")List<String> selectSouTablesByDcif(String tableNameDcif); }
4.2 Mapper XML(可选,若用注解可跳过)
如果使用 XML 配置,在 src/main/resources/mapper 下创建 AMapper.xml 和 BMapper.xml,这里示例用注解实现,可省略。
步骤5:创建 Service 层
ExcelService.java:
package com.example.service;import com.alibaba.excel.EasyExcel;
import com.example.entity.A;
import com.example.mapper.AMapper;
import com.example.mapper.BMapper;
import com.example.vo.ExcelVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;import java.util.ArrayList;
import java.util.List;@Service
@RequiredArgsConstructor
public class ExcelService {private final AMapper aMapper;private final BMapper bMapper;public void exportExcel(String filePath) {// 1. 查询所有 dcif 开头的表及对应 countList<A> dcifTables = aMapper.selectDcifTables();// 2. 组装 Excel 数据List<ExcelVO> excelData = new ArrayList<>();for (A dcifTable : dcifTables) {String dcifTableName = dcifTable.getTableName();String dcifCount = dcifTable.getCount();// 查询该 dcif 表对应的所有 sou_ 表List<String> souTables = bMapper.selectSouTablesByDcif(dcifTableName);for (String souTable : souTables) {// 查询 sou_ 表的 countString souCount = aMapper.selectCountByTableName(souTable);ExcelVO vo = new ExcelVO();vo.setTableNameDcif(dcifTableName);vo.setDcifCount(dcifCount);vo.setTableNameSou(souTable);vo.setSouCount(souCount);excelData.add(vo);}}// 3. 导出 ExcelEasyExcel.write(filePath, ExcelVO.class).sheet("数据统计").doWrite(excelData);}
}
步骤6:创建 Controller 或测试类
方式1:通过 Controller 触发导出
ExcelController.java:
package com.example.controller;import com.example.service.ExcelService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/excel")
@RequiredArgsConstructor
public class ExcelController {private final ExcelService excelService;@GetMapping("/export")public String exportExcel() {String filePath = "D:/data_statistics.xlsx"; // 导出文件路径excelService.exportExcel(filePath);return "Excel 导出成功,路径:" + filePath;}
}
启动项目后,访问 http://localhost:8080/excel/export 即可触发导出。
方式2:通过测试类直接执行
ExcelServiceTest.java:
package com.example.service;import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;@SpringBootTest
class ExcelServiceTest {@Resourceprivate ExcelService excelService;@Testvoid exportExcel() {String filePath = "D:/data_statistics.xlsx";excelService.exportExcel(filePath);System.out.println("Excel 导出成功,路径:" + filePath);}
}
最终效果
导出的 Excel 格式如下:
| table_name_dcif | count(dcif开头表数量) | table_name_sou | count(sou_开头表数量) |
|---|---|---|---|
| dcif_a | 1 | sou_one | 1 |
| dcif_a | 1 | sou_two | 1 |
| dcif_b | 2 | sou_four | 1 |
| dcif_b | 2 | sou_three | 1 |
| dcif_b | 2 | sou_one | 1 |
说明
- 代码中通过 MyBatis 分别查询
a表中dcif_开头的表、b表中关联的sou_表,再组装成 Excel 数据。 - 若数据库表结构或数据有变化,只需调整 Mapper 中的 SQL 即可。
- EasyExcel 会自动处理 Excel 列的映射和格式,无需手动创建 Excel 文档。
