1. 项目概述:企业级前后端分离架构实战
这套基于SpringBoot+Vue3+MyBatis的企业内管系统源码,是典型的现代化全栈开发解决方案。我在金融行业IT部门实施类似系统时,发现这种架构组合能完美平衡开发效率与系统性能。前端Vue3的Composition API让复杂表单逻辑变得清晰,后端SpringBoot的自动配置机制大幅减少了XML配置量,而MyBatis的灵活SQL映射则解决了企业级查询的多样化需求。
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. 技术栈深度解析
2.1 SpringBoot后端设计精要
采用2.7.x版本稳定分支,通过spring-boot-starter-web快速构建RESTful接口。特别要注意的是@Transactional注解的事务传播行为配置,在部门关联数据操作时建议使用PROPAGATION_REQUIRES_NEW。数据库连接池默认使用HikariCP,在application.yml中需要优化以下参数:
yaml复制spring:
datasource:
hikari:
maximum-pool-size: 20
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1800000
2.2 Vue3前端工程化实践
推荐使用Vite作为构建工具,比传统webpack快10倍以上。组合式API的典型应用场景:
javascript复制// 部门树形组件逻辑封装
const useDepartmentTree = () => {
const treeData = ref([])
const loadData = async () => {
const { data } = await axios.get('/api/departments')
treeData.value = transformToTree(data)
}
return { treeData, loadData }
}
2.3 MyBatis高级映射技巧
处理多表关联查询时,推荐使用resultMap嵌套collection:
xml复制<resultMap id="deptWithUsersMap" type="Department">
<id property="id" column="dept_id"/>
<collection property="users" ofType="User">
<id property="userId" column="user_id"/>
<result property="userName" column="user_name"/>
</collection>
</resultMap>
3. 数据库设计与优化
3.1 MySQL表结构设计
核心表字段设计示例:
sql复制CREATE TABLE `sys_user` (
`user_id` BIGINT NOT NULL AUTO_INCREMENT,
`dept_id` BIGINT COMMENT '所属部门',
`user_name` VARCHAR(50) NOT NULL,
`password` VARCHAR(100) NOT NULL COMMENT 'BCrypt加密存储',
`status` TINYINT DEFAULT 1 COMMENT '0-禁用 1-正常',
PRIMARY KEY (`user_id`),
INDEX `idx_dept` (`dept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
3.2 性能优化要点
- 查询优化:为所有外键字段建立索引
- 慢SQL监控:开启MySQL慢查询日志
- 连接池配置:根据服务器CPU核心数设置合理连接数
4. 前后端交互规范
4.1 接口协议设计
采用RESTful风格,响应体统一格式:
json复制{
"code": 200,
"message": "success",
"data": {...},
"timestamp": 1630000000000
}
4.2 安全控制方案
-
JWT认证流程:
- 登录接口返回access_token和refresh_token
- access_token过期时间设为2小时
- refresh_token过期时间设为7天
-
接口权限控制:
java复制@PreAuthorize("hasRole('ADMIN') or hasPermission('user:delete')")
@DeleteMapping("/users/{id}")
public Result deleteUser(@PathVariable Long id) {
//...
}
5. 典型业务模块实现
5.1 部门管理树形组件
前端采用Element Plus的Tree组件,后端使用递归查询:
java复制public List<DepartmentVO> getDepartmentTree() {
List<Department> allDepts = departmentMapper.selectAll();
return buildTree(allDepts, 0L); // 从根节点0开始构建
}
private List<DepartmentVO> buildTree(List<Department> list, Long parentId) {
return list.stream()
.filter(d -> d.getParentId().equals(parentId))
.map(d -> {
DepartmentVO vo = convertToVO(d);
vo.setChildren(buildTree(list, d.getId()));
return vo;
}).collect(Collectors.toList());
}
5.2 用户导入导出
使用EasyExcel处理大数据量:
java复制@PostMapping("/users/import")
public Result importUsers(@RequestParam MultipartFile file) {
List<UserImportDTO> list = EasyExcel.read(file.getInputStream())
.head(UserImportDTO.class)
.sheet()
.doReadSync();
// 批量处理逻辑...
}
6. 部署与运维方案
6.1 生产环境部署
推荐使用Docker Compose编排:
yaml复制version: '3'
services:
mysql:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: root123
volumes:
- ./mysql/data:/var/lib/mysql
backend:
build: ./backend
ports:
- "8080:8080"
depends_on:
- mysql
6.2 监控配置
SpringBoot Actuator关键端点配置:
properties复制management.endpoints.web.exposure.include=health,info,metrics
management.endpoint.health.show-details=always
management.metrics.tags.application=${spring.application.name}
7. 常见问题排查指南
- 跨域问题解决方案:
java复制@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*")
.maxAge(3600);
}
}
- MyBatis批量插入优化:
java复制@Transactional
public void batchInsert(List<User> users) {
SqlSession session = sqlSessionTemplate.getSqlSessionFactory()
.openSession(ExecutorType.BATCH);
try {
UserMapper mapper = session.getMapper(UserMapper.class);
users.forEach(mapper::insert);
session.commit();
} finally {
session.close();
}
}
8. 项目扩展方向建议
- 工作流引擎集成:可接入Activiti或Flowable实现审批流
- 分布式会话方案:采用Spring Session + Redis实现集群部署
- 接口文档生成:集成Knife4j增强Swagger UI展示
- 日志收集分析:通过ELK栈实现集中式日志管理
这套架构在实际项目中表现稳定,特别是在处理企业复杂的权限体系时,建议采用RBAC模型与数据权限结合的方式。我在最近的项目中,通过给SQL语句自动注入数据权限过滤条件,完美解决了多部门数据隔离的需求。具体实现可以参考MyBatis插件的开发方式,在StatementHandler层面进行SQL重写。
