网上查了很多资料都是下面的方法,但是不适用于Oracle
@Query(value = "select * from xxx where if(?1 !='',x1=?1,1=1) and if(?2 !='',x2=?2,1=1)" +
"and if(?3 !='',x3=?3,1=1) ",nativeQuery = true)
List find(String X1,String X2,String X3);
————————————————
版权声明:本文为CSDN博主「小码蚁啊」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36802726/article/details/81208853
----------------------------------------------
以下是Oracle中的方法,在service层对条件做判定
String equityLever;
if (StringUtils.isEmpty(params.get("equityLevel"))){
equityLever = null;
}else {
equityLever = String.valueOf(params.get("equityLevel"));
}
return commodityRepository.findAllByLever(equityLever);
@Query(value = "SELECT t.equityType FROM Commodity t where t.isDelete = 0 " +
"and t.shelfStatus = 2 " +
"and t.equityLevel like concat(concat('%',nvl(?1,null)),'%') " +
"GROUP BY t.equityType")
List findAllByLever(@Param("equityLevel") String equityLevel);
或者 加上nativeQuery = true ,写原生sql
@Query(value = "SELECT t.equity_type FROM EQUITY_COMMODITY t where t.IS_DELETE = 0 " +
"and t.SHELF_STATUS = 2 " +
"and t.EQUITY_LEVEL like concat(concat('%',nvl(?1,null)),'%') " +
"GROUP BY t.EQUITY_TYPE",nativeQuery = true)
List findAllByLever(@Param("equityLevel") String equityLevel);
如果你要分页,可能要重写count语句,我这边遇到加上pageable对象时count语句有问题的时候,看下面的sql
@Query(value = "SELECT ID,CUST_ID,LEGAL_RIGHTS_INST_NBR,STATUS_CD,REQ_TYPE,ACCEPT_SEQ_NO,PROD_INST_ID,OPEN_STATUS,IS_OPEN,LAN_ID,IS_SETTLEMENT,MQ_INFO, CREATED_AT FROM EQUITY_REQUEST_LOG " +"WHERE 1=1 " +"AND CUST_ID = nvl(?1,null) " +"AND LEGAL_RIGHTS_INST_NBR = nvl(?2,null) " +"AND STATUS_CD = nvl(?3,null) " +"ORDER BY ?#{#pageable} ",countQuery = "SELECT count (ID) FROM EQUITY_REQUEST_LOG " +"WHERE 1=1 " +"AND CUST_ID = nvl(?1,'') " +"AND LEGAL_RIGHTS_INST_NBR = nvl(?2,null) " +"AND STATUS_CD = nvl(?3,null)",nativeQuery = true)Page findAllByParams(String custId,String legalRightsInstNbr,String statusCd, Pageable pageable);
这样写的限制是你的入参必须是是字符串类型的.我试过用number和date类型.但是为null的时候查询会报错数据类型异常.暂时没找到解决方案.希望大佬们可以一起探讨一下.
个人觉得 jpa 普通用用还是可以的.但是要到复杂sql 或者 自定义反馈对象的时候用起来,不如mybatis顺手.仅仅是个人感觉哈