使用mybatis 实现量表关联并且统计数据量的步骤和代码

 更新时间:2025年10月29日 09:54:41   作者:骑士雄师  
本文介绍了使用SpringBoot+MyBatis+EasyExcel技术栈实现数据库查询结果导出为Excel文件的步骤,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

我们可以使用 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=utf8
    username: 你的数据库用户名
    password: 你的数据库密码
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  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 开头的表及对应 count
        List<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_ 表的 count
                String souCount = aMapper.selectCountByTableName(souTable);
                ExcelVO vo = new ExcelVO();
                vo.setTableNameDcif(dcifTableName);
                vo.setDcifCount(dcifCount);
                vo.setTableNameSou(souTable);
                vo.setSouCount(souCount);
                excelData.add(vo);
            }
        }
        // 3. 导出 Excel
        EasyExcel.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 {
    @Resource
    private ExcelService excelService;
    @Test
    void 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 文档。

到此这篇关于使用mybatis 实现量表关联并且统计数据量的步骤和代码的文章就介绍到这了,更多相关mybatis量表关联内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Windows10 Java环境变量配置过程图解

    Windows10 Java环境变量配置过程图解

    这篇文章主要介绍了Windows10 Java环境变量配置过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • Spring-Security对HTTP相应头的安全支持方式

    Spring-Security对HTTP相应头的安全支持方式

    这篇文章主要介绍了Spring-Security对HTTP相应头的安全支持方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • shiro整合springboot前后端分离

    shiro整合springboot前后端分离

    这篇文章主要介绍了shiro整合springboot前后端分离,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • intellij idea隐藏.iml和.idea等自动生成文件的问题

    intellij idea隐藏.iml和.idea等自动生成文件的问题

    这篇文章主要介绍了intellij idea隐藏.iml和.idea等自动生成文件的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • Spring详细讲解循环依赖是什么

    Spring详细讲解循环依赖是什么

    这篇文章主要介绍了Java中的Spring循环依赖详情,文章基于Java的相关资料展开详细介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • SpringBoot实现单点登录的实现详解

    SpringBoot实现单点登录的实现详解

    在现代的Web应用程序中,单点登录(Single Sign-On)已经变得越来越流行,在本文中,我们将使用Spring Boot构建一个基本的单点登录系统,需要的可以参考一下
    2023-05-05
  • Spring中AOP的切点、通知、切点表达式及知识要点整理

    Spring中AOP的切点、通知、切点表达式及知识要点整理

    这篇文章主要介绍了Spring中AOP的切点、通知、切点表达式及知识要点整理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • Java中的位运算符全解

    Java中的位运算符全解

    这篇文章主要为大家详细介绍了Java中的位运算符,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • 普通对象使用spring容器中的对象的实现方法

    普通对象使用spring容器中的对象的实现方法

    这篇文章主要介绍了普通对象使用spring容器中的对象的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • JDK19新特性使用实例详解

    JDK19新特性使用实例详解

    这篇文章主要为大家介绍了JDK19新特性使用实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09

最新评论