当前位置: 首页 > news >正文

使用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.xmlBMapper.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_dcifcount(dcif开头表数量)table_name_soucount(sou_开头表数量)
dcif_a1sou_one1
dcif_a1sou_two1
dcif_b2sou_four1
dcif_b2sou_three1
dcif_b2sou_one1

说明

  • 代码中通过 MyBatis 分别查询 a 表中 dcif_ 开头的表、b 表中关联的 sou_ 表,再组装成 Excel 数据。
  • 若数据库表结构或数据有变化,只需调整 Mapper 中的 SQL 即可。
  • EasyExcel 会自动处理 Excel 列的映射和格式,无需手动创建 Excel 文档。
http://www.dtcms.com/a/540245.html

相关文章:

  • 哈希表的HashMap 和 HashSet
  • 从编程语言出发如何考虑投入研发RAG智能体
  • 企业网站的推广方式和手段有哪些网站的建设主题
  • 微信服务号菜单链接网站怎么做创意界面
  • Qt 网络聊天室项目
  • Vue 3 中 ref 和 reactive 的区别
  • 第十章自我表达的路径--创建第二大脑读书笔记
  • 【码源】智能无人仓库管理系统(详细码源上~基于React+TypeScript+Vite):
  • 初识 Spring Boot
  • 人工智能助推城市规划新纪元:佛山年会深度解析大模型革新
  • c 网站开发 pdf洛阳已经开始群体感染了
  • 【Delphi】操纵EXE文件中版本信息(RT_VERSION)
  • 一周新闻热点事件seo 哪些媒体网站可以发新闻
  • Vue3的Pinia详解
  • 移动端性能监控探索:可观测 Android 采集探针架构与实现
  • OSS文件上传错误No buffer space available
  • 搜狐视频网站联盟怎么做企业网站优化的方式
  • c 网站做微信支付功能济源建设工程管理处网站
  • Visual Studio 编译带显卡加速的 OpenCV
  • 【EDA软件】【文件合并烧录操作方法】
  • Termux 部署 NextCloude 私人云盘,旧手机变云盘
  • 【优选算法】DC-Mergesort-Harmonies:分治-归并的算法之谐
  • WPF 控件速查 PDF 笔记(可直接落地版)(带图片)
  • 淘宝实时拍立淘按图搜索数据|商品详情|数据分析提取教程
  • WinCC的CS架构部署
  • 房地产店铺首页设计过程门户网站优化报价
  • 河南建设安全监督网站WordPress集成tipask
  • Taro 自定义tab栏和自定义导航栏
  • 辛格迪客户案例 | 迈威生物电子合约(eSignDMS)项目
  • 《微信小程序》第七章:TabBar设计