自动清除JPA回购类时,空指针除外

时间:2018-12-23 18:44:38

标签: java spring spring-boot jpa nullpointerexception

我正在尝试自动连接我的jpa存储库。但是,它将自动连接为null。我正在关注tutorial!那表示扩展CrudRepository的接口将在春天由我为运行时创建。当然,我可以从头开始编写所有实现的方法。仍然,我想知道这些教程的spring base配置对我来说是否可行。

我尝试使用@EnableJPARepositories批注,但它没有用。

-- java.com.GMorgan.RateMyFriendv5
  -- Controller
    ...
  -- Entity
    ...
  -- Repository
    UserRepositroy
  -- Service
    UserService
  RateMYFreindv5Application

UserRepository

public interface UserRepository extends CrudRepository<User, UUID> {
        List<User> findByUsername(String username);
        List<User> findByEmail(String email);
}

UserService

@Slf4j
@Service
public class UserService {
    @Autowired
    private UserRepository repository;

    public boolean login(String username, String password) {
        List<User> userList = repository.findByUsername(username);
        boolean isSuccessful = userList.get(0).isPassword(password);
        log.info("Username: {} isSucessful: {}", username, isSuccessful);
        return isSuccessful;
    }

    public boolean signUp(String email, String username, String password) {
        if (userEmailExists(email) || userUsernameExists(username)) return false;
        User user = new User();
        user.setEmail(email);
        user.setUsername(username);
        user.setPassword(password);
        user.setRole(Role.USER);
        repository.save(user);
        log.info("User email: {} username: {}", email, username);
        return true;
    }

    private boolean userEmailExists(String email) {
        return !repository.findByEmail(email).isEmpty();
    }

    private boolean userUsernameExists(String username) {
        return !repository.findByUsername(username).isEmpty();
    }
}

RateMyFriend

@EnableJpaRepositories(repositoryBaseClass = UserRepository.class)
@SpringBootApplication(scanBasePackages = {"com.GMorgan.RateMyFriendv5.Service",
        "com.GMorgan.RateMyFriendv5.Controller"})
public class RateMyFriendv5Application {

    public static void main(String[] args) {
        SpringApplication.run(RateMyFriendv5Application.class, args);
    }

}

0 个答案:

没有答案