Java Spring Boot无法从Oracle数据库获取正确的数据
我的代码如下:Java Spring Boot无法从Oracle数据库获取正确的数据,java,oracle,spring-boot,spring-data-jpa,Java,Oracle,Spring Boot,Spring Data Jpa,我的代码如下: 存储库: @Repository @Component public interface SearchInventoryRepository extends JpaRepository<Inventory, String>{ @Query(nativeQuery = true, value = "select * from ORACLE_DATA1") List<Inventory> findAllDatabases(); @Q
存储库:
@Repository
@Component
public interface SearchInventoryRepository extends JpaRepository<Inventory, String>{
@Query(nativeQuery = true, value = "select * from ORACLE_DATA1")
List<Inventory> findAllDatabases();
@Query(nativeQuery = true, value = "select count(*) from ORACLE_DATA1")
int getCount();
}
但是,在SpringAPI中,许多结果都是重复的,并且许多结果都无法获取。结果计数在SQL Developer中以及通过API获取时保持不变
我以前从未遇到过这样的问题。有人能帮忙吗?1)没有必要用@Repository
对扩展JpaRepository
2) 使用@Component
注释已经有@Repository
、@Service
或@Controller
注释的类是不正确的
@Component
只是将类标记为bean,其他组件集成了此功能
3) @Autowired
用于注入带注释类型的实例。这是不正确的:
@Autowired
public int getCount()
{
return searchInventoryRepository.getCount();
}
4) 您可以使用JpaRepository
提供的默认方法,而不是使用@Query
。例如:
searchInventoryRepository.findAll(); // already defined
及
我不知道为什么要使用本机查询,但JpaRepository扩展了PagingAndSortingRepository,PagingAndSortingRepository扩展了Crudepository,这提供了,我引用:
正在管理的实体类的复杂CRUD功能
例如:
public interface CrudRepository<T, ID extends Serializable>
extends Repository<T, ID> {
(1)
<S extends T> S save(S entity);
(2)
T findOne(ID primaryKey);
(3)
Iterable<T> findAll();
Long count();
(4)
void delete(T entity);
(5)
boolean exists(ID primaryKey);
(6)
// … more functionality omitted.
公共接口CrudRepository
扩展存储库{
(1)
S save(S实体);
(2)
T findOne(ID primaryKey);
(3)
Iterable findAll();
长计数();
(4)
无效删除(T实体);
(5)
存在布尔值(ID primaryKey);
(6)
//…省略了更多功能。
}
在现有的方法中,有两种方法可以满足您的需要。重新发明轮子是不好的
您可以从该链接获得更多信息
使用本机查询是否有特定的原因?这些查询非常简单,您应该能够使用
spring data
提供的默认方法count()
和findAll()
。另外,为什么您在两个不同的类中声明了相同的方法名?您可以直接调用父存储库本机查询。或者保留不同的名称,然后尝试调用,看看会发生什么。放弃findalldatabase
方法,使用findAll
作为JpaRepository
。这同样适用于您的计数,已经定义了一个count()
方法。我遵循了您的所有要点,但仍然遇到相同的问题。获取一行的多个条目,而许多行甚至没有被获取。还有什么建议吗?@shasha0607这些现成的方法应该适合您的用例。在不知道全部细节的情况下,很难推测可能的原因。您是否可以在Github中创建一个复制问题的样本回购协议?@shasha0607您确定“重复结果”确实是重复的吗?也许你的数据库里有两次。如果您像我上面所说的那样修改了代码,那么代码就没有问题。我已经检查了我的数据库,表中的所有条目都是唯一的。
@Autowired
public int getCount()
{
return searchInventoryRepository.getCount();
}
searchInventoryRepository.findAll(); // already defined
searchInventoryRepository.count(); // already defined
public interface CrudRepository<T, ID extends Serializable>
extends Repository<T, ID> {
(1)
<S extends T> S save(S entity);
(2)
T findOne(ID primaryKey);
(3)
Iterable<T> findAll();
Long count();
(4)
void delete(T entity);
(5)
boolean exists(ID primaryKey);
(6)
// … more functionality omitted.