**RuoYi(若依)**是一个基于Spring Boot和Spring Cloud的企业级快速开发平台。它集成了多种常用的技术栈和中间件,旨在帮助企业快速构建稳定、高效的应用系统。以下是关于RuoYi框架的详细介绍和基本使用教程,涵盖了从环境搭建到核心功能的使用。
RuoYi 框架简介 1. 技术栈后端技术:Spring Boot, MyBatis, Druid, Redis, Shiro/Spring Security, Quartz等
前端技术:Vue.js, Element UI, Axios, Webpack等
数据库:MySQL, Oracle, SQL Server等
中间件:Nginx, RabbitMQ, Elasticsearch等
2. 核心功能权限管理:用户、角色、菜单、权限的管理
系统管理:部门、岗位、字典、参数、通知公告等
工具管理:代码生成器、在线表单、定时任务等
监控管理:服务监控、操作日志、登录日志、在线用户等
多租户支持:支持多租户模式,便于企业级应用的扩展
环境搭建 1. 开发环境准备Java:安装JDK 8或更高版本
IDE:推荐使用IntelliJ IDEA或Eclipse
数据库:安装MySQL 5.7或更高版本
Node.js:用于前端开发,安装Node.js 12或更高版本
Git:用于版本控制
2. 下载RuoYi源码
访问RuoYi的GitHub仓库:https://github.com/y_project/RuoYi
克隆项目到本地
git clone https://github.com/y_project/RuoYi.git 3. 配置数据库
创建数据库:
CREATE DATABASE ruoyi DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
导入初始化SQL脚本:
进入ruoyi/sql目录,找到ruoyi.sql文件
执行SQL脚本:
mysql -u root -p ruoyi < ruoyi.sql 4. 配置项目
修改ruoyi-common/src/main/resources/ruoyi.properties文件,配置数据库连接信息:properties深色版本
spring.datasource.url=jdbc:mysql://localhost:3306/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=your_password
配置Redis(可选):properties深色版本
spring.redis.host=localhost spring.redis.port=6379 5. 构建和运行项目
后端:
打开IDE,导入项目
运行ruoyi-admin模块中的RuoYiApplication启动类
前端:
进入ruoyi-ui目录
安装依赖:
npm install
启动前端项目:
npm run dev 基本功能使用 1. 登录打开浏览器,访问:8080
使用默认账号admin,密码admin123登录
2. 权限管理用户管理:添加、编辑、删除用户
角色管理:创建、编辑、删除角色,分配权限
菜单管理:创建、编辑、删除菜单,分配给角色
权限管理:管理具体的权限点
3. 系统管理部门管理:创建、编辑、删除部门
岗位管理:创建、编辑、删除岗位
字典管理:管理系统的字典项
参数管理:管理系统的参数配置
通知公告:发布、编辑、删除通知公告
4. 工具管理代码生成器:根据数据库表生成CRUD代码
在线表单:创建和管理表单
定时任务:创建、编辑、删除定时任务
5. 监控管理服务监控:监控系统的健康状况
操作日志:查看用户的操作记录
登录日志:查看用户的登录记录
在线用户:查看当前在线的用户
扩展功能 1. 多租户支持在ruoyi-system模块中,修改SysTenantService类,实现多租户逻辑
在数据库中添加租户表,管理租户信息
在前端页面中,增加租户切换功能
2. 接口文档
使用Swagger生成API文档
在ruoyi-admin模块中,添加Swagger依赖:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
配置Swagger:
@Configuration @EnableSwagger2WebMvc public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.ruoyi")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("RuoYi API 文档") .description("RuoYi API 文档") .contact(new Contact("RuoYi", "https://github.com/y_project/RuoYi", "y_project@163.com")) .version("1.0") .build(); } }从项目的整体结构、核心模块、配置文件、主要功能等方面进行深入探讨。
项目结构RuoYi项目采用微服务架构设计,主要分为以下几个模块:
ruoyi-common:公共模块,包含一些常用的工具类和配置。
ruoyi-auth:认证授权模块,负责用户的登录、权限校验等。
ruoyi-system:系统管理模块,包括用户、角色、菜单、部门等管理功能。
ruoyi-gen:代码生成模块,用于根据数据库表生成CRUD代码。
ruoyi-job:定时任务模块,管理定时任务的创建、编辑和执行。
ruoyi-monitor:监控模块,包括服务监控、操作日志、登录日志等。
ruoyi-tool:工具模块,提供一些辅助工具,如在线表单等。
ruoyi-admin:后端主项目,整合了上述所有模块。
ruoyi-ui:前端项目,基于Vue.js和Element UI构建的前端界面。
核心模块详解 1. ruoyi-common功能:提供一些常用的工具类和配置,如日期工具、字符串工具、常量定义等。
主要文件:
config:配置文件,如数据库配置、Redis配置等。
core:核心工具类,如日期工具、字符串工具等。
exception:异常处理类。
utils:各种实用工具类。
2. ruoyi-auth功能:负责用户的认证和授权,使用Spring Security或Shiro实现。
主要文件:
config:安全配置类,如SecurityConfig。
controller:认证控制器,如LoginController。
domain:认证相关的实体类,如UserDetailsServiceImpl。
service:认证服务,如UserService。
3. ruoyi-system功能:系统管理模块,包括用户、角色、菜单、部门等管理功能。
主要文件:
controller:系统管理相关的控制器,如UserController、RoleController等。
domain:系统管理相关的实体类,如SysUser、SysRole等。
mapper:MyBatis映射文件,如SysUserMapper.xml。
service:系统管理相关的服务类,如SysUserService、SysRoleService等。
vo:视图对象,用于封装返回给前端的数据。
4. ruoyi-gen功能:代码生成模块,根据数据库表生成CRUD代码。
主要文件:
controller:代码生成相关的控制器,如GenController。
domain:代码生成相关的实体类,如GenTable。
mapper:MyBatis映射文件,如GenTableMapper.xml。
service:代码生成相关的服务类,如GenTableService。
util:代码生成工具类,如VelocityInitializer。
5. ruoyi-job功能:定时任务管理模块,使用Quartz实现。
主要文件:
controller:定时任务相关的控制器,如JobController。
domain:定时任务相关的实体类,如SysJob。
mapper:MyBatis映射文件,如SysJobMapper.xml。
service:定时任务相关的服务类,如SysJobService。
util:定时任务工具类,如ScheduleUtils。
6. ruoyi-monitor功能:监控模块,包括服务监控、操作日志、登录日志等。
主要文件:
controller:监控相关的控制器,如ServerController、LogController。
domain:监控相关的实体类,如SysLogininfor、SysOperLog。
mapper:MyBatis映射文件,如SysLogininforMapper.xml。
service:监控相关的服务类,如SysLogininforService、SysOperLogService。
7. ruoyi-tool功能:工具模块,提供一些辅助工具,如在线表单等。
主要文件:
controller:工具相关的控制器,如GenFormController。
domain:工具相关的实体类,如GenForm。
mapper:MyBatis映射文件,如GenFormMapper.xml。
service:工具相关的服务类,如GenFormService。
8. ruoyi-admin功能:后端主项目,整合了上述所有模块。
主要文件:
application.yml:主配置文件,包含各个模块的配置。
RuoYiApplication.java:主启动类。
config:全局配置类,如WebMvcConfig、DruidConfig。
controller:主控制器,如IndexController。
filter:过滤器,如CorsFilter。
interceptor:拦截器,如AuthTokenInterceptor。
service:主服务类,如IndexService。
9. ruoyi-ui功能:前端项目,基于Vue.js和Element UI构建的前端界面。
主要文件:
src
:前端源代码目录。
api:API接口文件,如user.js。
assets:静态资源文件,如图片、样式表。
components:自定义组件。
router:路由配置文件,如index.js。
store:状态管理文件,如index.js。
views:页面组件,如dashboard.vue。
App.vue:主应用组件。
main.js:入口文件。
配置文件详解 1. application.yml功能:主配置文件,包含各个模块的配置。
示例:
server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root password: your_password driver-class-name: com.mysql.cj.jdbc.Driver redis: host: localhost port: 6379 thymeleaf: cache: false mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl mapper-locations: classpath:mapper/**/*.xml druid: initial-size: 5 min-idle: 5 max-active: 20 test-on-borrow: false test-on-return: false test-while-idle: true time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 validation-query: SELECT 1 FROM DUAL filters: stat,wall,log4j connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 logging: level: com.ruoyi: info file: name: logs/ruoyi.log swagger: enabled: true title: RuoYi API 文档 description: RuoYi API 文档 version: 1.0 contact: name: RuoYi url: https://github.com/y_project/RuoYi email: y_project@163.com 主要功能详解 1. 登录功能:用户登录系统,验证用户名和密码。
实现:
前端:在ruoyi-ui/src/views/login/index.vue中,使用Axios发送登录请求。
import axios from 'axios'; methods: { handleLogin() { const { username, password } = this.loginForm; axios.post('/login', { username, password }).then(response => { if (response.data.code === 200) { this.$router.push('/dashboard'); } else { this.$message.error(response.data.msg); } }); } }
后端:在ruoyi-auth/controller/LoginController.java中,处理登录请求。
@RestController @RequestMapping("/auth") public class LoginController { @Autowired private AuthenticationManager authenticationManager; @PostMapping("/login") public AjaxResult login(@RequestBody LoginBody loginBody) { try { UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginBody.getUsername(), loginBody.getPassword()); Authentication authentication = authenticationManager.authenticate(authenticationToken); if (authentication.isAuthenticated()) { return AjaxResult.success("登录成功"); } else { return AjaxResult.error("用户名或密码错误"); } } catch (Exception e) { return AjaxResult.error("登录失败"); } } } 2. 用户管理功能:添加、编辑、删除用户。
实现:
前端:在ruoyi-ui/src/views/system/user/index.vue中,使用Axios发送请求。
import axios from 'axios'; methods: { addUser() { axios.post('/system/user', this.userForm).then(response => { if (response.data.code === 200) { this.$message.success("添加成功"); } else { this.$message.error(response.data.msg); } }); }, editUser() { axios.put('/system/user', this.userForm).then(response => { if (response.data.code === 200) { this.$message.success("编辑成功"); } else { this.$message.error(response.data.msg); } }); }, deleteUser(id) { axios.delete(`/system/user/${id}`).then(response => { if (response.data.code === 200) { this.$message.success("删除成功"); } else { this.$message.error(response.data.msg); } }); } }
后端:在ruoyi-system/controller/UserController.java中,处理用户管理请求。
@RestController @RequestMapping("/system/user") public class UserController { @Autowired private SysUserService userService; @PostMapping public AjaxResult addUser(@RequestBody SysUser user) { return toAjax(userService.insertUser(user)); } @PutMapping public AjaxResult editUser(@RequestBody SysUser user) { return toAjax(userService.updateUser(user)); } @DeleteMapping("/{userId}") public AjaxResult deleteUser(@PathVariable Long userId) { return toAjax(userService.deleteUserById(userId)); } } 3. 角色管理功能:添加、编辑、删除角色,分配权限。
实现:
前端:在ruoyi-ui/src/views/system/role/index.vue中,使用Axios发送请求。
import axios from 'axios'; methods: { addRole() { axios.post('/system/role', this.roleForm).then(response => { if (response.data.code === 200) { this.$message.success("添加成功"); } else { this.$message.error(response.data.msg); } }); }, editRole() { axios.put('/system/role', this.roleForm).then(response => { if (response.data.code === 200) { this.$message.success("编辑成功"); } else { this.$message.error(response.data.msg); } }); }, deleteRole(id) { axios.delete(`/system/role/${id}`).then(response => { if (response.data.code === 200) { this.$message.success("删除成功"); } else { this.$message.error(response.data.msg); } }); } }
后端:在ruoyi-system/controller/RoleController.java中,处理角色管理请求。
@RestController @RequestMapping("/system/role") public class RoleController { @Autowired private SysRoleService roleService; @PostMapping public AjaxResult addRole(@RequestBody SysRole role) { return toAjax(roleService.insertRole(role)); } @PutMapping public AjaxResult editRole(@RequestBody SysRole role) { return toAjax(roleService.updateRole(role)); } @DeleteMapping("/{roleId}") public AjaxResult deleteRole(@PathVariable Long roleId) { return toAjax(roleService.deleteRoleById(roleId)); } } 扩展功能 1. 多租户支持功能:支持多租户模式,每个租户有独立的数据空间。
实现:
数据库:在数据库中添加租户表,如sys_tenant。
CREATE TABLE sys_tenant ( tenant_id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '租户ID', tenant_name VARCHAR(50) NOT NULL COMMENT '租户名称', status CHAR(1) NOT NULL COMMENT '状态(0正常 1停用)', create_by VARCHAR(64) DEFAULT '' COMMENT '创建者', create_time DATETIME DEFAULT NULL COMMENT '创建时间', update_by VARCHAR(64) DEFAULT '' COMMENT '更新者', update_time DATETIME DEFAULT NULL COMMENT '更新时间', remark VARCHAR(500) DEFAULT NULL COMMENT '备注', PRIMARY KEY (tenant_id) ) ENGINE=InnoDB AUTO_INCREMENT=100 COMMENT='租户表';
后端:在ruoyi-system/service/ISysTenantService.java中,定义租户服务接口。
public interface ISysTenantService { List<SysTenant> selectTenantList(SysTenant tenant); int insertTenant(SysTenant tenant); int updateTenant(SysTenant tenant); int deleteTenantById(Long tenantId); }
前端:在ruoyi-ui/src/views/system/tenant/index.vue中,添加租户管理页面。
<template> <div> <!-- 租户管理页面内容 --> </div> </template> <script> import axios from 'axios'; export default { data() { return { tenantList: [] }; }, methods: { getTenantList() { axios.get('/system/tenant').then(response => { this.tenantList = response.data.rows; }); }, addTenant() { // 添加租户逻辑 }, editTenant() { // 编辑租户逻辑 }, deleteTenant(id) { axios.delete(`/system/tenant/${id}`).then(response => { if (response.data.code === 200) { this.$message.success("删除成功"); } else { this.$message.error(response.data.msg); } }); } }, mounted() { this.getTenantList(); } }; </script> 总结RuoYi框架是一个非常强大的企业级快速开发平台,它集成了众多主流技术,可以帮助开发者快速搭建稳定、高效的应用系统。