我想使用spring数据存储库接口(interface)来执行 native 查询-我认为这种方式是最简单的,因为复杂度低。
但是在扩展接口(interface)时。 CrudRepository<T, ID>
我需要写 T - 我的实体,它不可用。
我的 native 查询不返回任何具体实体,那么在没有实体的情况下创建 spring 存储库的最佳方法是什么?
最佳答案
CrudRepository
或 JpaRepository
没有 <Entity,ID>
就无法工作一对。
你最好创建一个自定义仓库,注入(inject) EntityManager 并从那里查询:
@Repository
public class CustomNativeRepositoryImpl implements CustomNativeRepository {
@Autowired
private EntityManager entityManager;
@Override
public Object runNativeQuery() {
entityManager.createNativeQuery("myNativeQuery")
.getSingleResult();
}
}
关于java - 创建没有实体的spring存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55513776/