您需要执行一个CustomRepository并添加一个带有本机查询的方法。
我就是这样做的:
- 创建自定义存储库:
公共接口OcorrenciaRepositoryCustom { List getStrings(列表值);}
- 实现您的自定义存储库:(执行的名称必须是原始存储库的名称,将Impl添加为后缀)。
公共类OcorrenciaRepositoryImpl实现OcorrenciaRepositoryCustom { @PersistenceContext私有EntityManager entityManager;@重载公共List getStrings(列表字符串){ StringBuilder sb =新StringBuilder();sb.append(“从soebm.ocorrencia o WHERE 1=1选择计数(o.id) ");如果(strings.get(0)!=null&StringUtils.isNotEmpty(strings.get(0){ sb.append(“和to_char(o.date,‘YYYY DD’) >= :dataInicio ");} Query =to_charif(strings.get(0)!=null &strings.get{ query.setParameter("dataInicio",strings.get(0));}返回query.getResultList();}
- 从主存储库扩展自定义存储库:
公共接口OcorrenciaRepository扩展JpaRepository、OcorrenciaRepositoryCustom { Ocorrencia findByPosto(Posto posto);}
- 现在,在服务中,您可以从主存储库调用新方法。
@自动配置的私有OcorrenciaRepository存储库;公共List findOcorrenciaCustom(String str) { List String=新的ArrayList() {add(DataInicio);};返回repository.getStrings(字符串);}
重要的是,自定义存储库位于JpaRepositories搜索的包下。
@EnableJpaRepositories("com.test.my.repository")
在本例中,我使用了Spring JPA1.9。在我的项目中效果很好。