ARTICLE · INTELLIGENCE

战地情报 · 详情页

来自尧图项目组的一线实战观察与深度解析

SpringBoot+Vue3+MyBatis企业级全栈开发实战

SpringBoot+Vue3+MyBatis企业级全栈开发实战

1. 项目概述:企业级全栈技术栈实战

这套基于SpringBoot+Vue3+MyBatis的企业信息管理系统,采用了当前主流的前后端分离架构。作为多年全栈开发的老兵,我认为这套技术组合在2023年依然是企业级应用开发的黄金配置。SpringBoot 3.x提供了稳定的后端服务能力,Vue3的Composition API让前端开发更高效,而MyBatis-Plus的加持则让数据库操作变得异常简单。

系统采用经典的MVC分层设计:

  • 前端:Vue3 + Element Plus + Axios
  • 后端:SpringBoot 3.x + MyBatis-Plus 3.5.x
  • 数据库:MySQL 8.0(兼容5.7版本)
  • 构建工具:Maven + npm
  • 接口规范:RESTful API + JWT认证

提示:这套架构特别适合需要快速迭代的中小型企业应用,从开发到部署的全套技术栈都经过生产环境验证。

2. 核心技术栈深度解析

2.1 SpringBoot后端设计精要

后端采用多模块Maven项目结构:

enterprise-system ├── system-common // 公共模块 ├── system-admin // 管理后台API ├── system-service // 业务服务层 └── system-mapper // 数据持久层

关键配置示例(application.yml):

spring: datasource: url: jdbc:mysql://localhost:3306/enterprise_db?useSSL=false&serverTimezone=Asia/Shanghai username: root password: 加密密码 redis: host: 127.0.0.1 port: 6379 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: logic-delete-field: deleted # 逻辑删除字段 logic-not-delete-value: 0 logic-delete-value: 1

2.2 Vue3前端架构设计

前端采用Vite构建工具,项目结构清晰:

src/ ├── api/ // 接口定义 ├── assets/ // 静态资源 ├── components/ // 公共组件 ├── composables/ // 组合式函数 ├── router/ // 路由配置 ├── stores/ // Pinia状态管理 └── views/ // 页面组件

典型页面组件示例(UserManagement.vue):

<script setup> import { ref, onMounted } from 'vue' import { getUserList } from '@/api/user' const tableData = ref([]) const loading = ref(false) const fetchData = async () => { loading.value = true try { const res = await getUserList() tableData.value = res.data } finally { loading.value = false } } onMounted(() => { fetchData() }) </script>

3. 数据库设计与MyBatis优化

3.1 MySQL表结构设计

核心表结构示例(用户管理模块):

CREATE TABLE `sys_user` ( `user_id` bigint NOT NULL AUTO_INCREMENT COMMENT '用户ID', `dept_id` bigint DEFAULT NULL COMMENT '部门ID', `username` varchar(30) NOT NULL COMMENT '用户账号', `password` varchar(100) NOT NULL COMMENT '密码', `nick_name` varchar(30) DEFAULT NULL COMMENT '用户昵称', `email` varchar(50) DEFAULT NULL COMMENT '用户邮箱', `phone` varchar(11) DEFAULT NULL COMMENT '手机号码', `status` char(1) DEFAULT '0' COMMENT '帐号状态(0正常 1停用)', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_time` datetime DEFAULT NULL COMMENT '更新时间', `deleted` char(1) DEFAULT '0' COMMENT '删除标志(0代表存在 1代表删除)', PRIMARY KEY (`user_id`), UNIQUE KEY `idx_username` (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户信息表';

3.2 MyBatis-Plus高级应用

动态SQL示例(部门查询):

@Mapper public interface DeptMapper extends BaseMapper<Dept> { @Select("<script>" + "SELECT * FROM sys_dept " + "<where>" + " <if test='deptName != null'> AND dept_name like CONCAT('%',#{deptName},'%') </if>" + " <if test='status != null'> AND status = #{status} </if>" + "</where>" + "ORDER BY order_num ASC" + "</script>") List<Dept> selectDeptList(@Param("deptName") String deptName, @Param("status") String status); }

4. 前后端交互关键实现

4.1 接口安全设计

JWT认证流程实现:

  1. 用户登录成功后生成Token
public String createToken(LoginUser loginUser) { return Jwts.builder() .setSubject(loginUser.getUsername()) .claim("user_id", loginUser.getUserId()) .setExpiration(new Date(System.currentTimeMillis() + expireTime)) .signWith(SignatureAlgorithm.HS512, secret) .compact(); }
  1. 前端Axios请求拦截器配置
service.interceptors.request.use(config => { const token = getToken() if (token) { config.headers['Authorization'] = 'Bearer ' + token } return config }, error => { return Promise.reject(error) })

4.2 文件上传下载方案

后端文件处理控制器:

@PostMapping("/upload") public R<String> uploadFile(@RequestParam("file") MultipartFile file) { try { String fileName = FileUploadUtils.upload(file); return R.ok(fileName); } catch (Exception e) { return R.fail("上传失败:" + e.getMessage()); } } @GetMapping("/download") public void downloadFile(String fileName, HttpServletResponse response) { try { FileUploadUtils.download(fileName, response); } catch (IOException e) { log.error("下载文件失败", e); } }

5. 系统部署与性能优化

5.1 多环境部署方案

使用Spring Profile实现环境隔离:

# application-dev.properties server.port=8080 spring.profiles.active=dev # application-prod.properties server.port=80 spring.profiles.active=prod

前端环境变量配置(.env.production):

VITE_API_BASE_URL=https://api.yourdomain.com VITE_APP_TITLE=企业信息管理系统

5.2 性能优化实战技巧

  1. 数据库连接池配置优化:
spring: datasource: hikari: maximum-pool-size: 20 minimum-idle: 5 idle-timeout: 30000 max-lifetime: 1800000 connection-timeout: 30000
  1. Vue3组件懒加载:
const UserManagement = defineAsyncComponent(() => import('./views/system/UserManagement.vue') )
  1. MyBatis二级缓存配置:
<cache eviction="LRU" flushInterval="60000" size="512" readOnly="true"/>

6. 常见问题排查手册

6.1 跨域问题解决方案

SpringBoot全局跨域配置:

@Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOriginPatterns("*") .allowedMethods("GET", "POST", "PUT", "DELETE") .allowCredentials(true) .maxAge(3600); } }

6.2 数据权限控制实现

基于注解的数据权限过滤:

@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface DataScope { String deptAlias() default ""; String userAlias() default ""; } // AOP实现 @Around("@annotation(dataScope)") public Object around(ProceedingJoinPoint point, DataScope dataScope) throws Throwable { // 数据权限过滤逻辑 dataFilter(filterSQL); return point.proceed(); }

7. 项目扩展与二次开发建议

  1. 工作流引擎集成:可接入Activiti或Flowable实现审批流程
  2. 消息通知增强:集成WebSocket实现实时消息推送
  3. 微服务改造:使用SpringCloud Alibaba拆分模块
  4. 移动端适配:开发uniapp版本实现多端统一
  5. 监控体系搭建:集成Prometheus+Grafana监控系统健康状态

这套系统在实际部署时,我建议先从小规模部门试用开始,逐步完善权限体系和业务流程。对于初期开发,可以优先实现核心的用户-角色-权限体系和基础数据管理功能,后续再根据实际需求扩展OA、CRM等模块。

RELATED READING

延伸阅读

更多一线实战笔记与深度复盘,助您持续精进