通過JPA注解映射視圖的實體類 jpa 視圖 無主鍵 @Query注解的用法(Spring Data JPA) jpa 使用sql語句


參考: https://blog.csdn.net/qq465235530/article/details/68064074

https://www.cnblogs.com/zj0208/p/6008627.html

 

這里主要說一下怎么用jpa映射一個視圖的實體類,其實跟表映射一樣,就是需要添加一個空的主鍵id標識

 

package com.cf.bus.core.rs.template.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name = "v_sc_template") // 這個是視圖名稱
public class SupplyChainTemplate {
@Id // 添加一個空的id標識,因為jpa在映射實體是需要一個id,這個必須
@Column(name = "name")
private String name;
@Column(name = "industry")
private String industry;
@Column(name = "com_type")
private String comType;
@Column(name = "remarks")
private String remarks;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getIndustry() {
return industry;
}

public void setIndustry(String industry) {
this.industry = industry;
}

public String getComType() {
return comType;
}

public void setComType(String comType) {
this.comType = comType;
}

public String getRemarks() {
return remarks;
}

public void setRemarks(String remarks) {
this.remarks = remarks;
}
}


@id一定要有,否則會報構建entityManagerFactory異常。

---------------------
作者:C7BF
來源:CSDN
原文:https://blog.csdn.net/qq465235530/article/details/68064074?utm_source=copy
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

 

 

參考文章:http://www.tuicool.com/articles/jQJBNv

 

1. 一個使用@Query注解的簡單例子

@Query(value = "select name,author,price from Book b where b.price>?1 and b.price<?2")
List<Book> findByPriceRange(long price1, long price2);

 

2.  Like表達式

@Query(value = "select name,author,price from Book b where b.name like %:name%")
List<Book> findByNameMatch(@Param("name") String name);

 

3. 使用Native SQL Query

所謂本地查詢,就是使用原生的sql語句(根據數據庫的不同,在sql的語法或結構方面可能有所區別)進行查詢數據庫的操作。

@Query(value = "select * from book b where b.name=?1", nativeQuery = true)
List<Book> findByName(String name);

 

4. 使用@Param注解注入參數

@Query(value = "select name,author,price from Book b where b.name = :name AND b.author=:author AND b.price=:price")
List<Book> findByNamedParam(@Param("name") String name, @Param("author") String author,
        @Param("price") long price);

 

5. SPEL表達式(使用時請參考最后的補充說明)

   '#{#entityName}'值為'Book'對象對應的數據表名稱(book)。

public interface BookQueryRepositoryExample extends Repository<Book, Long>{

       @Query(value = "select * from #{#entityName} b where b.name=?1", nativeQuery = true)
       List<Book> findByName(String name);

}

 

6. 一個較完整的例子

復制代碼
public interface BookQueryRepositoryExample extends Repository<Book, Long> {
    @Query(value = "select * from Book b where b.name=?1", nativeQuery = true) 
    List<Book> findByName(String name);// 此方法sql將會報錯(java.lang.IllegalArgumentException),看出原因了嗎,若沒看出來,請看下一個例子

    @Query(value = "select name,author,price from Book b where b.price>?1 and b.price<?2")
    List<Book> findByPriceRange(long price1, long price2);

    @Query(value = "select name,author,price from Book b where b.name like %:name%")
    List<Book> findByNameMatch(@Param("name") String name);

    @Query(value = "select name,author,price from Book b where b.name = :name AND b.author=:author AND b.price=:price")
    List<Book> findByNamedParam(@Param("name") String name, @Param("author") String author,
            @Param("price") long price);

}
復制代碼

 

7.  解釋例6中錯誤的原因:

     因為指定了nativeQuery = true,即使用原生的sql語句查詢。使用java對象'Book'作為表名來查自然是不對的。只需將Book替換為表名book。

@Query(value = "select * from book b where b.name=?1", nativeQuery = true)
List<Book> findByName(String name);

 

 

補充說明(2017-01-12):

  有同學提出來了,例子5中用'#{#entityName}'為啥取不到值啊?

  先來說一說'#{#entityName}'到底是個啥。從字面來看,'#{#entityName}'不就是實體類的名稱么,對,他就是。

  實體類Book,使用@Entity注解后,spring會將實體類Book納入管理。默認'#{#entityName}'的值就是'Book'。

  但是如果使用了@Entity(name = "book")來注解實體類Book,此時'#{#entityName}'的值就變成了'book'。

  到此,事情就明了了,只需要在用@Entity來注解實體類時指定name為此實體類對應的表名。在原生sql語句中,就可以把'#{#entityName}'來作為數據表名使用。

  


免責聲明!

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



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