java - 与存储库相关的方法仅返回空值
问题描述
我有一个 Spring Boot 应用程序,我在其中创建了一个实体、一个存储库和一个服务。
我通过事务将实体保存在数据库中,一切正常,我的数据库已按预期填充。另外,我应该提到我的数据库是在 PHPMyAdmin 中创建的。
我还创建了一个存储库,以便通过扩展 Crud 存储库从数据库中获取一些数据。我还有一个服务,它存储调用存储库的方法。
虽然,我没有返回任何方法(我的数据库不是空的)而且我不知道为什么。我也尝试为实体添加@EnableJpaRepositories 和@ComponentScan,但这不起作用。以下是我的课程:
实体(我不会把所有的 getter 和 setter 放在这里):
@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。谁能帮我提个建议?
解决方案
你的主课PicksApplication
很麻烦。该main
方法必须触发SpringApplication.run
spring boot 以初始化自身和 autowires 工作的上下文。您正在破坏代码中的所有内容。您可以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);
}
}
那么它应该可以工作,其余代码看起来还可以
推荐阅读
- docker - 在 Docker 运行命令中找不到 Pandas [附加卷]
- python - Counting words on multiple web pages at the same domain
- mysql - I cannot connect to my MySQL database in C with CLion
- python - 从 Jupyter 复制输出以粘贴到 Excel 中的简单方法
- linux - Reading and modifying data of syscall with ptrace
- angular - 等待来自父母的数据
- r - 根据特定条件在 dplyr 中过滤
- junit4 - 如何使用 jupiter Junit5 平台的老式引擎配置带有类别的 gradle 运行 JUnit4 测试
- python - 从特定轴嵌入查找
- java - 递归函数继续运行并且什么也不打印