feat: 分库分表
This commit is contained in:
parent
35f88a0937
commit
e0d76a3bf9
28
README.md
28
README.md
@ -1,28 +0,0 @@
|
||||
# SpringBoot 项目初始模板
|
||||
|
||||
> by [程序员鱼皮知识星球](https://yupi.icu)
|
||||
|
||||
Java SpringBoot 项目初始模板,整合了常用框架和示例代码,大家可以在此基础上快速开发自己的项目。
|
||||
|
||||
## 模板功能
|
||||
|
||||
- Spring Boot 2.7.0(贼新)
|
||||
- Spring MVC
|
||||
- MySQL 驱动
|
||||
- MyBatis
|
||||
- MyBatis Plus
|
||||
- Spring Session Redis 分布式登录
|
||||
- Spring AOP
|
||||
- Apache Commons Lang3 工具类
|
||||
- Lombok 注解
|
||||
- Swagger + Knife4j 接口文档
|
||||
- Spring Boot 调试工具和项目处理器
|
||||
- 全局请求响应拦截器(记录日志)
|
||||
- 全局异常处理器
|
||||
- 自定义错误码
|
||||
- 封装通用响应类
|
||||
- 示例用户注册、登录、搜索功能
|
||||
- 示例单元测试类
|
||||
- 示例 SQL(用户表)
|
||||
|
||||
访问 localhost:7529/api/doc.html 就能在线调试接口了,不需要前端配合啦~
|
96
pom.xml
96
pom.xml
@ -1,14 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.7.0</version>
|
||||
<version>2.1.3.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.yupi</groupId>
|
||||
<artifactId>springboot-init</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
@ -26,24 +27,13 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
<!-- 实现对 MyBatis 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.2.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>3.5.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.session</groupId>
|
||||
<artifactId>spring-session-data-redis</artifactId>
|
||||
<version>2.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
@ -95,6 +85,76 @@
|
||||
<version>4.13.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- 实现对数据库连接池的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency> <!-- 本示例,我们使用 MySQL -->
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.48</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 MyBatis 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Sharding-JDBC 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.shardingsphere</groupId>
|
||||
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
|
||||
<version>4.0.0-RC2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 保证 Spring AOP 相关的依赖包 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aspects</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 方便等会写单元测试 -->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-starter-test</artifactId>-->
|
||||
<!-- <scope>test</scope>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<!--引入Jaxb,开始-->
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-core</artifactId>
|
||||
<version>2.2.11</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-impl</artifactId>
|
||||
<version>2.2.11</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jaxb</groupId>
|
||||
<artifactId>jaxb-runtime</artifactId>
|
||||
<version>2.2.10-b140310.1920</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.activation</groupId>
|
||||
<artifactId>activation</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
<!--引入Jaxb,结束-->
|
||||
|
||||
<!-- 保证 Spring AOP 相关的依赖包 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aspects</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -3,7 +3,12 @@ package com.yupi.project;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
@Configuration
|
||||
@SpringBootApplication
|
||||
@MapperScan("com.yupi.project.mapper")
|
||||
public class MyApplication {
|
||||
|
@ -1,8 +1,6 @@
|
||||
package com.yupi.project.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@ -14,10 +12,10 @@ public class MybatisPlusConfig {
|
||||
/**
|
||||
* 新的分页插件
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
return interceptor;
|
||||
}
|
||||
// @Bean
|
||||
// public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
// MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
// interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
// return interceptor;
|
||||
// }
|
||||
}
|
@ -1,104 +0,0 @@
|
||||
package com.yupi.project.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.yupi.project.common.BaseResponse;
|
||||
import com.yupi.project.common.ErrorCode;
|
||||
import com.yupi.project.common.ResultUtils;
|
||||
import com.yupi.project.exception.BusinessException;
|
||||
import com.yupi.project.model.entity.User;
|
||||
import com.yupi.project.model.request.UserLoginRequest;
|
||||
import com.yupi.project.model.request.UserRegisterRequest;
|
||||
import com.yupi.project.service.UserService;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 用户接口
|
||||
*
|
||||
* @author yupi
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@PostMapping("/register")
|
||||
public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
|
||||
if (userRegisterRequest == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
String userAccount = userRegisterRequest.getUserAccount();
|
||||
String userPassword = userRegisterRequest.getUserPassword();
|
||||
String checkPassword = userRegisterRequest.getCheckPassword();
|
||||
if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
|
||||
return null;
|
||||
}
|
||||
long result = userService.userRegister(userAccount, userPassword, checkPassword);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
|
||||
if (userLoginRequest == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
String userAccount = userLoginRequest.getUserAccount();
|
||||
String userPassword = userLoginRequest.getUserPassword();
|
||||
if (StringUtils.isAnyBlank(userAccount, userPassword)) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
User user = userService.userLogin(userAccount, userPassword, request);
|
||||
return ResultUtils.success(user);
|
||||
}
|
||||
|
||||
@PostMapping("/logout")
|
||||
public BaseResponse<Integer> userLogout(HttpServletRequest request) {
|
||||
if (request == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
int result = userService.userLogout(request);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
@GetMapping("/current")
|
||||
public BaseResponse<User> getCurrentUser(HttpServletRequest request) {
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
long userId = loginUser.getId();
|
||||
User user = userService.getById(userId);
|
||||
User safetyUser = userService.getSafetyUser(user);
|
||||
return ResultUtils.success(safetyUser);
|
||||
}
|
||||
|
||||
@GetMapping("/search")
|
||||
public BaseResponse<List<User>> searchUser(String username, HttpServletRequest request) {
|
||||
userService.assertAdmin(request);
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
if (StringUtils.isNotBlank(username)) {
|
||||
queryWrapper.like("username", username);
|
||||
}
|
||||
List<User> userList = userService.list(queryWrapper);
|
||||
List<User> list = userList.stream().map(user -> userService.getSafetyUser(user)).collect(Collectors.toList());
|
||||
return ResultUtils.success(list);
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
public BaseResponse<Boolean> deleteUser(@RequestBody long id, HttpServletRequest request) {
|
||||
userService.assertAdmin(request);
|
||||
if (id <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
boolean b = userService.removeById(id);
|
||||
return ResultUtils.success(b);
|
||||
}
|
||||
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package com.yupi.project.mapper;
|
||||
|
||||
import com.yupi.project.model.entity.User;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* UserMapper
|
||||
*/
|
||||
public interface UserMapper extends BaseMapper<User> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -1,90 +0,0 @@
|
||||
package com.yupi.project.model.entity;
|
||||
|
||||
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 com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 用户
|
||||
*
|
||||
* @TableName user
|
||||
*/
|
||||
@TableName(value = "user")
|
||||
@Data
|
||||
public class User implements Serializable {
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 用户昵称
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
private String userAccount;
|
||||
|
||||
/**
|
||||
* 用户头像
|
||||
*/
|
||||
private String avatarUrl;
|
||||
|
||||
/**
|
||||
* 性别
|
||||
*/
|
||||
private Integer gender;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String userPassword;
|
||||
|
||||
/**
|
||||
* 电话
|
||||
*/
|
||||
private String phone;
|
||||
|
||||
/**
|
||||
* 邮箱
|
||||
*/
|
||||
private String email;
|
||||
|
||||
/**
|
||||
* 状态 0 - 正常
|
||||
*/
|
||||
private Integer userStatus;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 是否删除
|
||||
*/
|
||||
@TableLogic
|
||||
private Integer isDelete;
|
||||
|
||||
/**
|
||||
* 用户角色 0 - 普通用户 1 - 管理员
|
||||
*/
|
||||
private Integer userRole;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
package com.yupi.project.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.yupi.project.model.entity.User;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 用户服务
|
||||
*
|
||||
* @author yupi
|
||||
*/
|
||||
public interface UserService extends IService<User> {
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*
|
||||
* @param userAccount 用户账户
|
||||
* @param userPassword 用户密码
|
||||
* @param checkPassword 校验密码
|
||||
* @return 新用户 id
|
||||
*/
|
||||
long userRegister(String userAccount, String userPassword, String checkPassword);
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
* @param userAccount 用户账户
|
||||
* @param userPassword 用户密码
|
||||
* @param request
|
||||
* @return 脱敏后的用户信息
|
||||
*/
|
||||
User userLogin(String userAccount, String userPassword, HttpServletRequest request);
|
||||
|
||||
/**
|
||||
* 用户脱敏
|
||||
*
|
||||
* @param originUser
|
||||
* @return
|
||||
*/
|
||||
User getSafetyUser(User originUser);
|
||||
|
||||
/**
|
||||
* 用户注销
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
int userLogout(HttpServletRequest request);
|
||||
|
||||
/**
|
||||
* 是否为管理员
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
boolean isAdmin(HttpServletRequest request);
|
||||
|
||||
/**
|
||||
* 是否为管理员
|
||||
*
|
||||
* @param user
|
||||
* @return
|
||||
*/
|
||||
boolean isAdmin(User user);
|
||||
|
||||
/**
|
||||
* 断言是管理员
|
||||
*
|
||||
* @param request
|
||||
*/
|
||||
void assertAdmin(HttpServletRequest request);
|
||||
|
||||
/**
|
||||
* 获取登录用户(查缓存)
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
* @throws com.yupi.project.exception.BusinessException 未登录则抛异常
|
||||
*/
|
||||
User getLoginUser(HttpServletRequest request);
|
||||
}
|
@ -1,189 +0,0 @@
|
||||
package com.yupi.project.service.impl;
|
||||
|
||||
import static com.yupi.project.constant.UserConstant.USER_LOGIN_STATE;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.yupi.project.common.ErrorCode;
|
||||
import com.yupi.project.constant.UserConstant;
|
||||
import com.yupi.project.exception.BusinessException;
|
||||
import com.yupi.project.mapper.UserMapper;
|
||||
import com.yupi.project.model.entity.User;
|
||||
import com.yupi.project.service.UserService;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.DigestUtils;
|
||||
|
||||
/**
|
||||
* 用户服务实现类
|
||||
*
|
||||
* @author yupi
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
||||
implements UserService {
|
||||
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
|
||||
/**
|
||||
* 盐值,混淆密码
|
||||
*/
|
||||
private static final String SALT = "yupi";
|
||||
|
||||
@Override
|
||||
public long userRegister(String userAccount, String userPassword, String checkPassword) {
|
||||
// 1. 校验
|
||||
if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
|
||||
}
|
||||
if (userAccount.length() < 4) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户账号过短");
|
||||
}
|
||||
if (userPassword.length() < 8 || checkPassword.length() < 8) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户密码过短");
|
||||
}
|
||||
// 账户不能包含特殊字符
|
||||
String validPattern = "[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
|
||||
Matcher matcher = Pattern.compile(validPattern).matcher(userAccount);
|
||||
if (matcher.find()) {
|
||||
return -1;
|
||||
}
|
||||
// 密码和校验密码相同
|
||||
if (!userPassword.equals(checkPassword)) {
|
||||
return -1;
|
||||
}
|
||||
// 账户不能重复
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("userAccount", userAccount);
|
||||
long count = userMapper.selectCount(queryWrapper);
|
||||
if (count > 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR, "账号重复");
|
||||
}
|
||||
// 2. 加密
|
||||
String encryptPassword = DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());
|
||||
// 3. 插入数据
|
||||
User user = new User();
|
||||
user.setUserAccount(userAccount);
|
||||
user.setUserPassword(encryptPassword);
|
||||
boolean saveResult = this.save(user);
|
||||
if (!saveResult) {
|
||||
return -1;
|
||||
}
|
||||
return user.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public User userLogin(String userAccount, String userPassword, HttpServletRequest request) {
|
||||
// 1. 校验
|
||||
if (StringUtils.isAnyBlank(userAccount, userPassword)) {
|
||||
return null;
|
||||
}
|
||||
if (userAccount.length() < 4) {
|
||||
return null;
|
||||
}
|
||||
if (userPassword.length() < 8) {
|
||||
return null;
|
||||
}
|
||||
// 账户不能包含特殊字符
|
||||
String validPattern = "[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
|
||||
Matcher matcher = Pattern.compile(validPattern).matcher(userAccount);
|
||||
if (matcher.find()) {
|
||||
return null;
|
||||
}
|
||||
// 2. 加密
|
||||
String encryptPassword = DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());
|
||||
// 查询用户是否存在
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("userAccount", userAccount);
|
||||
queryWrapper.eq("userPassword", encryptPassword);
|
||||
User user = userMapper.selectOne(queryWrapper);
|
||||
// 用户不存在
|
||||
if (user == null) {
|
||||
log.info("user login failed, userAccount cannot match userPassword");
|
||||
return null;
|
||||
}
|
||||
// 3. 用户脱敏
|
||||
User safetyUser = getSafetyUser(user);
|
||||
// 4. 记录用户的登录态
|
||||
request.getSession().setAttribute(USER_LOGIN_STATE, safetyUser);
|
||||
return safetyUser;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户脱敏
|
||||
*
|
||||
* @param originUser
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public User getSafetyUser(User originUser) {
|
||||
if (originUser == null) {
|
||||
return null;
|
||||
}
|
||||
User safetyUser = new User();
|
||||
safetyUser.setId(originUser.getId());
|
||||
safetyUser.setUsername(originUser.getUsername());
|
||||
safetyUser.setUserAccount(originUser.getUserAccount());
|
||||
safetyUser.setAvatarUrl(originUser.getAvatarUrl());
|
||||
safetyUser.setGender(originUser.getGender());
|
||||
safetyUser.setPhone(originUser.getPhone());
|
||||
safetyUser.setEmail(originUser.getEmail());
|
||||
safetyUser.setUserRole(originUser.getUserRole());
|
||||
safetyUser.setUserStatus(originUser.getUserStatus());
|
||||
safetyUser.setCreateTime(originUser.getCreateTime());
|
||||
return safetyUser;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注销
|
||||
*
|
||||
* @param request
|
||||
*/
|
||||
@Override
|
||||
public int userLogout(HttpServletRequest request) {
|
||||
// 移除登录态
|
||||
request.getSession().removeAttribute(USER_LOGIN_STATE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAdmin(HttpServletRequest request) {
|
||||
Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
|
||||
User user = (User) userObj;
|
||||
return user != null && user.getUserRole() == UserConstant.ADMIN_ROLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAdmin(User user) {
|
||||
return user != null && user.getUserRole() == UserConstant.ADMIN_ROLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void assertAdmin(HttpServletRequest request) {
|
||||
if (!isAdmin(request)) {
|
||||
throw new BusinessException(ErrorCode.NO_AUTH);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getLoginUser(HttpServletRequest request) {
|
||||
Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
|
||||
User loginUser = (User) userObj;
|
||||
if (loginUser == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_LOGIN);
|
||||
}
|
||||
return loginUser;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -1,34 +1,48 @@
|
||||
spring:
|
||||
application:
|
||||
name: springboot-init
|
||||
# DataSource Config
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/my_db
|
||||
username: root
|
||||
password: 123456
|
||||
mvc:
|
||||
pathmatch:
|
||||
matching-strategy: ANT_PATH_MATCHER
|
||||
# session 失效时间(分钟)
|
||||
session:
|
||||
timeout: 86400
|
||||
store-type: redis
|
||||
# redis 配置
|
||||
redis:
|
||||
port: 6379
|
||||
host: localhost
|
||||
database: 0
|
||||
# ShardingSphere 配置项
|
||||
shardingsphere:
|
||||
datasource:
|
||||
# 所有数据源的名字
|
||||
names: db0,db1
|
||||
# 订单 orders 数据源配置 00
|
||||
# 订单 orders 数据源配置 01
|
||||
db0:
|
||||
type: com.zaxxer.hikari.HikariDataSource # 使用 Hikari 数据库连接池
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
jdbc-url: jdbc:mysql://120.26.194.135:3306/db0?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
username: db0
|
||||
password: xyy
|
||||
db1:
|
||||
type: com.zaxxer.hikari.HikariDataSource # 使用 Hikari 数据库连接池
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
jdbc-url: jdbc:mysql://120.26.194.135:3306/db1?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
username: db1
|
||||
password: xyy
|
||||
|
||||
# 分片规则
|
||||
sharding:
|
||||
tables:
|
||||
# orders 表配置
|
||||
orders:
|
||||
actualDataNodes: db${0..1}.orders_${0..7} # 映射到 ds-orders-0 和 ds-orders-1 数据源的 orders 表们
|
||||
databaseStrategy:
|
||||
inline:
|
||||
sharding-column: user_id
|
||||
algorithm-expression: db${user_id % 2}
|
||||
tableStrategy:
|
||||
inline:
|
||||
sharding-column: user_id
|
||||
algorithm-expression: orders_${user_id % 8}
|
||||
# 拓展属性配置
|
||||
props:
|
||||
sql:
|
||||
show: true # 打印 SQL
|
||||
server:
|
||||
port: 7529
|
||||
servlet:
|
||||
context-path: /api
|
||||
mybatis-plus:
|
||||
configuration:
|
||||
map-underscore-to-camel-case: false
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
global-config:
|
||||
db-config:
|
||||
logic-delete-field: isDelete # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
|
||||
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
|
||||
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
|
||||
mybatis:
|
||||
mapper-locations: classpath*:src/main/resources/mapper/*.xml
|
||||
type-aliases-package: com.yupi.project.model.entity
|
@ -1 +0,0 @@
|
||||
我的项目 by 程序员鱼皮 https://github.com/liyupi
|
@ -1,30 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.yupi.project.mapper.UserMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="com.yupi.project.model.entity.User">
|
||||
<id property="id" column="id" jdbcType="BIGINT"/>
|
||||
<result property="username" column="username" jdbcType="VARCHAR"/>
|
||||
<result property="userAccount" column="userAccount" jdbcType="VARCHAR"/>
|
||||
<result property="avatarUrl" column="avatarUrl" jdbcType="VARCHAR"/>
|
||||
<result property="gender" column="gender" jdbcType="TINYINT"/>
|
||||
<result property="userPassword" column="userPassword" jdbcType="VARCHAR"/>
|
||||
<result property="phone" column="phone" jdbcType="VARCHAR"/>
|
||||
<result property="email" column="email" jdbcType="VARCHAR"/>
|
||||
<result property="userStatus" column="userStatus" jdbcType="INTEGER"/>
|
||||
<result property="createTime" column="createTime" jdbcType="TIMESTAMP"/>
|
||||
<result property="updateTime" column="updateTime" jdbcType="TIMESTAMP"/>
|
||||
<result property="isDelete" column="isDelete" jdbcType="TINYINT"/>
|
||||
<result property="userRole" column="userRole" jdbcType="INTEGER"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,username,userAccount,
|
||||
avatarUrl,gender,userPassword,
|
||||
phone,email,userStatus,
|
||||
createTime,updateTime,isDelete,
|
||||
userRole
|
||||
</sql>
|
||||
</mapper>
|
@ -1,96 +0,0 @@
|
||||
package com.yupi.project.service;
|
||||
|
||||
import com.yupi.project.model.entity.User;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 用户服务测试
|
||||
*
|
||||
* @author yupi
|
||||
*/
|
||||
@SpringBootTest
|
||||
class UserServiceTest {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Test
|
||||
void testAddUser() {
|
||||
User user = new User();
|
||||
user.setUsername("dogYupi");
|
||||
user.setUserAccount("123");
|
||||
user.setAvatarUrl("");
|
||||
user.setGender(0);
|
||||
user.setUserPassword("xxx");
|
||||
user.setPhone("123");
|
||||
user.setEmail("456");
|
||||
boolean result = userService.save(user);
|
||||
System.out.println(user.getId());
|
||||
Assertions.assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateUser() {
|
||||
User user = new User();
|
||||
user.setId(1);
|
||||
user.setUsername("dogYupi");
|
||||
user.setUserAccount("123");
|
||||
user.setAvatarUrl("");
|
||||
user.setGender(0);
|
||||
user.setUserPassword("xxx");
|
||||
user.setPhone("123");
|
||||
user.setEmail("456");
|
||||
boolean result = userService.updateById(user);
|
||||
Assertions.assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteUser() {
|
||||
boolean result = userService.removeById(1L);
|
||||
Assertions.assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUser() {
|
||||
User user = userService.getById(1L);
|
||||
Assertions.assertNotNull(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
void userRegister() {
|
||||
String userAccount = "yupi";
|
||||
String userPassword = "";
|
||||
String checkPassword = "123456";
|
||||
try {
|
||||
long result = userService.userRegister(userAccount, userPassword, checkPassword);
|
||||
Assertions.assertEquals(-1, result);
|
||||
userAccount = "yu";
|
||||
result = userService.userRegister(userAccount, userPassword, checkPassword);
|
||||
Assertions.assertEquals(-1, result);
|
||||
userAccount = "yupi";
|
||||
userPassword = "123456";
|
||||
result = userService.userRegister(userAccount, userPassword, checkPassword);
|
||||
Assertions.assertEquals(-1, result);
|
||||
userAccount = "yu pi";
|
||||
userPassword = "12345678";
|
||||
result = userService.userRegister(userAccount, userPassword, checkPassword);
|
||||
Assertions.assertEquals(-1, result);
|
||||
checkPassword = "123456789";
|
||||
result = userService.userRegister(userAccount, userPassword, checkPassword);
|
||||
Assertions.assertEquals(-1, result);
|
||||
userAccount = "dogYupi";
|
||||
checkPassword = "12345678";
|
||||
result = userService.userRegister(userAccount, userPassword, checkPassword);
|
||||
Assertions.assertEquals(-1, result);
|
||||
userAccount = "yupi";
|
||||
result = userService.userRegister(userAccount, userPassword, checkPassword);
|
||||
Assertions.assertEquals(-1, result);
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user