SpringData如何通過(guò)@Query注解支持JPA語(yǔ)句和原生SQL語(yǔ)句
通過(guò)@Query注解支持JPA語(yǔ)句和原生SQL語(yǔ)句
在SpringData中們可是使用繼承接口直接按照規(guī)則寫(xiě)方法名即可完成查詢(xún)的方法,不需要寫(xiě)具體的實(shí)現(xiàn),但是這樣寫(xiě)又是不能滿(mǎn)足我們的需求,比如子查詢(xún),SpringData中提供了@Query注解可以讓我們寫(xiě)JPA的語(yǔ)句和原生的SQL語(yǔ)句,那接下來(lái)看看怎么寫(xiě)JPA的查詢(xún)語(yǔ)句和原生的SQL語(yǔ)句。
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.實(shí)際上Repository是一個(gè)口接口,沒(méi)有提供任何方法,是一個(gè)標(biāo)記接口
//2.實(shí)現(xiàn)了Repository接口就會(huì)被spring IOC容器識(shí)別為Repository Bean
// 會(huì)被納入IOC容器中
//3.Repository接口也可以同@RepositoryDefinition 注解代替,效果是一樣的
//4.接口中的泛型:第一個(gè)是那個(gè)實(shí)體類(lèi)的Repository,第二個(gè)是實(shí)體類(lèi)的主鍵的類(lèi)型
//@RepositoryDefinition(domainClass=Person.class,idClass=Integer.class)
/**
* 在Repository接口中申明方法 1.申明方法需要符合一定的規(guī)范 2.查詢(xún)方法需要以 find | read | get開(kāi)頭 3.涉及查詢(xún)條件時(shí)
* 需要用條件關(guān)鍵字連接 4.屬性首字母大寫(xiě) 5.支持級(jí)聯(lián)屬性
* 6.AddressId若當(dāng)前實(shí)體類(lèi)中有屬性,則優(yōu)先使用該屬性,若想要使用級(jí)聯(lián)屬性,需要用下劃線隔開(kāi)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);
// 級(jí)聯(lián)屬性查詢(xún)
// where address.id > ?
List<Person> findByAddress_IdGreaterThan(Integer is);
// 可以使用@Query注解在其value屬性中寫(xiě)JPA語(yǔ)句靈活查詢(xún)
@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查詢(xún)
@Query(value="SELECT count(*) FROM jpa_person", nativeQuery=true)
long getTotalRow();
}
下面是這個(gè)類(lèi)的測(cè)試類(lèi)
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.一個(gè)使用@Query注解的簡(jiǎn)單例子
@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表達(dá)式
@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
所謂本地查詢(xún),就是使用原生的sql語(yǔ)句(根據(jù)數(shù)據(jù)庫(kù)的不同,在sql的語(yǔ)法或結(jié)構(gòu)方面可能有所區(qū)別)進(jìn)行查詢(xún)數(shù)據(jù)庫(kù)的操作。
@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表達(dá)式(使用時(shí)請(qǐng)參考最后的補(bǔ)充說(shuō)明)
'#{#entityName}'值為'Book'對(duì)象對(duì)應(yīng)的數(shù)據(jù)表名稱(chēng)(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.一個(gè)較完整的例子
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將會(huì)報(bào)錯(cuò)(java.lang.IllegalArgumentException),看出原因了嗎,若沒(méi)看出來(lái),請(qǐng)看下一個(gè)例子
@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中錯(cuò)誤的原因
因?yàn)橹付薾ativeQuery = true,即使用原生的sql語(yǔ)句查詢(xún)。使用java對(duì)象'Book'作為表名來(lái)查自然是不對(duì)的。只需將Book替換為表名book。
@Query(value = "select * from book b where b.name=?1", nativeQuery = true) List<Book> findByName(String name);
補(bǔ)充說(shuō)明:
有同學(xué)提出來(lái)了,例子5中用'#{#entityName}'為啥取不到值啊?
先來(lái)說(shuō)一說(shuō)'#{#entityName}'到底是個(gè)啥。從字面來(lái)看,'#{#entityName}'不就是實(shí)體類(lèi)的名稱(chēng)么,對(duì),他就是。
實(shí)體類(lèi)Book,使用@Entity注解后,spring會(huì)將實(shí)體類(lèi)Book納入管理。默認(rèn)'#{#entityName}'的值就是'Book'。
但是如果使用了@Entity(name = "book")來(lái)注解實(shí)體類(lèi)Book,此時(shí)'#{#entityName}'的值就變成了'book'。
到此,事情就明了了,只需要在用@Entity來(lái)注解實(shí)體類(lèi)時(shí)指定name為此實(shí)體類(lèi)對(duì)應(yīng)的表名。在原生sql語(yǔ)句中,就可以把'#{#entityName}'來(lái)作為數(shù)據(jù)表名使用。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java多線程CountDownLatch的實(shí)現(xiàn)
本文主要介紹了Java多線程CountDownLatch的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
30w+數(shù)據(jù)使用RedisTemplate?pipeline空指針NullPointerException異常分析
這篇文章主要為大家介紹了30w+數(shù)據(jù)使用RedisTemplate?pipeline空指針NullPointerException異常分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
使用vscode搭建javaweb項(xiàng)目的詳細(xì)步驟
我個(gè)人是很喜歡VsCode的,開(kāi)源免費(fèi)、功能全面,所以為了方便,我把我?guī)缀跛械倪\(yùn)行都集成到了VsCode上來(lái),JavaWeb也不例外,下面這篇文章主要給大家介紹了關(guān)于使用vscode搭建javaweb項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2022-11-11
詳談Array和ArrayList的區(qū)別與聯(lián)系
下面小編就為大家?guī)?lái)一篇詳談Array和ArrayList的區(qū)別與聯(lián)系。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
完美解決Server?returned?HTTP?response?code:403?for?URL報(bào)錯(cuò)問(wèn)題
在調(diào)用某個(gè)接口的時(shí)候,突然就遇到了Server?returned?HTTP?response?code:?403?for?URL報(bào)錯(cuò)這個(gè)報(bào)錯(cuò),導(dǎo)致獲取不到接口的數(shù)據(jù),下面小編給大家分享解決Server?returned?HTTP?response?code:403?for?URL報(bào)錯(cuò)問(wèn)題,感興趣的朋友一起看看吧2023-03-03
Java微服務(wù)開(kāi)發(fā)之Swagger詳解
Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)。總體目標(biāo)是使客戶(hù)端和文件系統(tǒng)作為服務(wù)器以同樣的速度來(lái)更新。文件的方法,參數(shù)和模型緊密集成到服務(wù)器端的代碼,允許API來(lái)始終保持同步2021-10-10
一文搞懂MyBatis多數(shù)據(jù)源Starter實(shí)現(xiàn)
本文將實(shí)現(xiàn)一個(gè)MyBatis的Springboot的Starter包,引用這個(gè)Starter包后,僅需要提供少量配置信息,就能夠完成MyBatis多數(shù)據(jù)源的初始化和使用,需要的小伙伴可以參考一下2023-04-04
使用Java將字符串在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換
大家都知道在一些情況下,我們需要特殊的編碼格式,如:UTF-8,但是系統(tǒng)默認(rèn)的編碼為ISO-8859-1,遇到這個(gè)問(wèn)題,該如何對(duì)字符串進(jìn)行兩個(gè)編碼的轉(zhuǎn)換呢,下面小編給大家分享下java中如何在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換,感興趣的朋友一起看看吧2021-12-12
Java實(shí)現(xiàn)表單提交(支持多文件同時(shí)上傳)
本文介紹了Java、Android實(shí)現(xiàn)表單提交(支持多文件同時(shí)上傳)的方法,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01

