Spring-Boot-JPA, @Query(jpql 문구), Repository

4804 단어 다오층
package com.demo.jpa_query_test.respository;

import com.demo.jpa_query_test.model.Stu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.jdbc.core.JdbcTemplate;

import java.awt.print.Pageable;
import java.util.List;
/**
 *           
 *           
 */
public interface StuRespository extends JpaRepository {
    //        jdbcTemplate         studao     jdbcTemplate StuRespository     


    @Override
     S save(S s);

    @Override
    List findAll();

    //     ,               id
    //inner join      ,       ,   IClass      ,        bull
    //        s.iclass c        ,        
    @Query(value = "select  s from Stu  s inner join s.iclass c where c.id = :size ")
    List findByCarSize(@Param(value = "size") Integer size);

    //     ,               id
    //left join      ,      ,   IClass    null
    //        s.iclass c        ,        
    @Query(value = "select  s from Stu  s left join s.iclass c where c.id = :size ")
    List findByCarSize2(@Param(value = "size") Integer size);



    /**    hibernate  
     * Hibernate: select stu0_.id as id1_2_, stu0_.c_id as c_id3_2_, stu0_.name as name2_2_ from stu stu0_ where (select count(carlist1_.stu_id) from sut_car_relation carlist1_ where stu0_.id=carlist1_.stu_id)>=?
     * //     ,   sql     +         +   
     *     //jpql  size() concate()     
     *     //         2   
     */
    @Query(value = "select  s from Stu  s where size(s.carList)>= :size ")
    List findByCarSize3(@Param(value = "size") Integer size);

    /**
     *      sql                ,        
     *          stu_id            c       (          id)              
     *
     * @return
     */
    @Query(value = " select s.* from stu s ,(select sc.stu_id ,count(sc.stu_id) as coun_t from sut_car_relation sc group  by sc.stu_id) as c where s.id = c.stu_id order by c.coun_t asc" ,nativeQuery = true)
    List findByCarSize4();

    /**
     *   jpql            is null   null   is empty               
     * @return
     */
    @Query(value = "select  s from Stu  s  where  s.carList is empty ")
    List findByCarSize6();
    /**
     *   jpql            
     * @return
     */
    @Query(value = "select  s from Stu  s  order by size(s.carList) desc")
    List findByCarSize5();
    /**
     * spring boot  
     *          
     *       Pageable pageable = new PageRequest(currentPage, pagesize, Sort.Direction.DESC, "keywords");
     *       currentPage 0   
     */
    @Query(value = "from Stu  s  where  1=1")
    List findByIPage_w();

    /**
     *        page         。。     
     * //  spring boot 2.0 pageable     ,   
     * @Param on all parameters except Pageable and Sort typed once, or none at all_。。
     * @param name
     * @param pageable
     * @return
     */
    @Query(value = "select s from Stu s where s.name = :name")
    Page findAllTest1(@Param(value = "name") String name,org.springframework.data.domain.Pageable pageable);

    /**
     * jpql        ,?1    :name   ,        
     *
     */


    @Query(value = "from Stu  s  where s.name = ?1 and s.id = ?2")
    Stu findByNameAndId(String name,String id);

    /***
     *   sql
     *    sql
     * IF( expr1 , expr2 , expr3 )  expr1     TRUE,      expr2 ,expr1    FALSE,      expr3
     *      if(/"/"=:name,1=1, s.name like concat('%',:name,'%'))
     *        if(:stae is null,1=1, s.state = :state)
     *
     *  @Query(value = "select * from  tag_info T  where (1=1  and if(:tagState is null ,1=1,T.tag_state=:tagState)  and T.tag_type in :tagType and if(\"\"=:content,1=1,T.name like CONCAT('%',:content,'%'))) or (1=1 and T.id in :ids) order by :orderFiled :px limit :start , :ed", nativeQuery = true)
     *
     * IFNULL( expr1 , expr2 )     expr1      NULL        expr1,     expr2
     */
    //   sql    ,         ,Integer Boolean        ""    null
    //  in       ,            ,in ()    ,         ,   ,              ,        ,                

    /**
     * jpql      sql      
     */


  //         @Modifying
    @Query("delete from Stu s where  s.name = ?1")
    @Modifying
    void delByName(String name);


}

좋은 웹페이지 즐겨찾기