学会debug 调试
记住:
F7 : 进入函数
F8 : 下一步
F9 : 下一个断点
开始断点调试
![](https://img-blog.csdnimg.cn/direct/14d17f3b24214f7fb954160398e4cd41.png)
结束断点调试
![](https://img-blog.csdnimg.cn/direct/3ec1df0581e84055b94571a4c9cf221b.png)
一、本人跑到第七条
![](https://img-blog.csdnimg.cn/direct/0395e2eff5af4265af179b24e7ed2b17.png)
二、测试部分的代码
![](https://img-blog.csdnimg.cn/direct/2dddc807c9574a72b0a411bc537af461.png)
代码:
@Test
void yupi1Register() {
String userAccount = "yupi";
String userPassword = "";
String checkPassword = "123456";
// 造数据,跑到业务里面测试数据 yupi1Service.yupi1Register(userAccount,userPassword,checkPassword)
// 测试字段是否为空 || null
long result = yupi1Service.yupi1Register(userAccount,userPassword,checkPassword);
Assertions.assertEquals(-1,result);
// 测试账户小于 4 位,不让录入
userAccount = "yu";
userPassword = "123456";
result = yupi1Service.yupi1Register(userAccount,userPassword,checkPassword);
Assertions.assertEquals(-1,result);
// 测试密码 & 校验密码长度 小于 8 位,不让录入
userAccount = "yupicdd";
userPassword = "12345";
checkPassword = "123456";
result = yupi1Service.yupi1Register(userAccount,userPassword,checkPassword);
Assertions.assertEquals(-1,result);
// 测试,用户名,有特殊字符,不让录入,且空格,不属于特殊字符。鱼皮造的数据有误
userAccount = "yu+pi";
userPassword = "123456789";
checkPassword = "123456789";
result = yupi1Service.yupi1Register(userAccount,userPassword,checkPassword);
Assertions.assertEquals(-1,result);
// 测试 密码 & 校验码 需相同,否则,不让录入
userAccount = "yupicdd";
userPassword = "123456789";
checkPassword = "1234567890";
result = yupi1Service.yupi1Register(userAccount,userPassword,checkPassword);
Assertions.assertEquals(-1,result);
// 这条数据符合条件,成功插入数据库
userAccount = "dogyupi";
userPassword = "123456789";
checkPassword = "123456789";
result = yupi1Service.yupi1Register(userAccount,userPassword,checkPassword);
Assertions.assertEquals(-1,result);
// 这条数据符合条件,成功插入数据库
userAccount = "yupi12";
userPassword = "1234567890";
checkPassword = "1234567890";
result = yupi1Service.yupi1Register(userAccount,userPassword,checkPassword);
Assertions.assertEquals(-1,result);
}
2.1 本人对测试代码进行修改 userAccount = "yu+pi";
特殊:
// 测试,用户名,有特殊字符,不让录入,且空格,不属于特殊字符。鱼皮造的数据有误
userAccount = "yu+pi";
userPassword = "123456789";
checkPassword = "123456789";
result = yupi1Service.yupi1Register(userAccount,userPassword,checkPassword);
Assertions.assertEquals(-1,result);
![](https://img-blog.csdnimg.cn/direct/3da9a3540e084187b76cf098d194935e.png)
![](https://img-blog.csdnimg.cn/direct/c45553e86c0c487ebeb4fb9180eac7c6.png)
![](https://img-blog.csdnimg.cn/direct/d6b9d3e4dd4c4b3995f96e3d3a450fb0.png)
三、反复测试代码,需把表格里的数据删除
代码
![](https://img-blog.csdnimg.cn/direct/90df87e2337b4e4596317a7f87819e77.png)
操作,刷新表数据
![](https://img-blog.csdnimg.cn/direct/275902ca117c40b89a69712943407f73.png)
四、其他关联的完整代码
4.1 业务数据
![](https://img-blog.csdnimg.cn/direct/cc58ef6690b2411b9434d57d05c54474.png)
以下不可复制:【自动生成的】
![](https://img-blog.csdnimg.cn/direct/bbbb874da6da44149864bb84919e0551.png)
![](https://img-blog.csdnimg.cn/direct/ff7e0f1eb1524bbfb17dd31c9b1840b9.png)
代码
package com.lingdong.usercenter.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingdong.usercenter.service.Yupi1Service;
import com.lingdong.usercenter.model.domain.Yupi1;
import com.lingdong.usercenter.mapper.Yupi1Mapper;
import org.apache.commons.lang3.StringUtils;
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 Daisy
*/
@Service
public class Yupi1ServiceImpl extends ServiceImpl<Yupi1Mapper, Yupi1>
implements Yupi1Service {
@Resource
private Yupi1Mapper yupi1Mapper;
@Override
public long yupi1Register(String userAccount, String userPassword, String checkPassword) {
// 1.校验 账户、密码、校验码 是否包含任何真空值(包含空格或空值)
// if (true 里面任意一个字段:包含空格或空值) return -1; 就失败了
if(StringUtils.isAnyBlank(userAccount,userPassword,checkPassword))
{
return -1;
}
if (userAccount.length() < 4)
{
return -1;
}
if (userPassword.length() < 8 || checkPassword.length() < 8)
{
return -1;
}
// 账户不能包含特殊字符
// m.find();//返回true 输入的账户有 特殊字符,需重新输入
String validPattern = "[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
Matcher matcher = Pattern.compile(validPattern).matcher(userAccount);
if (matcher.find())
{
return -1;
}
// 密码和校验密码相同
// 鱼皮代码 !userPassword.equals(checkPassword) , ! 去掉
/**
*输入密码 & 校验密码要相等吗?
*回答:
* 通常情况下,输入密码和校验密码需要相等。这是为了确保用户输入的密码是正确的,并且没有被其他人篡改或窃取。
* 在注册、登录等场景中,通常会要求用户输入两次密码,以验证其准确性。
*
* 这段代码是判断用户输入的密码(userPassword)是否与校验密码(checkPassword)相等。
*/
if (!(userPassword.equals(checkPassword)))
{
return -1;
}
//账户不能重复,放到最后,因为可以少查询一次数据库,提高效率
// 网址:https://blog.csdn.net/weixin_51654689/article/details/130903956
// Yupi1 相当于,表,存入实体对象
//
// 创建 QueryWrapper 对象
// 等值条件查询
// if count > 0 , 说明表里有重复数据,返回 -1
QueryWrapper<Yupi1> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userAccount",userAccount);
long count = yupi1Mapper.selectCount(queryWrapper);
if (count > 0)
{
return -1;
}
// 3.对密码进行加密(密码千万不要直接以明文存储到数据库中)
// 写代码前,已测试
/**
* 在本项目,test 文件夹下,UserCenterApplicationTests 里面书写代码,运行测试的
*
* @Test
* void testDigest() throws NoSuchAlgorithmException{
* String newPassword = DigestUtils.md5DigestAsHex(("abcd" + "mypassword").getBytes());
* System.out.println(newPassword);
* }
*
* 运行成功 输出为:(16进制)【可变】78e3504a70116aa7474d614b517809c6
*
*/
final String sAlt = "yupi1";
String encryptPassword = DigestUtils.md5DigestAsHex((sAlt + userPassword).getBytes());
//4.向数据库插入用户数据
// Yupi1 yupi1 = new Yupi1();实体对象才有方法的呀
// boolean saveResult = this.save(yupi1); 两个值,true false
// if (!saveResult) 失败了,返回 -1
Yupi1 yupi1 = new Yupi1();
yupi1.setUserAccount(userAccount);
yupi1.setUserPassword(encryptPassword);
boolean saveResult = this.save(yupi1);
if (!saveResult)
{
return -1;
}
return 0;
}
}
4.2 测试数据 密码,加密,不以明文显示出来
![](https://img-blog.csdnimg.cn/direct/ffc82a19c4b74ceab4e84225ea157259.png)
代码:
@Test
void testDigest() throws NoSuchAlgorithmException{
String newPassword = DigestUtils.md5DigestAsHex(("abcd" + "mypassword").getBytes());
System.out.println(newPassword);
}
运行效果:
![](https://img-blog.csdnimg.cn/direct/9b38034157b54211bdd56a1cc93cb68f.png)