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?

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM