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

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

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

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

步驟1:創(chuàng)建項(xià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ū)動(dòng) -->
    <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 簡(jiǎn)化代碼 -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <!-- 測(cè)試依賴 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

步驟2:配置數(shù)據(jù)庫(kù)連接

application.yml 中配置 MySQL 連接:

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

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

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

A.java(對(duì)應(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(對(duì)應(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)出實(shí)體類

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(可選,若用注解可跳過(guò))

如果使用 XML 配置,在 src/main/resources/mapper 下創(chuàng)建 AMapper.xmlBMapper.xml,這里示例用注解實(shí)現(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 開頭的表及對(duì)應(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();
            // 查詢?cè)?dcif 表對(duì)應(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)計(jì)")
               .doWrite(excelData);
    }
}

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

方式1:通過(guò) 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;
    }
}

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

方式2:通過(guò)測(cè)試類直接執(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

說(shuō)明

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

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

相關(guān)文章

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

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

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

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

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

    shiro整合springboot前后端分離

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

    intellij idea隱藏.iml和.idea等自動(dòng)生成文件的問題

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

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

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

    SpringBoot實(shí)現(xiàn)單點(diǎn)登錄的實(shí)現(xiàn)詳解

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

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

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

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

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

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

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

    JDK19新特性使用實(shí)例詳解

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

最新評(píng)論

邓州市| 保康县| 湟中县| 漠河县| 武胜县| 突泉县| 沁阳市| 东方市| 扎囊县| 牟定县| 教育| 库车县| 万载县| 竹溪县| 金华市| 内乡县| 宁晋县| 贵州省| 崇州市| 台北县| 钟祥市| 晋城| 岳普湖县| 铁岭市| 城固县| 宣威市| 丹寨县| 商河县| 正蓝旗| 巫溪县| 云梦县| 高陵县| 隆安县| 罗田县| 大荔县| 浦城县| 门头沟区| 专栏| 邳州市| 商水县| 武胜县|