【发布时间】:2020-06-21 08:54:57
【问题描述】:
我的代码如下:
存储库:
@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();
}
服务:
@Transactional
@Service
public class GetInventoryService {
@Autowired
private SearchInventoryRepository searchInventoryRepository;
public List<Inventory> findAllDatabases()
{
return searchInventoryRepository.findAllDatabases();
}
@Autowired
public int getCount()
{
return searchInventoryRepository.getCount();
}
}
控制器:
@RestController
@Component
public class GetInventoryController {
@Autowired
private GetInventoryService getInventoryService;
@CrossOrigin
@GetMapping("/getAll")
public List<Inventory> getAll()
{
return getInventoryService.findAllDatabases();
}
@CrossOrigin
@GetMapping("/getCount")
public int getCount()
{
return getInventoryService.getCount();
}
}
当我在 SQL developer 中运行以下查询时,它们会产生正确的结果:
select * from ORACLE_DATA1;
select count(*) from ORACLE_DATA1;
但是在spring api中,很多结果都是重复的,很多结果都没有取到。结果计数在 SQL Developer 中以及通过 API 获取时保持不变。
我以前从未遇到过这样的问题。有人可以帮忙吗?
【问题讨论】:
-
使用本机查询是否有特定原因?这些查询很简单,你应该可以使用
spring-data提供的默认方法count()和findAll() -
另外,为什么你在两个不同的类中声明了相同的方法名?您可以直接调用父存储库原生查询。或者保留不同的名字,然后试着打电话看看会发生什么。
-
放弃你的
findAllDatabases方法并使用findAll和JpaRepository一样。这同样适用于您的计数,已经定义了一个count()方法。
标签: java oracle spring-boot spring-data-jpa