JPA query学习

1.JPA query   统计 

@Query("select count(bo) from BaseOrder as bo where bo.gGameId=?1 and bo.gRoleId=?2 and bo.createdAt >= ?3 and bo.status=2")

    Integer countByGameIdAndRoleIdAndStartDate(String gameId, String roleId, Date startDate);


    @Query("select count(bo) from BaseOrder as bo where bo.gGameId=?1 and bo.gRoleId=?2 and bo.status=2")
    Integer countByGameIdAndRoleId(String gameId, String roleId);


    @Query("select new Map(sum(bo.orderAmount) as amount,bo.channel as channel,bo.gGameId as gameId,bo.tradeType as tradeType,bo.currency as currency) from BaseOrder as bo where bo.channel in(?1) and bo.createdAt between ?2 and ?3 and bo.status =2 group by bo.channel,bo.gGameId,bo.tradeType,bo.currency")

    List<Object[]> getTotalAmountByChannel(List<String> channel, Date startTime , Date endTime);

2.distinct

 @Query("select DISTINCT new com.kz.balance.response.RepSelection(b.gameName,b.gameCode)  from Bill b")
List<RepSelection> getGameList();

@Query("select DISTINCT new com.kz.balance.response.RepSelection(b.mstChannelName,b.mstChannelCode)  from Bill b")
List<RepSelection> getmstChannelList();

@Query("select DISTINCT new com.kz.balance.response.RepSelection(b.companyName,b.companyCode)  from Bill b")
List<RepSelection> getcompanyNameList();

@Query("select DISTINCT new com.kz.balance.response.RepSelection(b.payPartnerName,b.payPartnerCode)  from Bill b")
List<RepSelection> getpayTypeList();

3.多条件查询

public interface BillDao extends CrudRepository<Bill,Long>, JpaSpecificationExecutor<Bill> {


//获取账单(最新20条数据)
@Query("select b from Bill b order by b.billDate desc")
List<Bill> findAll(Pageable pageable);

-----------------------------------------------------------------------------------------

@Override
public List<Bill> getBillBySelection(ReqSelection selection) {

String billNo = selection.getBillNo();
if(billNo!=null && !billNo.equals("")) {
List<Bill> lists = new ArrayList<Bill>();
Bill b = billDao.findByBillNo(billNo);
lists.add(b);
return lists;
}
String checkNo = selection.getCheckNo();
if(checkNo!=null && !checkNo.equals("")) {
return billDao.findByCheckNo(checkNo);
}

if(selection.getBillDate()==null&&selection.getBillSate()==null&&selection.getCompanyCode()==null
&&selection.getGameCode()==null&&selection.getMstChannelCode()==null&&selection.getPayPartnerCode()==null) {
return billDao.findAll(new PageRequest(0, 5));
}

        return billDao.findAll(getWhereClause(selection));
}

private Specification<Bill> getWhereClause(ReqSelection selection){
       return new Specification<Bill>() {
           @Override
           public Predicate toPredicate(Root<Bill> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
               List<Predicate> predicates = new ArrayList<>();
               String billDate= selection.getBillDate();
               if(billDate != null && !billDate.equals("")){
                   predicates.add(criteriaBuilder.equal(root.get("billDate"), billDate));
               }
               List<String> billState= selection.getBillSate();
               if(billState != null && billState.size()>0){
                   predicates.add(root.get("billState").in(billState));
               }
               String mstChannelCode= selection.getMstChannelCode();
               if(mstChannelCode != null && !mstChannelCode.equals("")){
                   predicates.add(criteriaBuilder.equal(root.get("mstChannelCode"), mstChannelCode));
               }
               
               String payPartnerCode= selection.getPayPartnerCode();
               if(payPartnerCode != null && !payPartnerCode.equals("")){
                   predicates.add(criteriaBuilder.equal(root.get("payPartnerCode"), payPartnerCode));
               }
               String companyCode= selection.getCompanyCode();
               if(companyCode != null && !companyCode.equals("")){
                   predicates.add(criteriaBuilder.equal(root.get("companyCode"), companyCode));
               }
               String gameCode= selection.getGameCode();
               if(gameCode != null && !gameCode.equals("")){
                   predicates.add(criteriaBuilder.equal(root.get("gameCode"), gameCode));
               }
               Predicate[] pre = new Predicate[predicates.size()];
               return query.where(predicates.toArray(pre)).getRestriction();
           }
       };
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值