欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringData如何通過@Query注解支持JPA語句和原生SQL語句

 更新時間:2021年11月25日 16:13:59   作者:qq_36722039  
這篇文章主要介紹了SpringData如何通過@Query注解支持JPA語句和原生SQL語句,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

通過@Query注解支持JPA語句和原生SQL語句

在SpringData中們可是使用繼承接口直接按照規(guī)則寫方法名即可完成查詢的方法,不需要寫具體的實現(xiàn),但是這樣寫又是不能滿足我們的需求,比如子查詢,SpringData中提供了@Query注解可以讓我們寫JPA的語句和原生的SQL語句,那接下來看看怎么寫JPA的查詢語句和原生的SQL語句。

package com.springdata.study.repository; 
import java.util.List; 
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param; 
import com.springdata.study.entitys.Person;
 
//1.實際上Repository是一個口接口,沒有提供任何方法,是一個標記接口
//2.實現(xiàn)了Repository接口就會被spring IOC容器識別為Repository Bean
//  會被納入IOC容器中
//3.Repository接口也可以同@RepositoryDefinition 注解代替,效果是一樣的
//4.接口中的泛型:第一個是那個實體類的Repository,第二個是實體類的主鍵的類型
//@RepositoryDefinition(domainClass=Person.class,idClass=Integer.class)
 
/**
 * 在Repository接口中申明方法 1.申明方法需要符合一定的規(guī)范 2.查詢方法需要以 find | read | get開頭 3.涉及查詢條件時
 * 需要用條件關(guān)鍵字連接 4.屬性首字母大寫 5.支持級聯(lián)屬性
 * 6.AddressId若當前實體類中有屬性,則優(yōu)先使用該屬性,若想要使用級聯(lián)屬性,需要用下劃線隔開Address_Id
 */ 
public interface PersonRepositoiry extends Repository<Person, Integer> {
 // select p from Person where p.name = ?
 Person getByName(String name);
 
 List<Person> findByNameStartingWithAndIdLessThan(String name, Integer id);
 
 // where name like %? and id < ?
 List<Person> findByNameEndingWithAndIdLessThan(String name, Integer id);
 
 // where email in ? age < ?
 List<Person> readByEmailInOrAgeLessThan(List<String> emails, int age);
 
 // 級聯(lián)屬性查詢
 // where address.id > ?
 List<Person> findByAddress_IdGreaterThan(Integer is);
 
 // 可以使用@Query注解在其value屬性中寫JPA語句靈活查詢
 @Query("SELECT p FROM Person p WHERE p.id = (SELECT max(p2.id) FROM Person p2)")
 Person getMaxIdPerson();
 
 // 在@Query注解中使用占位符
 @Query(value = "SELECT p FROM Person p where p.name = ?1 and p.email = ?2")
 List<Person> queryAnnotationParam1(String name, String email);
 
 // 使用命名參數(shù)傳遞參數(shù)
 @Query(value = "SELECT p FROM Person p where p.name = :name")
 List<Person> queryAnnotationParam2(@Param("name") String name);
 
 // SpringData可以在參數(shù)上添加%
 @Query("SELECT p FROM Person p WHERE p.name LIKE %?1%")
 List<Person> queryAnnotationLikeParam(String name);
 
 // SpringData可以在參數(shù)上添加%
 @Query("SELECT p FROM Person p WHERE p.name LIKE %:name%")
 List<Person> queryAnnotationLikeParam2(@Param("name")String name);
 
 //在@Query注解中添加nativeQuery=true屬性可以使用原生的SQL查詢
 @Query(value="SELECT count(*) FROM jpa_person", nativeQuery=true)
 long getTotalRow(); 
}

下面是這個類的測試類

package com.springdata.study.test; 
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List; 
import javax.sql.DataSource; 
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.springdata.study.entitys.Person;
import com.springdata.study.repository.PersonRepositoiry;
import com.springdata.study.service.PersonService;
 
public class DataSourceTest { 
 private ApplicationContext applicationContext;
 private PersonService personService;
 private PersonRepositoiry personRepositoiry;
 
 {
  applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  personRepositoiry = applicationContext.getBean(PersonRepositoiry.class);
  personService = applicationContext.getBean(PersonService.class);
 }
 
 @SuppressWarnings("resource")
 @Test
 public void testDataSource() throws SQLException {
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  DataSource dataSource = applicationContext.getBean(DataSource.class);
  System.out.println(dataSource.getConnection());
 }
 
 @Test
 public void testSpringdata() {
  Object person = personService.getPerson("LQF");
  System.out.println(person);
 }
 
 @Test
 public void testFindByNameStartingWithAndIdLessThan() {
  List<Person> persons = personRepositoiry.findByNameStartingWithAndIdLessThan("g", 6);
  persons = personRepositoiry.findByNameEndingWithAndIdLessThan("g", 6);
  System.out.println(persons);
 }
 
 @Test
 public void testReadByEmailInOrAgeLessThan() {
  List<Person> persons = personRepositoiry.readByEmailInOrAgeLessThan(Arrays.asList("123@qq.com"), 25);
  System.out.println(persons);
 }
 
 @Test
 public void testFindByAddressIdGreaterThan() {
  personRepositoiry.findByAddress_IdGreaterThan(1);
 }
 
 @Test
 public void testGetMaxIdPerson() {
  Person person = personRepositoiry.getMaxIdPerson();
  System.out.println(person);
 }
 
 @Test
 public void testQueryAnnotationParam() {
  List<Person> persons = personRepositoiry.queryAnnotationParam1("liqingfeng", "123@qq.com");
  System.out.println(persons);
 }
 
 @Test
 public void testQueryAnnotationParam2() {
  List<Person> persons = personRepositoiry.queryAnnotationParam2("lqf");
  System.out.println(persons);
 }
 
 @Test
 public void testQueryAnnotationLikeParam() {
  List<Person> persons = personRepositoiry.queryAnnotationLikeParam2("li");
  System.out.println(persons);
 }
 
 @Test
 public void testGetTotalRow() {
  long count = personRepositoiry.getTotalRow();
  System.out.println(count);
 } 
}

@Query注解的用法(Spring Data JPA)

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語句(根據(jù)數(shù)據(jù)庫的不同,在sql的語法或結(jié)構(gòu)方面可能有所區(qū)別)進行查詢數(shù)據(jù)庫的操作。

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

4.使用@Param注解注入?yún)?shù)

@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'對象對應的數(shù)據(jù)表名稱(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);

補充說明:

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

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

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

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

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

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java多線程CountDownLatch的實現(xiàn)

    Java多線程CountDownLatch的實現(xiàn)

    本文主要介紹了Java多線程CountDownLatch的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 30w+數(shù)據(jù)使用RedisTemplate?pipeline空指針NullPointerException異常分析

    30w+數(shù)據(jù)使用RedisTemplate?pipeline空指針NullPointerException異常分析

    這篇文章主要為大家介紹了30w+數(shù)據(jù)使用RedisTemplate?pipeline空指針NullPointerException異常分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • 使用vscode搭建javaweb項目的詳細步驟

    使用vscode搭建javaweb項目的詳細步驟

    我個人是很喜歡VsCode的,開源免費、功能全面,所以為了方便,我把我?guī)缀跛械倪\行都集成到了VsCode上來,JavaWeb也不例外,下面這篇文章主要給大家介紹了關(guān)于使用vscode搭建javaweb項目的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • 詳談Array和ArrayList的區(qū)別與聯(lián)系

    詳談Array和ArrayList的區(qū)別與聯(lián)系

    下面小編就為大家?guī)硪黄斦凙rray和ArrayList的區(qū)別與聯(lián)系。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 完美解決Server?returned?HTTP?response?code:403?for?URL報錯問題

    完美解決Server?returned?HTTP?response?code:403?for?URL報錯問題

    在調(diào)用某個接口的時候,突然就遇到了Server?returned?HTTP?response?code:?403?for?URL報錯這個報錯,導致獲取不到接口的數(shù)據(jù),下面小編給大家分享解決Server?returned?HTTP?response?code:403?for?URL報錯問題,感興趣的朋友一起看看吧
    2023-03-03
  • Java微服務開發(fā)之Swagger詳解

    Java微服務開發(fā)之Swagger詳解

    Swagger 是一個規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風格的 Web 服務??傮w目標是使客戶端和文件系統(tǒng)作為服務器以同樣的速度來更新。文件的方法,參數(shù)和模型緊密集成到服務器端的代碼,允許API來始終保持同步
    2021-10-10
  • 一文搞懂MyBatis多數(shù)據(jù)源Starter實現(xiàn)

    一文搞懂MyBatis多數(shù)據(jù)源Starter實現(xiàn)

    本文將實現(xiàn)一個MyBatis的Springboot的Starter包,引用這個Starter包后,僅需要提供少量配置信息,就能夠完成MyBatis多數(shù)據(jù)源的初始化和使用,需要的小伙伴可以參考一下
    2023-04-04
  • 使用Java將字符串在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換

    使用Java將字符串在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換

    大家都知道在一些情況下,我們需要特殊的編碼格式,如:UTF-8,但是系統(tǒng)默認的編碼為ISO-8859-1,遇到這個問題,該如何對字符串進行兩個編碼的轉(zhuǎn)換呢,下面小編給大家分享下java中如何在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換,感興趣的朋友一起看看吧
    2021-12-12
  • Java實現(xiàn)表單提交(支持多文件同時上傳)

    Java實現(xiàn)表單提交(支持多文件同時上傳)

    本文介紹了Java、Android實現(xiàn)表單提交(支持多文件同時上傳)的方法,具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • Java之String[] args含義及作用

    Java之String[] args含義及作用

    這篇文章主要介紹了Java之String[] args含義及作用,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08

最新評論