一、新增

二、修改(可适当调整,利用BeanUtil.copy)

  • 2.1
public Optional<UserDTO> updateUser(UserDTO userDTO) {
   
        return Optional.of(userRepository
                .findById(userDTO.getId()))
                .filter(Optional::isPresent)
                .map(Optional::get)
                .map(user -> {
   
                    this.clearUserCaches(user);
                    user.setLogin(userDTO.getLogin().toLowerCase());
                    user.setFirstName(userDTO.getFirstName());
                    user.setLastName(userDTO.getLastName());
                    user.setEmail(userDTO.getEmail().toLowerCase());
                    user.setImageUrl(userDTO.getImageUrl());
                    user.setActivated(userDTO.isActivated());
                    user.setLangKey(userDTO.getLangKey());
                    Set<String> managedAuthorities = user.getAuthorities();
                    managedAuthorities.clear();
                    this.clearUserCaches(user);
                    log.debug("Changed Information for User: {}", user);
                    return user;
                })
                .map(user1 -> new UserDTO(user1));
    }
  • 2.2
public void changePassword(String currentClearTextPassword, String newPassword) {
   
        SecurityUtils.getCurrentUserLogin()
            .flatMap(userRepository::findOneByLogin)
            .ifPresent(user -> {
   
                String currentEncryptedPassword = user.getPassword();
                if (!passwordEncoder.matches(currentClearTextPassword, currentEncryptedPassword)) {
   
                    throw new InvalidPasswordException();
                }
                String encryptedPassword = passwordEncoder.encode(newPassword);
                user.setPassword(encryptedPassword);
                this.clearUserCaches(user);
                log.debug("Changed password for User: {}", user);
            });
    }

三、删除

  • 3.1 方式1
  public void deleteUser(String login) {
   
        userRepository.findOneByLogin(login).ifPresent(user -> {
   
            userRepository.delete(user);
            this.clearUserCaches(user);
            log.debug("Deleted User: {}", user);
        });
    }
  • 3.2 方式2
    @Scheduled(cron = "0 0 1 * * ?")
    public void removeNotActivatedUsers() {
   
    userRepository   
       .findAllByActivatedIsFalseAndActivationKeyIsNotNullAndCreatedDateBefore(Instant.now().minus(3, ChronoUnit.DAYS))
                .forEach(user -> {
   
                    log.debug("Deleting not activated user {}", user.getLogin());
                    userRepository.delete(user);
                    this.clearUserCaches(user);
                });
    }
   public void delete(List<Long> roleIds) {
   
        roleRepository.findAllById(roleIds)
                .forEach(role -> {
   
                    roleRepository.delete(role);
                });
    }

四、查询,需要在构造对象DTO的时候,合适的构造方法即可

  • 4.1
@Transactional(readOnly = true)
    public Page<UserDTO> getAllManagedUsers(Pageable pageable) {
   
        return userRepository.findAllByLoginNot(pageable, Constants.ANONYMOUS_USER).map(UserDTO::new);
    }
  • 4.2
 public Optional<AuditEvent> find(Long id) {
        return Optional.ofNullable(persistenceAuditEventRepository.findById(id))
            .filter(Optional::isPresent)
            .map(Optional::get)
            .map(auditEventConverter::convertToAuditEvent);
    }