@Test @Transactional public void registerUser() { User user = new User(); user.setUsername("sunriseydy-test"); user.setEmail("syhthems@mail.sunriseydy.top"); user.setPassword(passwordEncoder.encode("syhthems")); user.setUserStatus(UserStatusEnum.VALIDE_STATUS); user = userService.registerUser(user); Assert.assertNotNull(user); log.info(user.toString()); user = userService.selectByUsername("sunriseydy"); Assert.assertNotNull(user); log.info(user.toString()); }
@Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public User registerUser(User newUser) { Assert.notNull(newUser, "注册失败，用户为空"); Assert.hasText(newUser.getUsername(), "注册失败，用户名为空"); Assert.hasText(newUser.getPassword(), "注册失败，用户密码为空"); Assert.isTrue(!this.existsWithUsername(newUser.getUsername()), "注册失败，用户名已存在"); newUser.setPassword(passwordEncoder.encode(newUser.getPassword())); if (super.insertSelective(newUser) == 0) { throw new ServiceException("sorry，注册失败"); } if (newUser == null || newUser.getUserId() == null) { throw new ServiceException("注册失败，无法获取用户id"); } Role newUserRole = roleService.selectByRoleCode(BaseConstants.DEFAULT_USER_ROLE_CODE); if (newUserRole == null || newUserRole.getRoleId() == null) { throw new ServiceException("注册失败，获取默认角色失败"); } UserRole newUserAndRole = new UserRole(); newUserAndRole.setUserId(newUser.getUserId()); newUserAndRole.setRoleId(newUserRole.getRoleId()); if (userRoleService.insertSelective(newUserAndRole) == 0) { throw new ServiceException("注册失败，新用户关联角色失败"); } return newUser; }
UserServiceImpl extends BaseServiceImpl<User> implements UserService { @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public User registerUser(User newUser) { Assert.notNull(newUser, "注册失败，用户为空"); Assert.hasText(newUser.getUsername(), "注册失败，用户名为空"); Assert.hasText(newUser.getPassword(), "注册失败，用户密码为空"); Assert.isTrue(!this.existsWithUsername(newUser.getUsername()), "注册失败，用户名已存在"); newUser.setPassword(passwordEncoder.encode(newUser.getPassword())); if (super.insertSelective(newUser) == 0) { throw new ServiceException("sorry，注册失败"); } if (newUser == null || newUser.getUserId() == null) { throw new ServiceException("注册失败，无法获取用户id"); } Role newUserRole = roleService.selectByRoleCode(BaseConstants.DEFAULT_USER_ROLE_CODE); if (newUserRole == null || newUserRole.getRoleId() == null) { throw new ServiceException("注册失败，获取默认角色失败"); } UserRole newUserAndRole = new UserRole(); newUserAndRole.setUserId(newUser.getUserId()); newUserAndRole.setRoleId(newUserRole.getRoleId()); if (userRoleService.insertSelective(newUserAndRole) == 0) { throw new ServiceException("注册失败，新用户关联角色失败"); } return newUser; } }
UserServiceImpl extends BaseServiceImpl<User> implements UserService { @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public User registerUser(User newUser) { Assert.notNull(newUser, "注册失败，用户为空"); Assert.hasText(newUser.getUsername(), "注册失败，用户名为空"); Assert.hasText(newUser.getPassword(), "注册失败，用户密码为空"); Assert.isTrue(!this.existsWithUsername(newUser.getUsername()), "注册失败，用户名已存在"); newUser.setPassword(passwordEncoder.encode(newUser.getPassword())); if (super.insertSelective(newUser) == 0) { throw new ServiceException("sorry，注册失败"); } if (newUser == null || newUser.getUserId() == null) { throw new ServiceException("注册失败，无法获取用户id"); } Role newUserRole = roleService.selectByRoleCode(BaseConstants.DEFAULT_USER_ROLE_CODE); if (newUserRole == null || newUserRole.getRoleId() == null) { throw new ServiceException("注册失败，获取默认角色失败"); } UserRole newUserAndRole = new UserRole(); newUserAndRole.setUserId(newUser.getUserId()); newUserAndRole.setRoleId(newUserRole.getRoleId()); if (userRoleService.insertSelective(newUserAndRole) == 0) { throw new ServiceException("注册失败，新用户关联角色失败"); } return newUser; }  }
UserServiceImpl extends BaseServiceImpl<User> implements UserService { @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public User registerUser(User newUser) { Assert.notNull(newUser, "注册失败，用户为空"); Assert.hasText(newUser.getUsername(), "注册失败，用户名为空"); Assert.hasText(newUser.getPassword(), "注册失败，用户密码为空"); Assert.isTrue(!this.existsWithUsername(newUser.getUsername()), "注册失败，用户名已存在"); newUser.setPassword(passwordEncoder.encode(newUser.getPassword())); if (super.insertSelective(newUser) == 0) { throw new ServiceException("sorry，注册失败"); } if (newUser == null || newUser.getUserId() == null) { throw new ServiceException("注册失败，无法获取用户id"); } Role newUserRole = roleService.selectByRoleCode(BaseConstants.DEFAULT_USER_ROLE_CODE); if (newUserRole == null || newUserRole.getRoleId() == null) { throw new ServiceException("注册失败，获取默认角色失败"); } UserRole newUserAndRole = new UserRole(); newUserAndRole.setUserId(newUser.getUserId()); newUserAndRole.setRoleId(newUserRole.getRoleId()); if (userRoleService.insertSelective(newUserAndRole) == 0) { throw new ServiceException("注册失败，新用户关联角色失败"); } return newUser; }  @Override Boolean existsWithUsername(String username); @Override User selectByUsername(String username); @Override User selectByEmail(String email); @Override Boolean existsWithEmail(String email); @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) User registerUser(User newUser); @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) @CacheEvict(cacheNames = "UserDetails", key = "'CustomUserDetailsServiceImpl:loadUserByUsername:(' + #user.getUsername() + ')'", cacheManager = "JDKCacheManager", condition = "#user != null && #user.getUsername() != null ") int updateLogin(User user); @Override @CacheEvict(cacheNames = "UserDetails", key = "'CustomUserDetailsServiceImpl:loadUserByUsername:(' + #username + ')'", condition = "#username != null", cacheManager = "JDKCacheManager") void userLogout(String username); }
UserServiceImpl extends BaseServiceImpl<User> implements UserService { @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public User registerUser(User newUser) { Assert.notNull(newUser, "注册失败，用户为空"); Assert.hasText(newUser.getUsername(), "注册失败，用户名为空"); Assert.hasText(newUser.getPassword(), "注册失败，用户密码为空"); Assert.isTrue(!this.existsWithUsername(newUser.getUsername()), "注册失败，用户名已存在"); newUser.setPassword(passwordEncoder.encode(newUser.getPassword())); if (super.insertSelective(newUser) == 0) { throw new ServiceException("sorry，注册失败"); } if (newUser == null || newUser.getUserId() == null) { throw new ServiceException("注册失败，无法获取用户id"); } Role newUserRole = roleService.selectByRoleCode(BaseConstants.DEFAULT_USER_ROLE_CODE); if (newUserRole == null || newUserRole.getRoleId() == null) { throw new ServiceException("注册失败，获取默认角色失败"); } UserRole newUserAndRole = new UserRole(); newUserAndRole.setUserId(newUser.getUserId()); newUserAndRole.setRoleId(newUserRole.getRoleId()); if (userRoleService.insertSelective(newUserAndRole) == 0) { throw new ServiceException("注册失败，新用户关联角色失败"); } return newUser; }  @Override Boolean existsWithUsername(String username); @Override User selectByUsername(String username); @Override User selectByEmail(String email); @Override Boolean existsWithEmail(String email); @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) User registerUser(User newUser); @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) @CacheEvict(cacheNames = "UserDetails", key = "'CustomUserDetailsServiceImpl:loadUserByUsername:(' + #user.getUsername() + ')'", cacheManager = "JDKCacheManager", condition = "#user != null && #user.getUsername() != null ") int updateLogin(User user); @Override @CacheEvict(cacheNames = "UserDetails", key = "'CustomUserDetailsServiceImpl:loadUserByUsername:(' + #username + ')'", condition = "#username != null", cacheManager = "JDKCacheManager") void userLogout(String username);  }