1. 核心技术
基础框架:SpringBoot |
---|
数据缓存:Redis |
数据库:MySQL |
权限控制:SpringSecurity |
工作流引擎:Activiti |
前端技术:vue-admin-template + Node.js + Npm + Vue + ElementUI + Axios |
微信公众号:公众号菜单 + 微信授权登录 + 消息推送 |
2. 项目模块
- YunshangOffice:根模块,管理子模块
- common:公共类父模块
- common-util:核心工具类
- service-util:service模块工具类
- spring-security:spring-security业务模块
- model:实体类模块
- service-oa:系统服务模块
- common:公共类父模块
3. 环境搭建
3.1 搭建项目结构
3.2 引入依赖
3.2.1 YunshangOffice模块依赖
<!--注意springboot工程一定要引入这个依赖,否则会出错-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
</parent>
<properties>
<java.version>1.8</java.version>
<mybatis-plus.version>3.4.1</mybatis-plus.version>
<mysql.version>8.0.30</mysql.version>
<knife4j.version>3.0.3</knife4j.version>
<jwt.version>0.9.1</jwt.version>
<fastjson.version>2.0.21</fastjson.version>
</properties>
<!--配置dependencyManagement锁定依赖的版本-->
<dependencyManagement>
<dependencies>
<!--mybatis-plus 持久层-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--knife4j-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
<!--jjwt-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jwt.version}</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
3.2.2 common-util模块依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided </scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>
3.2.3 service-util模块依赖
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>common-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
3.2.4 model模块依赖
<dependencies>
<!--lombok用来简化实体类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<scope>provided </scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<scope>provided </scope>
</dependency>
</dependencies>
3.2.5 service-oa模块依赖
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>model</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>service-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.3 MyBatis-Plus测试
3.3.1 创建数据库和角色表
-- 建数据库
CREATE DATABASE `YunshangOffice`;
use `YunshangOffice`;
-- 建角色表
CREATE TABLE `sys_role` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '角色id',
`role_name` varchar(20) NOT NULL DEFAULT '' COMMENT '角色名称',
`role_code` varchar(20) DEFAULT NULL COMMENT '角色编码',
`description` varchar(255) DEFAULT NULL COMMENT '描述',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`is_deleted` tinyint(3) NOT NULL DEFAULT '0' COMMENT '删除标记(0:不可用 1:可用)',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='角色';
3.3.2 配置文件
使用了springboot的应用,配置文件只有一个,名称为application.yml(后缀还可以为yaml或properties),但是在配置文件中可以使用spring.profiles属性指定特定场景下的配置文件,例如,指定开发环境配置文件后缀为dev,那么开发环境的配置文件名称就为application-dev.properties。
# application.yml
spring:
application:
name: service-oa
profiles:
active: dev
# application-dev.yml,注意这个跟上面的是两个文件
server:
port: 8800
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 查看日志
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/数据库名?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8 #serverTimezone用于设置时区
username: 自己的用户名
password: 自己的密码
3.3.3 实体类
//BaseEntity
package pers.beiluo.yunshangoffice.model.base;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Data
public class BaseEntity implements Serializable {
//设置主键生成策略是自增,实体类主键字段与表主键字段一致,所以不需要设置映射关系
@TableId(type = IdType.AUTO)
private Long id;
/**
* 使用@TableField注解将表中字段与实体类字段匹配
*/
@TableField("create_time")
private Date createTime;
@TableField("update_time")
private Date updateTime;
/**
* @TableLogic注解表示逻辑删除
*/
@TableLogic
@TableField("is_deleted")
private Integer isDeleted;
/**
* exist属性为false表示这个字段在表中不存在
*/
@TableField(exist = false)
private Map<String,Object> param = new HashMap<>();
}
//sysrole
package pers.beiluo.yunshangoffice.model.system;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import pers.beiluo.yunshangoffice.model.base.BaseEntity;
/**
* 经过测试发现,如果使用@Data注解,不是子类直接定义的属性,是不会生成相应的set,get方法的
* 也就是说,如果父类不使用@Data注解,那么子类中就不会存在父类属性的set,get方法
* 而且需要注意,父类属性不会被写到子类的toString方法中
* 使用单独的注解如ToString注解,可以通过配置将父类属性写到子类toString方法中
* @TableName注解用于将实体类与表关联起来
*/
@Data
@TableName("sys_role")
public class SysPole extends BaseEntity {
//serialVersionUID用于在序列化和反序列化时检查版本一致性问题
private static final long serialVersionUID = 1L;
@TableField("role_name")
private String roleName;
@TableField("role_code")
private String roleCode;
@TableField("description")
private String description;
}
3.3.4 添加Mapper类
package pers.beiluo.yunshangoffice.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import pers.beiluo.yunshangoffice.model.system.SysRole;
/**
* @Mapper:说明这是一个mapper,如果在启动类使用了@MapperScan注解,这里就可以不使用@Mapper注解
* 因为接口的对象是在运行时生成的,所以编译时会爆红,但是不影响运行
*/
@Mapper
public interface SysRoleMapper extends BaseMapper<SysRole> {
}
3.3.5 启动类
package pers.beiluo.yunshangoffice;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 使用@MapperScan注解设置在哪个包中扫描mapper,否则会出错
*/
@SpringBootApplication(scanBasePackages = "pers.beiluo.yunshangoffice")
@MapperScan("pers.beiluo.yunshangoffice")
public class ServiceAuthApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceAuthApplication.class,args);
}
}
3.3.6 测试
//测试类
package pers.beiluo.yunshangoffice;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import pers.beiluo.yunshangoffice.mapper.SysRoleMapper;
import pers.beiluo.yunshangoffice.model.system.SysRole;
import javax.annotation.Resource;
import java.util.List;
//springboot中的测试类需要写上@SpringBootTest注解,否则测试会报错
@SpringBootTest
public class SysRoleMapperTest {
/**
* 这里使用
*/
@Autowired
private SysRoleMapper mapper;
}
3.3.6.1 查询所有角色
//查询所有用户测试
@Test
public void getAllRole(){
List<SysRole> sysRoles = mapper.selectList(null);
sysRoles.forEach(System.out::println);
}
3.3.6.2 添加角色
//insert测试
@Test
public void insertTest(){
SysRole sysRole = new SysRole();
sysRole.setRoleName("角色管理员");
sysRole.setRoleCode("role");
sysRole.setDescription("角色管理员");
int insert = mapper.insert(sysRole);
System.out.println(insert);
//mybatis中自动生成的主键会自动填回到实体类对象中
System.out.println(sysRole);
}
3.3.6.3 更新角色
//跟新角色
@Test
public void updateTest(){
SysRole sysRole = new SysRole();
sysRole.setId(1L);
sysRole.setRoleName("普通管理员111");
int i = mapper.updateById(sysRole);
System.out.println(i);
}
3.3.6.4 删除角色
//根据id删除角色
@Test
public void deleteTest(){
int i = mapper.deleteById(9);
System.out.println(i);
}
//批量删除角色
@Test
public void deleteBatchTest(){
//Arrays.asList将数组转换成list
int i = mapper.deleteBatchIds(Arrays.asList(1,2));
System.out.println(i);
}
3.3.6.5 条件查询
//条件查询
@Test
public void queryTest(){
//new一个wrapper
//QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();
//设置条件,注意第一个参数为数据库表的字段名,不是实体类的字段名
//sysRoleQueryWrapper.eq("role_code", "role");
//使用lambdaquerywrapper可以避免字段名写错的问题
LambdaQueryWrapper<SysRole> sysRoleQueryWrapper = new LambdaQueryWrapper<>();
//设置条件,这种方式获取表字段名的原理是:首先会有一个缓存存储了实体类字段与表字段的对应关系
//然后通过方法名得到对应的实体类字段名,再通过缓存查找相应字段名
sysRoleQueryWrapper.eq(SysRole::getRoleCode,"role");
//查询
List<SysRole> sysRoles = mapper.selectList(sysRoleQueryWrapper);
sysRoles.forEach(System.out::println);
}