Search in sources :

Example 1 with ServiceException

use of com.jxys.base.exception.ServiceException in project springboot-scaffold by a241978181.

the class UserServiceImpl method signUp.

@Override
public String signUp(SignUpData data) {
    // 创建User对象
    User user = new User(data);
    try {
        // 加密密码
        user.setPassword(EncryptConfigUtil.encyptPwd(this.encryptorPassword, user.getPassword()));
        // 尝试创建用户
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", user.getName());
        User u = this.userMapper.selectOne(queryWrapper);
        if (u != null) {
            return u.getName() + "用户已存在,请修改用户名后重新创建!";
        }
        // 建议使用索引约束来判断用户名是否存在,用户存在时会抛出异常,可以自行捕获数据库的异常,并返回用户已存在的错误提示
        userMapper.insertOneUser(user);
    } catch (Exception e) {
        logger.error("用户创建失败", e);
        throw new ServiceException("用户创建失败");
    }
    return "用户创建成功";
}
Also used : User(com.jxys.entity.User) ErrorServiceException(com.jxys.base.exception.ErrorServiceException) ServiceException(com.jxys.base.exception.ServiceException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) ErrorServiceException(com.jxys.base.exception.ErrorServiceException) ServiceException(com.jxys.base.exception.ServiceException) UserNotFoundException(com.jxys.user.exception.UserNotFoundException) PasswordErrorException(com.jxys.user.exception.PasswordErrorException)

Aggregations

QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)1 ErrorServiceException (com.jxys.base.exception.ErrorServiceException)1 ServiceException (com.jxys.base.exception.ServiceException)1 User (com.jxys.entity.User)1 PasswordErrorException (com.jxys.user.exception.PasswordErrorException)1 UserNotFoundException (com.jxys.user.exception.UserNotFoundException)1