java创建没有实体的spring存储库
我想使用spring数据存储库接口来执行本机查询——我认为这种方式最简单,因为复杂性较低
但是当扩展接口ex.CrudRepository<T, ID>
时,我需要编写T-my实体,这是不可用的
我的本机查询不会返回任何具体的实体,那么创建没有实体的spring存储库的最佳方法是什么
你可以在下面搜索框中键入要查询的问题!
我想使用spring数据存储库接口来执行本机查询——我认为这种方式最简单,因为复杂性较低
但是当扩展接口ex.CrudRepository<T, ID>
时,我需要编写T-my实体,这是不可用的
我的本机查询不会返回任何具体的实体,那么创建没有实体的spring存储库的最佳方法是什么
# 1 楼答案
CrudRepository
或JpaRepository
在没有<Entity,ID>
对的情况下不能工作您最好创建一个定制的回购协议,从中插入EntityManager和查询:
# 2 楼答案
您可以使用
@Repository
注释您的实现,并获得EntityManager的实例# 3 楼答案
如果您使用JPA,则需要实体。与前面的答案一样,您可以直接从EntityManager创建NativeQuery或使用Criteria API
有关自定义报告和常见回购行为的一些文档:
https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html#repositories.custom-behaviour-for-all-repositories
# 4 楼答案
我认为,当您没有用于本机查询结果集的具体实体类时,可以考虑使用JdbcTemplate。使用JdbcTemplate查询数据需要resultset的POJO类和实现POJO类的RowMapper接口的映射器
# 5 楼答案
目前,JPA中没有只使用本机或JPQL/HQL查询(使用@Query表示法)创建存储库的功能。要解决此问题,您可以创建一个虚拟对象以插入扩展接口,如下所示:
# 6 楼答案
这对我们有用。请参阅实体管理器
https://www.baeldung.com/hibernate-entitymanager
顺便说一句,我认为这里甚至不需要@Repository注释