Spring Boot无法从Oracle数据库中获取正确的数据

时间:2020-03-09 12:55:59

标签: 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();

    @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提取时均保持不变。

我以前从未遇到过这样的问题。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

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

searchInventoryRepository.count(); // already defined

答案 1 :(得分:1)

我不知道您为什么使用本机查询,但是JpaRepository扩展了PagingAndSortingRepository,而PagingAndSortingRepository扩展了CrudRepository,这提供了,我引用:

sophisticated CRUD functionality for the entity class that is being managed

示例:

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.

}

在现有方法中,有两种可以满足您的需要。重新发明轮子是不好的。

您可以从此链接获取更多信息 https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html