Spring Data JPA中@Query多个参数为空处理方式
三种方式解决
一、使用:name @Query(value="SELECT c.byname as byname, c.cart as cart,c.phone as phone,c.surname as surname, c.id as id,c.update_time as updateTime,c.head_img as headImg,c.is_blacklist as isBlacklist,c.is_member as isMember,"
+ "c.open_id as openid,c.take_address as takeAddress, c.pay_money as payMoney, "
+ " SUM(culog.pay_money) AS countPayMoney FROM cuser c "
+ "LEFT JOIN cuser_money_log culog ON c.id = culog.cuser_id AND culog.is_pay = 1 "
+ "WHERE IF (:byname is not null, c.byname LIKE CONCAT('%',:byname,'%') , 1 = 1) and IF (:isMember is not null, c.is_member = :isMember , 1 = 1) and IF (:isBlacklist is not null, c.is_blacklist = :isBlacklist , 1 = 1) and "
+ "IF (:phone is not null, c.phone = :phone , 1 = 1)"
+ "GROUP BY c.id LIMIT :PageOne,:PageSize",nativeQuery=true)
List<Map<String, Object>> countByQuery(@Param("byname") String byname,@Param("isMember") Integer isMember,@Param("isBlacklist") Integer isBlacklist,@Param("phone") String phone,@Param("PageOne") Integer PageOne, @Param("PageSize")Integer PageSize); 转载自:https://www.cnblogs.com/laixin09/p/9776868.html
二、使用1,2,3方式
// service层
public Page<SysUserDTO> findByUsernameAndOrgName(String username, String orgName, Pageable pageable){
String name = (username==null)?null:"%"+username+"%";
String orgname = (orgName==null)?null:"%"+orgName+"%";
return sysUserDAO.findByUsernameAndOrgName(name,orgname,pageable);
//jpa 多对多关系的表联合查询 DAO层
@Query(value = "select s from SysUserDTO s left join s.sysOrgDTOSet o where (?1 is null or s.username like ?1) and (?2 is null or o.name like ?2)")
Page<SysUserDTO> findByUsernameAndOrgName(String username, String orgName, Pageable pageable);
三:一二结合
@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<XXX> find(String X1,String X2,String X3);
说明:xxx是数据库表名,x1、x2、x3为查询的字段名。
下面的大写的XXX是实体类的名,X1X2X3为查询的参数。
if(?1 !='',x1=?1,1=1) 代表传入的参数X1如果不为""(Spring类型空是""而不是null)将参数传入x1,如果为空时显示1=1 代表参数为真,对查询结果不产生作用。
转载自:https://blog.csdn.net/qq_36802726/article/details/81208853
案例
jpa提供了@Query注解,可以通过自定义原生的sql来查询
public interface AbcRepository extends JpaRepository<Abc, String>, JpaSpecificationExecutor<Abc> { @Query(value = "SELECT a.* FROM abc" + "INNER JOIN (SELECT script_name,MAX(date_created) 'date_created' FROM abc" + "GROUP BY script_name) b " + "ON a.script_name = b.script_name " + "AND a.date_created = b.date_created " + "AND IF(ifnull(?1,'') != '',a.script_name LIKE CONCAT('%',?1,'%'),1=1) " + "AND IF(ifnull(?2,'') != '',a.script_status LIKE CONCAT('%',?2,'%'),1=1) " + "AND IF(ifnull(?3,'') != '',a.script_flag LIKE CONCAT('%',?3,'%'),1=1) " + "AND IF(ifnull(?4,'') != '',a.level_one=?4,1=1) order by ?#{#pageable}", countQuery = "SELECT a.* FROM job_script a " + "INNER JOIN (SELECT script_name,MAX(date_created) 'date_created' FROM job_script " + "GROUP BY script_name) b " + "ON a.script_name = b.script_name " + "AND a.date_created = b.date_created " + "AND IF(ifnull(?1,'') != '',a.script_name LIKE CONCAT('%',?1,'%'),1=1)" + "AND IF(ifnull(?2,'') != '',a.script_status LIKE CONCAT('%',?2,'%'),1=1) " + "AND IF(ifnull(?3,'') != '',a.script_flag LIKE CONCAT('%',?3,'%'),1=1) " + "AND IF(ifnull(?4,'') != '',a.level_one=?4,1=1)", nativeQuery = true) Page<abc> findJobScriptsByConditons(String scriptName, String scriptStatus, String scriptFlag, String levelOne, Pageable pageable); }
相关链接:https://blog.csdn.net/aust_zyl/article/details/94405977?
posted on 2020-09-22 10:12 Steven5007 阅读(4783) 评论(0) 编辑 收藏 举报