SpringJPA에 대한 동적 질의

4236 단어
최근 개발에서 만났던 조회 용례를 간단하게 기록해 두다
1. Repository @Repository public interface TransTask Manager extends JpaRepository, JpaSpecificationExecutor {}
2. Repository 쿼리 문
1, 객체를 매개 변수로 전송 @Modifying @Transactional @Query("update TransTask t set t.flow Status =: #{#transTask.flow Status = where t.flow Status =: #{#old Status}") int update Flow Status(@Param("transTask") TransTask, @Param("old Status")
2, 문자열은 매개 변수로 @Modifying@Transactional @Query("update TransTask t set t.transId =:transId where t.taskId =:taskId") int updateTransId(@Param("transId") String transId, @Param("taskId") String taskId)
3, 매개변수 위치 @Query("from TransTask t where t.taskId =? 1 and t.taskName like? 2 and t.flow Status = 100 and t.isDelete=2") Pagequery ByParams(@Param("taskId") String taskId, @Param("taskName") String taskName, Pageable pageable pageable);
4, 네이티브 sql@Query(value = "select t.play Url from ty trans task t where t.taskId =:taskId", nativeQuery = true) String getPlay Urls(@Param("taskId") String taskId)
5,Example_인스턴스 조회 Person person = new Person(),person.setFirstname("Dave");//Firstname = 'Dave'
ExampleMatcher matcher = ExampleMatcher.matching() .withMatcher ("name",GenericPropertyMatchers.startsWith ()//이름은 "일치 시작"방식으로 조회합니다.withIgnorePaths("int");//속성 무시: 주목할지 여부.기본 타입이기 때문에 무시해야 한다
Example example = Example.of(person, matcher);//Example 도메인 객체 및 구성에 따라 새 Example Matcher 작성
6, specification 쿼리
@Transactional(readOnly = true)
public Page query(TransTaskReq req) {

PageRequest pageRequest = new PageRequest(req.getPageNum(), req.getPageSize(), Sort.Direction.valueOf(req.getOrder()), req.getSort());

Specification specification = new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {

                List predicates = new ArrayList<>();

            
                // 
                if (StringUtils.isNotBlank(req.getTaskId())) {
                    predicates.add(cb.equal(root.get("taskId").as(String.class), req.getTaskId()));
                }

                // 
                if (StringUtils.isNotBlank(req.getTaskName())) {
                    predicates.add(cb.like(root.get("taskName").as(String.class), "%" + req.getTaskName() + "%"));
                }

        // in  
        Expression exp = root.get("id");//root.get("id").as(String.class)
                predicates.add(exp.in(noticeIds));
                
        // in  
        CriteriaBuilder.In in = cb.in(root.get("id"));
        in.value(" ");
        in.value(" ");
        predicates.add(in);
                
        //in 
                
        In in = cb.in(root.get(BailPay_.customerId));
        in.value(customer.id());
        predicates.add(in);
                     
                    
                    
        //or
        Predicate p = cb.equal(root.get("status").as(String.class), status);
        for (int i = 1; i < statuses.length; ++i) {
            p = cb.or(p, cb.equal(root.get("status").as(String.class), statuses[i]));
                }
        list.add(p);
                        
                        
        //or  
        Predicate p1=cb.like(root.get(“name”).as(String.class), “%”+uqm.getName()+“%”);
        Predicate p2=cb.equal(root.get("uuid").as(Integer.class), uqm.getUuid());
        Predicate p3=cb.gt(root.get("age").as(Integer.class), uqm.getAge());

         Predicate :
        Predicate p = cb.and(p3,cb.or(p1,p2));
                    
                Predicate[] pre = new Predicate[predicates.size()];
                return query.where(predicates.toArray(pre)).getRestriction();
            }
        };

        return transTaskRelExtManager.findAll(specification, pageRequest);
    }


    

좋은 웹페이지 즐겨찾기