CRUDとページネーションを実装し、ネイティブSQLクエリ、クエリのための方法をベース命名規則springDataJPQL

まず、使用

図1に示すように、開口部は、この方法は、JPQLパラメータ、戻り値を提供し、JPQL CRUDテストのコメント文で記述され、クエリを受け入れる@query方法で注釈を追加するために、結果を使用して、DAOの方法で定義されています

2、ネイティブSQL文を使用して:DAOはコメントでネイティブSQLステートメントを追加し、注釈@queryプロセスを追加するためのメソッドを定義し、属性を追加します。nativeQuery = trueの場合、テスト

3.クエリを命名する方法は:

  ルールは、メソッドを定義し、確かにすることにより、フレーム自体は、メソッド名、ルールに従ってSQLクエリ文を生成することができます。

    1、findByで始まる必要があります

    2、フィールド属性名はエンティティクラスfindByが続いている問い合わせます

    図3は、複数の条件が存在する場合、エンティティクラスに続く属性名を追加する方法であり、

    4、メソッド定義されたクエリのパラメータに対応します

    図5に示すように、戻り値のデータ型定義の戻り値、ページネーションクエリは、メソッドのパラメータを追加する場合、ページング可能にします

二,,実装クラスを書きます

パッケージcn.zrf.jpa.entity。

輸入のjavax.persistence *。; 

@Entity 
@Table(名前= "cust_customer")
パブリッククラスカスタマー{ 
    //配置主键自增的策略
    @GeneratedValue(戦略= GenerationType.IDENTITYを)
    @Id 
    @Column(名前= "CUST_ID")
    プライベート長いCUSTID。
    @Column(名前= "CUST_NAME")
    プライベート文字列CUSTNAME。
    @Column(名前= "cust_source")
    プライベート文字列custSource。
    @Column(名前= "cust_indutry")
    プライベート文字列custIndutry。
    @Column(名前= "cust_level")
    プライベート文字列custLevel。
    @Column(名前= "CUST_ADDRESS") 
    プライベート文字列custAddress。
    @Column(名前= "cust_phone") 
    公共ボイドsetCustId(長いCUSTID){
    プライベート文字列custPhone。

    @Override 
    パブリック文字列のtoString(){ 
        戻り"顧客{" + 
                "CUSTID =" + CUSTID + 
                "CUSTNAME = '" + CUSTNAME + '\'' + 
                "custSource = '" + custSource + '\'' + 
                " 、custIndutry = ' "+ custIndutry + '\'' + 
                "、custLevel = ' "+ custLevel + '\'' + 
                "、custAddress = ' "+ custAddress + '\'' + 
                "、custPhone = '」+ custPhone + '\'」+ 
                '}'; 
    } 

    パブリックロングgetCustId(){ 
        CUSTIDを返します。
    }
    }

        this.custId = CUSTID。

    パブリック文字列getCustNameの(){ 
        CUSTNAMEを返します。
    } 

    公共ボイドsetCustName(文字列CUSTNAME){ 
        this.custName = CUSTNAME。
    } 

    パブリック文字列getCustSource(){ 
        custSourceを返します。
    } 

    公共ボイドsetCustSource(文字列custSource){ 
        this.custSource = custSource。
    } 

    パブリック文字列getCustIndutry(){ 
        custIndutryを返します。
    } 

    公共ボイドsetCustIndutry(文字列custIndutry){ 
        this.custIndutry = custIndutry。
    } 

    パブリック文字列getCustLevel(){ 
        custLevelを返します。
    } 

    公共ボイドsetCustLevel(文字列custLevel){ 
        this.custLevel = custLevel。
    } 

    パブリック文字列getCustAddress(){ 
        custAddressを返します。
    } 

    公共ボイドsetCustAddress(文字列custAddress){ 
        this.custAddress = custAddress。
    } 

    パブリック文字列getCustPhone(){ 
        custPhoneを返します。
    } 

    公共ボイドsetCustPhone(文字列custPhone){ 
        this.custPhone = custPhone。
    } 
}

  

第三に、書き込みDAO

パッケージcn.zrf.jpa.dao。

輸入cn.zrf.jpa.entity.Customer; 
輸入org.springframework.data.domain.Pageable; 
輸入org.springframework.data.jpa.repository.JpaRepository; 
輸入org.springframework.data.jpa.repository.JpaSpecificationExecutor。
輸入org.springframework.data.jpa.repository.Modifying。
輸入org.springframework.data.jpa.repository.Query。

輸入はjava.util.List; 

パブリックインターフェースCustomerDaoはJpaRepository <顧客、ロング>、JpaSpecificationExecutor <顧客> {延び
    //查询全部
    @query( "顧客から")
    のリストを<顧客> getAllCustomer(); 
    //查询全部并分页
    @query( "顧客から")
    一覧<顧客> 
    //条件查询(根据ID查询) 
    @query(「顧客からWHERE CUST_ID =?」)
    顧客にgetCustomerById(ロング上記ID)
    アドレス、名前に基づいて//ファジークエリ
    @query(「好きcustAddress顧客からのようCUSTNAMEおよび? ")
    一覧<顧客> getCustList(文字列のアドレス、文字列CUSTNAME); 
    ローカルIDに基づいて//更新操作
    @query(" ?? = CUST_NAME CUSTID = SET「)は、お客様の更新
    @Modifying 
    無効getUpdateById(文字列名、IDロング) ; //ネイティブSQL文のファジークエリ 
    ( "?WHERE CUST_NAME好きcust_customer SELECT *から"真の値=、= nativeQuery)@query 
    一覧<顧客> getCustomerListByNative(文字列名)。//メソッドの命名クエリ
    / ** 1はfindByの冒頭で使用されるべき
     フィールドfindByエンティティクラスに続いて* 2クエリの属性名
    
   
     * 3複数の条件がインクリメントされている場合及び方法は+属性名前付きエンティティ・クラス
     定義は、*パラメータ4対応する方法クエリは
     5返す返されるデータ型定義の値* 
     所望のページングクエリは方法ページング可能にパラメータを追加する場合* 6 
     * / 
    に基づいて// IDクエリ
    カスタマーfindByCustId(ロング上記IDを述べた)、
    名前、アドレスに基づいて//あいまいクエリ
    一覧<顧客> findByCustNameLikeAndCustAddressLike(文字列名、文字列のアドレス); 
    //ページングクエリ
    一覧<顧客> findByCustAddressLike(文字列のアドレス、ページング可能ページング可能); 
}

  

第四に、テストクラス

パッケージcn.jpa.test。

輸入cn.zrf.jpa.dao.CustomerDao; 
輸入cn.zrf.jpa.entity.Customer; 
輸入org.junit.Test; 
輸入org.junit.runner.RunWith; 
輸入org.springframework.beans.factory.annotation.Autowired; 
輸入org.springframework.data.domain.PageRequest; 
輸入org.springframework.test.annotation.Commit; 
輸入org.springframework.test.context.ContextConfiguration。
輸入org.springframework.test.context.junit4.SpringJUnit4ClassRunner。
輸入org.springframework.transaction.annotation.Transactional; 

輸入java.awt.print.Pageable。
輸入はjava.util.List; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration( "クラスパス:applicationContext.xmlを")
パブリッククラスTestJpql { 
    @Autowired 
    CustomerDao customerDao。
    //查询全部
    @Test 
    ます。public void getAllCustomerTest(){ 
        一覧<顧客>リスト= customerDao.getAllCustomer(); 
        {:(リストお客様の顧客)のため
            のSystem.out.println(顧客)。
        } 
    } 
    //查询全部并分页
    @Test 
    公共ボイドgetAllCustomerByPageTest(){ 
        リスト<顧客>リスト= customerDao.getAllCustomerByPage(新しいPageRequest(0,3))。
        {:(リストお客様の顧客)のため
            のSystem.out.println(顧客)。
        } 
    } 
    //根据ID查询
    @Test 
    公共ボイドgetCustomerByIdTest(){ 
        顧客顧客= customerDao.getCustomerById(1リットル)。
        System.out.println(顧客); 
    } 
    //ファジークエリ住所、名前
    @Test 
    ます。public void getCuseListTest(){ 
        一覧<顧客> CUSTLIST = customerDao.getCustList( "北京%%"、 "%ロング%"); 
        (顧客顧客:CUSTLIST)用{ 
            ;のSystem.out.println(顧客)
        } 
    } 
    // IDローカルに従って更新操作
    @Test 
    @Transactional 
    @Commit 
    公共ボイドgetUpdateByIdTest(){ 
        customerDao.getUpdateById( "張無忌"、3リットル)。
    } 
    //ネイティブSQL文あいまいクエリ
    @Test  
    ます。public void getCustomerByNativeTest(){
        一覧<顧客> = customerDao.getCustomerListByNativeリスト( "%ロング%"); 
        (お客様お客様のために:リスト){ 
            のSystem.out.println(顧客); 
        } 
    } 
    //試験方法の命名規則クエリ
    // IDクエリ
    @Test 
    公共ボイドfindByIdTest(){ 
        カスタマーカスタマーcustomerDao.findByCustId =(1L)
        のSystem.out.println(顧客); 
    } 
    //ファジークエリアドレス、名前
    @Test 
    公共ボイドfindByCustNameLikeAndAddressLike(){ 
        リスト<顧客> = customerDao.findByCustNameLikeAndCustAddressLikeリスト( "張%%" "北京%%"); 
        (顧客顧客のために:リスト){ 
            System.out.printlnは(顧客)
        } 
    } 
    //クエリベースのページングアドレス
    @Test 
    公共ボイドfindByCustAddressLike(){
        一覧<顧客>リスト= customerDao.findByCustAddressLike( "%京%"、新PageRequest(0、3)); 
        {:(リストお客様の顧客)のため
            のSystem.out.println(顧客)。
        } 
    } 
}

  

 

おすすめ

転載: www.cnblogs.com/zhangrongfei/p/11391177.html