springboot + thymeleaf + layui 初嘗試

一、背景

公司運營的同事有個任務,提供一個數據文件給我,然后從數據庫中找出對應的加密串再導出來給他。這個活不算是很難,但時不時就會有需求。

同事給我的文件有時是給excel表格,每一行有4列,逗號隔開,合并成一列數據,這類文件需要把所有數據復制到文本編輯器進行處理,把逗號替換成空格,再使用列塊編輯模式復制2、3、4列替換原來的excel數據。有時是給.DAT的文件,這類文件需要手動修改后綴為csv,修改后就跟普通的excel表格一樣打開,去掉第一列。最后添加一行表頭,再對第一列進行篩選去重。

去重后準備導入到數據庫的臨時表,在此之前需要手動清空臨時表的歷史數據。導入后再執行一段sql語句,然后把查詢結果導出為excel文件給到同事。

這樣的工作重復重復再重復,確實挺無趣的,何不鼓搗一個工具給同事自己去處理?

二、步驟
2.1 項目搭建

項目結構如下圖:
在這里插入圖片描述

創建項目,使用springboot 2.5.14poi 4.1.2mybatis,前端使用 thymeleaf + layui-v2.6.8

具體看maven配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.14</version></parent><modelVersion>4.0.0</modelVersion><groupId>com.xxx</groupId><artifactId>test</artifactId><version>1.0</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><!-- Spring框架基本的核心工具 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId></dependency><!-- SpringBoot Web容器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-el</artifactId></exclusion><exclusion><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-websocket</artifactId></exclusion></exclusions></dependency><!-- thymeleaf --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.4</version><exclusions><exclusion><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId></exclusion></exclusions></dependency><!-- 阿里數據庫連接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.16</version></dependency><!-- Mysql驅動包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--常用工具類 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency><!-- io常用工具類 --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version></dependency><!-- excel工具 --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version><exclusions><exclusion><groupId>org.apache.commons</groupId><artifactId>commons-math3</artifactId></exclusion><exclusion><groupId>org.zaxxer</groupId><artifactId>SparseBitSet</artifactId></exclusion></exclusions></dependency><!-- servlet包 --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>${java.version}</source><target>${java.version}</target><encoding>${project.build.sourceEncoding}</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.7.3</version><executions><execution><goals><goal>repackage</goal></goals></execution></executions><configuration><mainClass>com.xxx.AdminApplication</mainClass></configuration></plugin></plugins></build></project>

為了節省jar包體積,盡可能把不需要的依賴給排除。

2.2 后端處理邏輯

Controller內容

import com.xxx.domain.Result;
import com.xxx.domain.CellItem;
import com.xxx.domain.HelmetConfig;
import com.xxx.service.HelmetService;
import com.xxx.utils.file.DatUtil;
import com.xxx.utils.poi.ExcelUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;/*** 通用請求處理** @author admin*/
@Controller
public class CommonController {public static final String[] EXCEL_EXTENSION = {"xls", "xlsx", "XLS", "XLSX"};public static final String DAT_EXTENSION = "DAT";@Resourceprivate HelmetService helmetService;@GetMapping(value = {"/", "/index"})public String index(Model model) {return "index";}/*** 通用下載請求*/@GetMapping("/download")public void fileDownload(HttpServletResponse response) {List<HelmetConfig> list = helmetService.queryAll();ExcelUtil<HelmetConfig> util = new ExcelUtil<>(HelmetConfig.class);util.exportExcel(response, list, "Sheet1");}/*** 通用上傳請求(單個)*/@PostMapping("/upload")@ResponseBodypublic Result uploadFile(MultipartFile file) {if (file == null || file.isEmpty()) {return Result.error("文件不能為空");}String extension = FilenameUtils.getExtension(file.getOriginalFilename());List<CellItem> list;if (Arrays.asList(EXCEL_EXTENSION).contains(extension)) {list = ExcelUtil.getData(file);} else if (DAT_EXTENSION.equalsIgnoreCase(extension)) {list = DatUtil.readDat(file);} else {return Result.error("文件格式不正確");}if (list.isEmpty()) {return Result.error("操作失敗,請重試");}helmetService.batchAdd(list);return Result.success("操作成功,請點擊【下載文件】");}
}

數據庫根據最后的查詢sql創建一個視圖(View),通過mybatis對這個試圖進行查詢,然后把結構進行導出即可。

ExcelUtil.getData()內容

public static List<CellItem> getData(MultipartFile file) {InputStream inputStream = null;List<CellItem> rowList = new ArrayList<>();try {inputStream = file.getInputStream();XSSFWorkbook wb = new XSSFWorkbook(inputStream);int ignoreRows = 0;int sheetNum = wb.getNumberOfSheets();//for循環:取前N個表,下標從0開始for (int i = 0; i < sheetNum; i++) {XSSFSheet sheetI = wb.getSheetAt(i);//列數int cellSize = sheetI.getRow(0).getLastCellNum();//第N+1行開始,可以通過傳參,從第N+1行開始取for (int rowIndex = ignoreRows; rowIndex <= sheetI.getLastRowNum(); rowIndex++) {XSSFRow row = sheetI.getRow(rowIndex);if (row == null) {continue;}if (cellSize == 1) {XSSFCell cell = row.getCell(0);String cellValue = cell.getStringCellValue();if (cellValue.contains(",")) {CellItem item = new CellItem();String[] cells = cellValue.split(",");String deviceId = cells[1];Boolean exists = checkExists(rowList, deviceId);if (exists) {continue;}item.setDeviceId(deviceId.trim());item.setProductId(cells[2]);item.setMac(cells[3]);rowList.add(item);}} else if (cellSize == 4){//在每行中的每一列,從下標1開始,忽略第一列,一直取到所有CellItem item = new CellItem();String deviceId = row.getCell(1).getStringCellValue();Boolean exists = checkExists(rowList, deviceId);if (exists) {continue;}item.setDeviceId(deviceId.trim());item.setProductId(row.getCell(2).getStringCellValue());item.setMac(row.getCell(3).getStringCellValue());rowList.add(item);}}}} catch (IOException e) {e.printStackTrace();} finally {if (inputStream != null) {try {inputStream.close();} catch (Exception e) {log.error("文件流關閉失敗:{}", e.getMessage());}}}return rowList;
}private static Boolean checkExists(List<CellItem> rowList, String key) {for (int i = 0; i < rowList.size(); i++) {CellItem item = rowList.get(i);if (item.getDeviceId().equals(key.trim())) {return Boolean.TRUE;}}return Boolean.FALSE;
}

DatUtil.readDat()

public static List<CellItem> readDat(MultipartFile file) {List<CellItem> list = new ArrayList<>();try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {String line;while ((line = reader.readLine()) != null) {String[] split = line.split(",");String deviceId = split[1];Boolean exists = checkExists(list, deviceId);if (exists) {continue;}CellItem item = new CellItem();item.setDeviceId(deviceId.trim());item.setMac(split[2]);item.setProductId(split[3]);list.add(item);}} catch (IOException e) {e.printStackTrace();}return list;
}private static Boolean checkExists(List<CellItem> rowList, String key) {for (int i = 0; i < rowList.size(); i++) {CellItem item = rowList.get(i);if (item.getDeviceId().equals(key.trim())) {return Boolean.TRUE;}}return Boolean.FALSE;
}

導出的代碼這里省略了。

2.3 配置

application.yml

# 開發環境配置
server:# 服務器的HTTP端口port: 8080servlet:# 應用的訪問路徑context-path: /# Spring配置
spring:profiles:active: druid#thymeleaf 頁面的緩存開關thymeleaf:enabled: truecache: truemode: HTML5encoding: utf-8suffix: .html# 文件上傳servlet:multipart:# 單個文件大小max-file-size: 10MB# 設置總上傳的文件大小max-request-size: 50MB# MyBatis配置
mybatis:# 搜索指定包別名typeAliasesPackage: com.xxx.domain# 配置mapper的掃描,找到所有的mapper.xml映射文件mapperLocations: classpath:mapper/*.xml# 加載全局的配置文件configLocation: classpath:mybatis/mybatis-config.xml# 日志配置
logging:level:com.xxx: infoorg.springframework: warn

數據庫配置application-druid.yml

# 數據源配置
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://172.16.1.2:3306/test?useUnicode=true&useSSL=false&allowLoadLocalInfile=false&autoReconnect=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8username: rootpassword: root#Spring Boot 默認是不注入這些屬性值的,需要自己綁定#druid 數據源專有配置initialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: true
2.3 前端處理邏輯

layui的相關文件放到resources/static目錄,再新建一個index.html文件放入resources/templates目錄,這兩個目錄是thymeleaf默認的,如果要修改可以在application.yml進行配置。靜態文件如下:
在這里插入圖片描述
為了壓縮jar包的體積,把所有不必要的文件都精簡掉了。

以下是index.html內容

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>測試</title><script th:src="@{/layui.js}"></script><link rel="stylesheet" th:href="@{/css/layui.css}" media="all">
</head>
<body><div class="layui-card"><div class="layui-card-header">操作面板</div><div class="layui-card-body"><div class="layui-tab" lay-filter="window"><ul class="layui-tab-title"><li class="layui-this" lay-id="uploadTab">文件上傳</li><li lay-id="downloadTab">文件下載</li></ul><div class="layui-tab-content"><div class="layui-tab-item layui-show"><form id="upload_form" class="layui-form" enctype="multipart/form-data"><div class="layui-form-item"><label class="layui-form-label">文件</label><div class="layui-input-block"><button type="button" class="layui-btn" id="upload"><i class="layui-icon">&#xe61f;</i>選擇文件</button></div></div><div class="layui-form-item"><div class="layui-input-block"><button id="btnSubmit" class="layui-btn" onclick="return false;">立即提交</button></div></div></form></div><div class="layui-tab-item"><div class="layui-form-item"><label class="layui-form-label">文件</label><div class="layui-input-block"><button type="button" class="layui-btn" id="downloadBtn"><i class="layui-icon">&#xe601;</i>下載文件</button></div></div></div></div></div></div></div>
</body>
</html>
<script>layui.use(['upload', 'layer', 'element'], function () {let $ = layui.jquery, layer = layui.layer, element = layui.element, upload = layui.upload;//執行實例upload.render({elem: '#upload' //綁定元素, url: '/upload' //上傳接口, accept: 'file' //允許上傳的文件類型,不寫默認是圖片, acceptMime: ".xlsx,.xls,.DAT,.dat" //不寫默認驗證圖片格式,一定要省略【exts】參數, auto: false //選擇文件后不自動上傳, bindAction: '#btnSubmit' //指向一個按鈕觸發上傳, before: function (obj) {layer.load(); //上傳loading},done: function (res) {console.log(res)layer.closeAll('loading'); //關閉loadinglayer.alert(res.msg);if (res.code === 200) {element.tabChange('window', 'downloadTab');}}, error: function (res) {console.error(res)layer.msg(res.msg);layer.closeAll('loading'); //關閉loading}});$("#downloadBtn").on('click', function () {location.href = "/download";})});
</script>

編輯好測試沒問題直接打包放到服務器上執行就可以啦。

三、實現效果
3.1 文件導入

在這里插入圖片描述
導入成功后會自動切換到【文件下載】的tab頁

3.2 文件導出

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/207475.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/207475.shtml
英文地址,請注明出處:http://en.pswp.cn/news/207475.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

編譯和使用WPS-ghrsst-to-intermediate生成SST

一、下載 V1.0 https://github.com/bbrashers/WPS-ghrsst-to-intermediate/tree/masterV1.5&#xff08;使用過程報錯&#xff0c;原因不詳&#xff0c;能正常使用的麻煩告知一下方法&#xff09; https://github.com/dmitryale/WPS-ghrsst-to-intermediate二、修改makefile…

【CVE 復現】CVE-2022-0185 fsconfig之整數溢出

影響版本&#xff1a;Linux-v5.1~v5.16.2 測試版本&#xff1a;Linux-5.11.22&#xff0c;由于懶得搞環境&#xff0c;所以直接用的 bsauce 大佬提供的 測試環境 看看 patch&#xff1a; diff --git a/fs/fs_context.c b/fs/fs_context.c index b7e43a780a625b..24ce12f0db32…

ResNeXt(2017)

文章目錄 Abstract1. Introductionformer workour work 2. Related Work多分支卷積網絡分組卷積壓縮卷積網絡Ensembling 3. Method3.1. Template3.2. Revisiting Simple Neurons3.3. Aggregated Transformations3.4. Model Capacity 4. Experiment 原文地址 源代碼 Abstract 我…

【python】vscode中選擇虛擬環境venv

vscode 怎么指定 python venv&#xff1f; 在VSCode中選擇Python解釋器&#xff1a; 打開命令面板&#xff1a;按下 CtrlShiftP&#xff08;Windows/Linux&#xff09;或 CmdShiftP&#xff08;Mac&#xff09;。在命令面板中&#xff0c;鍵入 “Python: Select Interpreter”…

14.Java程序設計-基于Springboot的高校社團管理系統設計與實現

摘要 隨著高校社團活動的不斷豐富和社團數量的逐漸增加&#xff0c;高校社團管理面臨著日益復雜的挑戰。為了提高社團管理的效率和透明度&#xff0c;本研究基于Spring Boot框架設計并實現了一套高校社團管理系統。該系統旨在整合社團創建、成員管理、活動發布等多個功能&…

水位線和窗口

水位線特點 插入到數據流中的一個標記&#xff0c;可以認為是一個特殊的數據主要內容是一個時間戳水位線是基于數據的時間戳生成的&#xff0c;即事件時間水位線必須單調遞增水位線可以通過設置延遲&#xff0c;來保證正確處理亂序數據一個水位線&#xff0c;表示事件時間已經…

[FPGA 學習記錄] 數碼管動態顯示

數碼管動態顯示 文章目錄 1 理論學習1.1 數碼管動態掃描顯示原理 2 實戰演練2.1 實驗目標2.2 程序設計2.2.1 框圖繪制2.2.2 數據生成模塊 data_gen2.2.2.1 波形繪制2.2.2.2 代碼編寫2.2.2.3 代碼編譯2.2.2.4 邏輯仿真2.2.2.4.1 仿真代碼編寫2.2.2.4.2 仿真代碼編譯2.2.2.4.3 波…

如何解決el-table中動態添加固定列時出現的行錯位

問題描述 在使用el-table組件時&#xff0c;我們有時需要根據用戶的操作動態地添加或刪除一些固定列&#xff0c;例如操作列或選擇列。但是&#xff0c;當我們使用v-if指令來控制固定列的顯示或隱藏時&#xff0c;可能會出現表格的行錯位的問題&#xff0c;即固定列和非固定列…

el-tree數據量過大,造成瀏覽器卡死、崩潰

el-tree數據量過大&#xff0c;造成瀏覽器卡死、崩潰 場景&#xff1a;樹形結構展示&#xff0c;數據超級多&#xff0c;超過萬條&#xff0c;每次打開都會崩潰 我這里采用的是引入新的插件虛擬樹&#xff0c;它是參照element-plus 中TreeV2改造vue2.x版本虛擬化樹形控件&…

2024年強烈推薦mac 讀寫NTFS工具Tuxera NTFS for Mac2023中文破解版

大家好啊&#xff5e;今天要給大家推薦的是 Tuxera NTFS for Mac2023中文破解版&#xff01; 小可愛們肯定知道&#xff0c;Mac系統一直以來都有一個小小的痛點&#xff0c;就是無法直接讀寫NTFS格式的移動硬盤和U盤。但是&#xff0c;有了Tuxera NTFS for Mac2023&#xff0c;…

正則表達式:字符串處理的瑞士軍刀

&#x1f90d; 前端開發工程師&#xff08;主業&#xff09;、技術博主&#xff08;副業&#xff09;、已過CET6 &#x1f368; 阿珊和她的貓_CSDN個人主頁 &#x1f560; 牛客高級專題作者、在牛客打造高質量專欄《前端面試必備》 &#x1f35a; 藍橋云課簽約作者、已在藍橋云…

記一次xss通殺挖掘歷程

前言 前端時間&#xff0c;要開放一個端口&#xff0c;讓我進行一次安全檢測&#xff0c;發現的一個漏洞。 經過 訪問之后發現是類似一個目錄索引的端口。(這里上厚碼了哈) 錯誤案例測試 亂輸內容asdasffda之后看了一眼Burp的抓包&#xff0c;抓到的內容是可以發現這是一個…

MuJoCo機器人動力學仿真平臺安裝與教程

MuJoCo是一個機器人動力學仿真平臺&#xff0c;它包括一系列的物理引擎、可視化工具和機器人模擬器等工具&#xff0c;用于研究和模擬機器人的運動和動力學特性。以下是MuJoCo的安裝教程&#xff1a; 下載和安裝MuJoCo Pro。可以從MuJoCo的官方網站上下載最新版本的安裝包。根…

【Python機器學習系列】一文徹底搞懂機器學習中表格數據的輸入形式(理論+源碼)

一、問題 機器學習或者深度學習在處理表格數據&#xff08;Tabular data&#xff09;、圖像數據&#xff08;Image data&#xff09;、文本數據&#xff08;Text data&#xff09;、時間序列數據&#xff08;Time series data&#xff09;上得到了廣泛的應用。 其中&#xff0c…

微信小程序 - 創建 ZIP 壓縮包

微信小程序 - 創建 ZIP 壓縮包 場景分享代碼片段導入 JSZip創建ZIP文件追加寫入文件測試方法參考資料 場景 微信小程序只提供了解壓ZIP的API&#xff0c;并沒有提供創建ZIP的方法。 當我們想把自己處理好的保存&#xff0c;打包ZIP保存下來時就需要自己實現了。 分享代碼片段…

無重復字符的最長子串(LeetCode 3)

文章目錄 1.問題描述2.難度等級3.熱門指數4.解題思路方法一&#xff1a;暴力法方法二&#xff1a;滑動窗口 參考文獻 1.問題描述 給定一個字符串 s &#xff0c;請你找出其中不含有重復字符的最長子串的長度。 s 由英文字母、數字、符號和空格組成。 示例 1&#xff1a; 輸…

基于Java商品銷售管理系統

基于Java商品銷售管理系統 功能需求 1、商品管理&#xff1a;系統需要提供商品信息的管理功能&#xff0c;包括商品的錄入、編輯、查詢和刪除。每個商品應包含基本信息如名稱、編碼、類別、價格、庫存量等。 2、客戶管理&#xff1a;系統需要能夠記錄客戶的基本信息&#xf…

算法:常見的哈希表算法

文章目錄 兩數之和判斷是否互為字符重排存在重復元素存在重復元素字母異位詞分組 本文總結的是關于哈希表常見的算法 哈希表其實就是一個存儲數據的容器&#xff0c;所以其實它本身的算法難度并不高&#xff0c;只是利用哈希表可以對于一些場景進行優化 兩數之和 class Solut…

Michael.W基于Foundry精讀Openzeppelin第41期——ERC20Capped.sol

Michael.W基于Foundry精讀Openzeppelin第41期——ERC20Capped.sol 0. 版本0.1 ERC20Capped.sol 1. 目標合約2. 代碼精讀2.1 constructor() && cap()2.2 _mint(address account, uint256 amount) 0. 版本 [openzeppelin]&#xff1a;v4.8.3&#xff0c;[forge-std]&…

AI智能降重軟件大全,免費最新AI智能降重軟件

在當今信息爆炸的時代&#xff0c;內容創作者們面臨著巨大的寫作壓力&#xff0c;如何在保持高質量的前提下提高效率成為擺在許多人面前的難題。AI智能降重軟件因其獨特的算法和功能逐漸成為提升文案質量的得力助手。本文將專心分享一些優秀的AI智能降重軟件。 147SEO改寫軟件 …