与存储库相关的方法仅返回空值

时间:2019-10-01 21:51:21

标签: java spring spring-boot jpa

我有一个Spring Boot应用程序,在其中创建了一个实体,一个存储库和一个服务。

我通过事务将实体保存在数据库中,并且一切正常,我的数据库已按预期填充。另外,我应该提到我的数据库是在PHPMyAdmin中创建的。

我还创建了一个存储库,以通过扩展Crud存储库从数据库中获取一些数据。我还有一项服务,用于存储调用存储库的方法。

尽管如此,我没有任何方法返回任何值(我的数据库不为空),我也不知道为什么。我也尝试为实体添加@EnableJpaRepositories和@ComponentScan,但这没有用。以下是我的课程:

实体(我不会在这里放置所有的获取器和设置器):

@Entity
@Table(name = "matches", schema = "tennis", catalog = "")
public class MatchesEntity {
    private int id;
    private String namePlayer1;
    private String namePlayer2;
    private int setsPlayer1;
    private int setsPlayer2;
    private String odd1;
    private String odd2;
    private String competition;
    private String surface;
    private String status;

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "Name_player1")
    public String getNamePlayer1() {
        return namePlayer1;
    }

    public void setNamePlayer1(String namePlayer1) {
        this.namePlayer1 = namePlayer1;
    }

    @Basic
    @Column(name = "Name_player2")
    public String getNamePlayer2() {
        return namePlayer2;
    }

    // other getter & setters
}

存储库:

@Repository
public interface MatchesRepository extends CrudRepository<MatchesEntity, 
Integer> {

     List<MatchesEntity> getAllBySurface(String surface);

}

服务:

@Service
public class MatchesService {

@Autowired
MatchesRepository matchesRepository;

public int countMatchesOnHard() {
    return matchesRepository.getAllBySurface("hard").size();
}

public MatchesEntity findMatchById() {
    return matchesRepository.findById(2378).get();
}

}

主要类别:

 @SpringBootApplication
 @EnableJpaRepositories(basePackageClasses={MatchesRepository.class})
 @EntityScan(basePackageClasses=MatchesEntity.class)
 public class PicksApplication {

     @Autowired
     static MatchesService matchesService;

     public static void main(String[] args) {
         MatchesEntity matchesEntity = matchesService.findMatchById();
         int numberOfMatchesOnHard = matchesService.countMatchesOnHard();
         System.out.println(numberOfMatchesOnHard);
   }
}

我尝试与存储库相关的任何方法都返回null。 有人可以帮我一个建议吗?

1 个答案:

答案 0 :(得分:2)

您的主班PicksApplication很麻烦。 main方法必须触发SpringApplication.run才能使Spring Boot初始化自身,并触发自动装配的上下文。您正在破坏代码中的所有内容。您可以利用CommandLineRunner并以run()方法添加代码。

喜欢这个

@SpringBootApplication
public class PicksApplication implements CommandLineRunner {

    @Autowired
    private MatchesService matchesService;

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

    @Override
    public void run(String... args) throws Exception {
         MatchesEntity matchesEntity = matchesService.findMatchById();
         int numberOfMatchesOnHard = matchesService.countMatchesOnHard();
         System.out.println(numberOfMatchesOnHard);
    }
}

然后它应该可以工作,其余代码看起来还可以

相关问题