最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用mybatis 實現(xiàn)量表關(guān)聯(lián)并且統(tǒng)計數(shù)據(jù)量的步驟和代碼

 更新時間:2025年10月29日 09:54:41   作者:騎士雄師  
本文介紹了使用SpringBoot+MyBatis+EasyExcel技術(shù)棧實現(xiàn)數(shù)據(jù)庫查詢結(jié)果導(dǎo)出為Excel文件的步驟,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧

我們可以使用 Spring Boot + MyBatis + EasyExcel 技術(shù)棧來完成。以下是詳細(xì)的實現(xiàn)步驟和代碼:

步驟1:創(chuàng)建項目并添加依賴

pom.xml 中添加以下依賴:

<dependencies>
    <!-- Spring Boot 基礎(chǔ) -->
    <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 驅(qū)動 -->
    <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:配置數(shù)據(jù)庫連接

application.yml 中配置 MySQL 連接:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test_db?useSSL=false&serverTimezone=UTC&characterEncoding=utf8
    username: 你的數(shù)據(jù)庫用戶名
    password: 你的數(shù)據(jù)庫密碼
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  mapper-locations: classpath:mapper/*.xml

步驟3:創(chuàng)建實體類

3.1 數(shù)據(jù)庫表映射實體類

A.java(對應(yīng)表 a):

package com.example.entity;
import lombok.Data;
@Data
public class A {
    private String tableName;
    private String count;
    private String id;
}

B.java(對應(yīng)表 b):

package com.example.entity;
import lombok.Data;
@Data
public class B {
    private String tableNameDcif;
    private String tableNameSou;
    private String id;
}

3.2 Excel 導(dǎo)出實體類

ExcelVO.java(用于封裝 Excel 行數(shù)據(jù)):

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開頭表數(shù)量)")
    private String dcifCount;
    @ExcelProperty("table_name_sou")
    private String tableNameSou;
    @ExcelProperty("count(sou_開頭表數(shù)量)")
    private String souCount;
}

步驟4:創(chuàng)建 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 下創(chuàng)建 AMapper.xmlBMapper.xml,這里示例用注解實現(xiàn),可省略。

步驟5:創(chuàng)建 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 開頭的表及對應(yīng) count
        List<A> dcifTables = aMapper.selectDcifTables();
        // 2. 組裝 Excel 數(shù)據(jù)
        List<ExcelVO> excelData = new ArrayList<>();
        for (A dcifTable : dcifTables) {
            String dcifTableName = dcifTable.getTableName();
            String dcifCount = dcifTable.getCount();
            // 查詢該 dcif 表對應(yīng)的所有 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. 導(dǎo)出 Excel
        EasyExcel.write(filePath, ExcelVO.class)
               .sheet("數(shù)據(jù)統(tǒng)計")
               .doWrite(excelData);
    }
}

步驟6:創(chuàng)建 Controller 或測試類

方式1:通過 Controller 觸發(fā)導(dǎo)出

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"; // 導(dǎo)出文件路徑
        excelService.exportExcel(filePath);
        return "Excel 導(dǎo)出成功,路徑:" + filePath;
    }
}

啟動項目后,訪問 http://localhost:8080/excel/export 即可觸發(fā)導(dǎo)出。

方式2:通過測試類直接執(zhí)行

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 導(dǎo)出成功,路徑:" + filePath);
    }
}

最終效果

導(dǎo)出的 Excel 格式如下:

table_name_dcifcount(dcif開頭表數(shù)量)table_name_soucount(sou_開頭表數(shù)量)
dcif_a1sou_one1
dcif_a1sou_two1
dcif_b2sou_four1
dcif_b2sou_three1
dcif_b2sou_one1

說明

  • 代碼中通過 MyBatis 分別查詢 a 表中 dcif_ 開頭的表、b 表中關(guān)聯(lián)的 sou_ 表,再組裝成 Excel 數(shù)據(jù)。
  • 若數(shù)據(jù)庫表結(jié)構(gòu)或數(shù)據(jù)有變化,只需調(diào)整 Mapper 中的 SQL 即可。
  • EasyExcel 會自動處理 Excel 列的映射和格式,無需手動創(chuàng)建 Excel 文檔。

到此這篇關(guān)于使用mybatis 實現(xiàn)量表關(guān)聯(lián)并且統(tǒng)計數(shù)據(jù)量的步驟和代碼的文章就介紹到這了,更多相關(guān)mybatis量表關(guān)聯(lián)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Windows10 Java環(huán)境變量配置過程圖解

    Windows10 Java環(huán)境變量配置過程圖解

    這篇文章主要介紹了Windows10 Java環(huán)境變量配置過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • Spring-Security對HTTP相應(yīng)頭的安全支持方式

    Spring-Security對HTTP相應(yīng)頭的安全支持方式

    這篇文章主要介紹了Spring-Security對HTTP相應(yīng)頭的安全支持方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • shiro整合springboot前后端分離

    shiro整合springboot前后端分離

    這篇文章主要介紹了shiro整合springboot前后端分離,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • intellij idea隱藏.iml和.idea等自動生成文件的問題

    intellij idea隱藏.iml和.idea等自動生成文件的問題

    這篇文章主要介紹了intellij idea隱藏.iml和.idea等自動生成文件的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Spring詳細(xì)講解循環(huán)依賴是什么

    Spring詳細(xì)講解循環(huán)依賴是什么

    這篇文章主要介紹了Java中的Spring循環(huán)依賴詳情,文章基于Java的相關(guān)資料展開詳細(xì)介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • SpringBoot實現(xiàn)單點登錄的實現(xiàn)詳解

    SpringBoot實現(xiàn)單點登錄的實現(xiàn)詳解

    在現(xiàn)代的Web應(yīng)用程序中,單點登錄(Single?Sign-On)已經(jīng)變得越來越流行,在本文中,我們將使用Spring?Boot構(gòu)建一個基本的單點登錄系統(tǒng),需要的可以參考一下
    2023-05-05
  • Spring中AOP的切點、通知、切點表達(dá)式及知識要點整理

    Spring中AOP的切點、通知、切點表達(dá)式及知識要點整理

    這篇文章主要介紹了Spring中AOP的切點、通知、切點表達(dá)式及知識要點整理,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • Java中的位運(yùn)算符全解

    Java中的位運(yùn)算符全解

    這篇文章主要為大家詳細(xì)介紹了Java中的位運(yùn)算符,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • 普通對象使用spring容器中的對象的實現(xiàn)方法

    普通對象使用spring容器中的對象的實現(xiàn)方法

    這篇文章主要介紹了普通對象使用spring容器中的對象的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • JDK19新特性使用實例詳解

    JDK19新特性使用實例詳解

    這篇文章主要為大家介紹了JDK19新特性使用實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09

最新評論

景宁| 汶川县| 武清区| 西乌| 泸定县| 贵南县| 永寿县| 文登市| 兰州市| 松江区| 吉林省| 临城县| 宁晋县| 青海省| 铁力市| 沈阳市| 湟中县| 商水县| 天全县| 柳林县| 凤山县| 河池市| 抚顺县| 罗田县| 富平县| 镇江市| 徐闻县| 南川市| 寻甸| 淄博市| 丹棱县| 文山县| 车致| 吉木萨尔县| 屯门区| 军事| 扶余县| 大悟县| 茶陵县| 石阡县| 宿州市|