parent
e9d80733a1
commit
82ae1f9e90
10 changed files with 331 additions and 48 deletions
@ -1,16 +1,91 @@ |
||||
package cc.bnblogs.usercenterbackend.model; |
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType; |
||||
import com.baomidou.mybatisplus.annotation.TableField; |
||||
import com.baomidou.mybatisplus.annotation.TableId; |
||||
import com.baomidou.mybatisplus.annotation.TableName; |
||||
import java.io.Serializable; |
||||
import java.util.Date; |
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @description: 用户实体 |
||||
* @author: zfp@bnblogs.cc |
||||
* @date: 2023/3/13 15:39 |
||||
* 用户 |
||||
* @TableName user |
||||
*/ |
||||
@TableName(value ="user") |
||||
@Data |
||||
public class User { |
||||
public class User implements Serializable { |
||||
/** |
||||
* id |
||||
*/ |
||||
@TableId(type = IdType.AUTO) |
||||
private Long id; |
||||
private String name; |
||||
private Integer age; |
||||
|
||||
/** |
||||
* 用户昵称 |
||||
*/ |
||||
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; |
||||
|
||||
/** |
||||
* 是否删除 |
||||
*/ |
||||
private Integer isDelete; |
||||
|
||||
/** |
||||
* 用户角色 0 - 普通用户 1 - 管理员 |
||||
*/ |
||||
private Integer userRole; |
||||
|
||||
/** |
||||
* 星球编号 |
||||
*/ |
||||
private String planetCode; |
||||
|
||||
@TableField(exist = false) |
||||
private static final long serialVersionUID = 1L; |
||||
} |
@ -0,0 +1,21 @@ |
||||
package cc.bnblogs.usercenterbackend.service; |
||||
|
||||
import cc.bnblogs.usercenterbackend.model.User; |
||||
import com.baomidou.mybatisplus.extension.service.IService; |
||||
|
||||
/** |
||||
* @author zfp@bnblogs.cc |
||||
* @description 针对表【user(用户)】的数据库操作Service |
||||
* @createDate 2023-03-13 17:18:58 |
||||
*/ |
||||
public interface UserService extends IService<User> { |
||||
|
||||
/** |
||||
* 用户注册 |
||||
* @param userAccount 登录账户名称(不是昵称) |
||||
* @param userPassword 用户密码 |
||||
* @param checkPassword 二次密码 |
||||
* @return 新用户id |
||||
*/ |
||||
long userRegister(String userAccount,String userPassword,String checkPassword); |
||||
} |
@ -0,0 +1,87 @@ |
||||
package cc.bnblogs.usercenterbackend.service.impl; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
import cc.bnblogs.usercenterbackend.model.User; |
||||
import cc.bnblogs.usercenterbackend.service.UserService; |
||||
import cc.bnblogs.usercenterbackend.mapper.UserMapper; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.util.DigestUtils; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.regex.Matcher; |
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* @author zfp@bnblogs.cc |
||||
* @description 针对表【user(用户)】的数据库操作Service实现 |
||||
* @createDate 2023-03-13 17:18:58 |
||||
*/ |
||||
@Service |
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User> |
||||
implements UserService{ |
||||
|
||||
private static final String ACCOUNT_VALID= "^[a-zA-Z][a-zA-Z0-9_]{3,}"; |
||||
private static final String SALT = "salt"; |
||||
|
||||
@Resource |
||||
private UserMapper userMapper; |
||||
|
||||
@Override |
||||
public long userRegister(String userAccount, String userPassword, String checkPassword) { |
||||
// 1.校验字段
|
||||
// 字段值不能为空
|
||||
if (StringUtils.isAnyBlank(userAccount,userPassword,checkPassword)) { |
||||
return -1; |
||||
} |
||||
// 账户名不少于4位
|
||||
if (userAccount.length() < 4) { |
||||
return -1; |
||||
} |
||||
// 账户名只能使用字母数字下划线,且必须字母开头
|
||||
Matcher matcher = Pattern.compile(ACCOUNT_VALID).matcher(userAccount); |
||||
|
||||
if (!matcher.find()) { |
||||
return -1; |
||||
} |
||||
|
||||
// 密码不少于8位
|
||||
if (userPassword.length() < 8) { |
||||
return -1; |
||||
} |
||||
|
||||
// 两次输入的密码不一致
|
||||
if (!userPassword.equals(checkPassword)) { |
||||
return -1; |
||||
} |
||||
|
||||
// 账户不能重复, 涉及数据库操作的检验放在靠后的地方
|
||||
QueryWrapper<User> qw = new QueryWrapper<>(); |
||||
qw.eq("userAccount",userAccount); |
||||
long count = userMapper.selectCount(qw); |
||||
if (count > 0) { |
||||
return -1; |
||||
} |
||||
|
||||
//2.对原始密码进行加密
|
||||
String encryptPassword = DigestUtils.md5DigestAsHex(( SALT+ userPassword).getBytes()); |
||||
|
||||
|
||||
//3.存入数据库中
|
||||
User user = new User(); |
||||
user.setUserAccount(userAccount); |
||||
user.setUserPassword(encryptPassword); |
||||
int saveResult = userMapper.insert(user); |
||||
// 插入数据失败
|
||||
if (saveResult == 0) { |
||||
return -1; |
||||
} |
||||
return user.getId(); |
||||
} |
||||
} |
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,31 @@ |
||||
<?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="cc.bnblogs.usercenterbackend.mapper.UserMapper"> |
||||
|
||||
<resultMap id="BaseResultMap" type="cc.bnblogs.usercenterbackend.model.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"/> |
||||
<result property="planetCode" column="planetCode" jdbcType="VARCHAR"/> |
||||
</resultMap> |
||||
|
||||
<sql id="Base_Column_List"> |
||||
id,username,userAccount, |
||||
avatarUrl,gender,userPassword, |
||||
phone,email,userStatus, |
||||
createTime,updateTime,isDelete, |
||||
userRole,planetCode |
||||
</sql> |
||||
</mapper> |
@ -1,26 +1,64 @@ |
||||
package cc.bnblogs.usercenterbackend; |
||||
|
||||
import cc.bnblogs.usercenterbackend.mapper.UserMapper; |
||||
import cc.bnblogs.usercenterbackend.model.User; |
||||
import org.junit.Assert; |
||||
import cc.bnblogs.usercenterbackend.service.UserService; |
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.util.DigestUtils; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.List; |
||||
|
||||
@SpringBootTest |
||||
class UserCenterBackendApplicationTests { |
||||
@Resource |
||||
private UserMapper userMapper; |
||||
|
||||
@Resource |
||||
private UserService userService; |
||||
@Test |
||||
void contextLoads() { |
||||
System.out.println(("----- selectAll method test ------")); |
||||
List<User> userList = userMapper.selectList(null); |
||||
Assert.assertEquals(5, userList.size()); |
||||
userList.forEach(System.out::println); |
||||
// 密码加密
|
||||
String newPassword = DigestUtils.md5DigestAsHex(("abcd" + "mypassword").getBytes()); |
||||
// String newPassword_1 = DigestUtils.md5DigestAsHex(("abcd" + "mypassword").getBytes());
|
||||
|
||||
System.out.println(newPassword); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
void testRegisterUser() { |
||||
String userAccount = "aaaa"; |
||||
String userPassword = "12345678"; |
||||
String checkPassword = ""; |
||||
// 测试字段为空
|
||||
long result = userService.userRegister(userAccount, userPassword, checkPassword); |
||||
Assertions.assertEquals(-1, result); |
||||
// 测试账户名长度
|
||||
userAccount = "aaa"; |
||||
result = userService.userRegister(userAccount, userPassword, checkPassword); |
||||
Assertions.assertEquals(-1, result); |
||||
|
||||
// 测试账户名是否包含特殊字符
|
||||
userAccount = "hjsdu$ "; |
||||
result = userService.userRegister(userAccount, userPassword, checkPassword); |
||||
Assertions.assertEquals(-1, result); |
||||
|
||||
// 测试密码不少于8位
|
||||
userAccount = "aaaa"; |
||||
userPassword = "123456"; |
||||
result = userService.userRegister(userAccount, userPassword, checkPassword); |
||||
Assertions.assertEquals(-1, result); |
||||
|
||||
// 测试两次密码输入是否一致
|
||||
userPassword = "12345678"; |
||||
checkPassword = "123456789"; |
||||
result = userService.userRegister(userAccount, userPassword, checkPassword); |
||||
Assertions.assertEquals(-1, result); |
||||
|
||||
// 正确插入
|
||||
userPassword = "12345678"; |
||||
checkPassword = "12345678"; |
||||
result = userService.userRegister(userAccount, userPassword, checkPassword); |
||||
Assertions.assertTrue(result > 0); |
||||
|
||||
|
||||
} |
||||
} |
||||
|
@ -1,27 +0,0 @@ |
||||
package cc.bnblogs.usercenterbackend.mapper; |
||||
|
||||
import cc.bnblogs.usercenterbackend.model.User; |
||||
import org.junit.Assert; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.List; |
||||
|
||||
@SpringBootTest |
||||
//@RunWith(SpringRunner.class)
|
||||
public class SampleTest { |
||||
|
||||
@Resource |
||||
private UserMapper userMapper; |
||||
|
||||
// 导入这个import org.junit.jupiter.api.Test;就不需要加@RunWith(SpringRunner.class)
|
||||
@Test |
||||
public void testSelect() { |
||||
System.out.println(("----- selectAll method test ------")); |
||||
List<User> userList = userMapper.selectList(null); |
||||
Assert.assertEquals(5, userList.size()); |
||||
userList.forEach(System.out::println); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,35 @@ |
||||
package cc.bnblogs.usercenterbackend.service; |
||||
|
||||
import cc.bnblogs.usercenterbackend.model.User; |
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
||||
import javax.annotation.Resource; |
||||
|
||||
|
||||
/** |
||||
* @description: |
||||
* @author: zfp@bnblogs.cc |
||||
* @date: 2023/3/13 17:26 |
||||
*/ |
||||
@SpringBootTest |
||||
public class UserServiceTest { |
||||
@Resource |
||||
private UserService userService; |
||||
@Test |
||||
void addUser() { |
||||
User user = new User(); |
||||
user.setUsername("aaa"); |
||||
user.setUserAccount("111"); |
||||
user.setAvatarUrl("https://hugo.bnblogs.cc/images/img/20220215001349.png"); |
||||
user.setUserPassword("1111"); |
||||
user.setPhone("111"); |
||||
user.setEmail("11"); |
||||
user.setPlanetCode("11"); |
||||
|
||||
boolean result = userService.save(user); |
||||
System.out.println(user.getId()); |
||||
Assertions.assertTrue(result); |
||||
} |
||||
} |
Loading…
Reference in new issue