markdown复制## 1. 项目概述与核心价值
刚接手这个Spring Boot财务管理系统时,我意识到这不仅是学生毕设的典型选题,更是能真实解决个人和小微企业记账痛点的实用工具。市面上现成的记账软件要么功能冗余,要么缺乏定制性,而这个项目正好填补了轻量级自主管理系统的空白。
系统采用经典的三层架构设计,前端用Thymeleaf模板引擎实现动态页面,后端基于Spring Boot 2.7快速搭建,数据库选用MySQL 8.0。特别在交易流水处理模块,通过JPA动态查询实现了多条件组合筛选,这是很多商业软件都做得不够灵活的地方。整个开发周期约3周,但核心记账功能在第一周就已跑通。
## 2. 技术架构解析
### 2.1 Spring Boot的选型考量
选择Spring Boot而非传统SSM框架,主要基于三点考虑:
1. 内嵌Tomcat简化部署,学生用一台笔记本就能完成开发测试全流程
2. Starter依赖自动配置,避免XML配置的繁琐(特别是对于不熟悉Spring生态的初学者)
3. Actuator端点监控,方便后期性能调优
实际开发中,这几个依赖包最关键:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
财务系统的核心是流水表设计,我的方案包含这些关键字段:
sql复制CREATE TABLE `transaction` (
`id` bigint NOT NULL AUTO_INCREMENT,
`amount` decimal(12,2) NOT NULL COMMENT '支持百万级金额',
`type` enum('INCOME','EXPENSE') COLLATE utf8mb4_bin NOT NULL,
`category_id` int DEFAULT NULL COMMENT '关联分类表',
`transaction_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`remark` varchar(200) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
特别注意:金额字段必须用decimal而非float,避免浮点运算精度问题。曾有个项目因用错类型导致月末对账差0.01元,排查了整整两天。
通过JPA Specification实现动态查询是项目的技术亮点:
java复制public class TransactionSpecs {
public static Specification<Transaction> betweenDate(LocalDate start, LocalDate end) {
return (root, query, cb) ->
cb.between(root.get("transactionTime"),
start.atStartOfDay(),
end.plusDays(1).atStartOfDay());
}
public static Specification<Transaction> byCategory(Integer categoryId) {
return (root, query, cb) ->
cb.equal(root.get("category").get("id"), categoryId);
}
}
// 使用示例
Specification<Transaction> spec = where(betweenDate(startDate, endDate))
.and(byCategory(categoryId));
放弃复杂的前端图表库,采用服务端渲染的简约方案:
html复制<div th:each="cat : ${categories}" class="progress-bar"
th:style="'width:' + ${cat.percentage} + '%;' +
'background-color:' + ${cat.color} + ';'">
</div>
必须配置spring-boot-maven-plugin确保生成可执行jar:
xml复制<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
在application-prod.properties中务必设置:
properties复制# 避免内存泄漏
server.tomcat.max-threads=200
server.tomcat.min-spare-threads=20
# 启用Gzip压缩
server.compression.enabled=true
server.compression.mime-types=text/html,text/css,application/javascript
MySQL 8.0默认使用系统时区,会导致时间显示错乱。解决方案:
properties复制spring.datasource.url=jdbc:mysql://localhost:3306/finance?serverTimezone=Asia/Shanghai
bash复制timedatectl set-timezone Asia/Shanghai
多人同时操作时可能出现乐观锁异常。我的处理方案:
java复制@Version
private Integer version;
这个基础框架还可以深化:
我在实现导出Excel功能时,发现Apache POI在大数据量时内存消耗严重,后来改用Alibaba EasyExcel解决了性能问题。具体实现可以参考我的GitHub仓库中的ExportController类。
整个项目最耗时的其实是分类体系的设计,需要平衡灵活性和易用性。最终采用三级分类(大类-中类-小类)加标签系统,既满足详细统计需求,又不会让用户选择困难。```