使用mybatis 實現(xiàn)量表關(guān)聯(lián)并且統(tǒng)計數(shù)據(jù)量的步驟和代碼
我們可以使用 Spring Boot + MyBatis + EasyExcel 技術(shù)棧來完成。以下是詳細的實現(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.xml 和 BMapper.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_dcif | count(dcif開頭表數(shù)量) | table_name_sou | count(sou_開頭表數(shù)量) |
|---|---|---|---|
| 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表中關(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)文章希望大家以后多多支持腳本之家!
- Mybatisplus多表關(guān)聯(lián)分頁查詢多種實現(xiàn)方式
- mybatisplus 多表關(guān)聯(lián)條件分頁查詢的實現(xiàn)
- mybatis主從表關(guān)聯(lián)查詢,返回對象帶有集合屬性解析
- 關(guān)于QueryWrapper,實現(xiàn)MybatisPlus多表關(guān)聯(lián)查詢方式
- mybatis-plus多表關(guān)聯(lián)查詢功能的實現(xiàn)
- 詳解MyBatis如何在大數(shù)據(jù)量下使用流式查詢進行數(shù)據(jù)同步
- 使用MyBatis查詢千萬級數(shù)據(jù)量操作實現(xiàn)
- MyBatisPlus?大數(shù)據(jù)量查詢慢的問題解決
相關(guān)文章
spring boot整合redis實現(xiàn)RedisTemplate三分鐘快速入門
這篇文章主要介紹了spring boot整合redis實現(xiàn)RedisTemplate三分鐘快速入門,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
詳解spring boot使用@Retryable來進行重處理
本篇文章主要介紹了詳解spring boot使用@Retryable來進行重處理,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
Java處理InterruptedException異常的理論與實踐
在使用Java的過程中,有個情景或許很多人見過,您在編寫一個測試程序,程序需要暫停一段時間,于是調(diào)用 Thread.sleep()。但是編譯器或 IDE 報錯說沒有處理檢查到的 InterruptedException。InterruptedException 是什么呢,為什么必須處理它?下面跟著小編一起來看看。2016-08-08

