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

springdata jpa使用Example快速實(shí)現(xiàn)動(dòng)態(tài)查詢功能

 更新時(shí)間:2021年11月18日 11:37:11   作者:一號(hào)搬磚手  
這篇文章主要介紹了springdata jpa使用Example快速實(shí)現(xiàn)動(dòng)態(tài)查詢功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Example官方介紹

Query by Example (QBE) is a user-friendly querying technique with a simple interface. It allows dynamic query creation and does not require to write queries containing field names. In fact, Query by Example does not require to write queries using store-specific query languages at all.

谷歌翻譯:

按例查詢(QBE)是一種用戶界面友好的查詢技術(shù)。 它允許動(dòng)態(tài)創(chuàng)建查詢,并且不需要編寫包含字段名稱的查詢。 實(shí)際上,按示例查詢不需要使用特定的數(shù)據(jù)庫(kù)的查詢語(yǔ)言來(lái)編寫查詢語(yǔ)句。

Example api的組成

  • Probe:含有對(duì)應(yīng)字段的實(shí)例對(duì)象。
  • ExampleMatcher:ExampleMatcher攜帶有關(guān)如何匹配特定字段的詳細(xì)信息,相當(dāng)于匹配條件。
  • Example:由Probe和ExampleMatcher組成,用于查詢。

限制

  • 屬性不支持嵌套或者分組約束,比如這樣的查詢 firstname = ?0 or (firstname = ?1 and lastname = ?2)
  • 靈活匹配只支持字符串類型,其他類型只支持精確匹配

Limitations

1. No support for nested/grouped property constraints like firstname = ?0 or (firstname = ?1 and lastname = ?2)

2. Only supports starts/contains/ends/regex matching for strings and exact matching for other property types

使用

創(chuàng)建實(shí)體映射:

@Entity
@Table(name="t_user")
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(name="username")
    private String username;
    @Column(name="password")
    private String password;
    @Column(name="email")
    private String email;
    @Column(name="phone")
    private String phone;
    @Column(name="address")
    private String address;
}

測(cè)試查詢

@Test
public void contextLoads() {
    User user = new User();
    user.setUsername("admin");
    Example<User> example = Example.of(user);
    List<User> list = userRepository.findAll(example);
    System.out.println(list);
}

打印的sql語(yǔ)句如下:

Hibernate: 
    select
        user0_.id as id1_0_,
        user0_.address as address2_0_,
        user0_.email as email3_0_,
        user0_.password as password4_0_,
        user0_.phone as phone5_0_,
        user0_.username as username6_0_ 
    from
        t_user user0_ 
    where
        user0_.username=?

可以發(fā)現(xiàn),試用Example查詢,默認(rèn)情況下會(huì)忽略空值,官方文檔也有說(shuō)明:

This is a simple domain object. You can use it to create an Example. By default, fields having null values are ignored, and strings are matched using the store specific defaults. Examples can be built by either using the of factory method or by using ExampleMatcher. Example is immutable.

在上面的測(cè)試之中,我們只是只是定義了Probe而沒(méi)有ExampleMatcher,是因?yàn)槟J(rèn)會(huì)不傳時(shí)會(huì)使用默認(rèn)的匹配器。點(diǎn)進(jìn)方法可以看到下面的代碼:

static <T> Example<T> of(T probe) {
    return new TypedExample(probe, ExampleMatcher.matching());
}
static ExampleMatcher matching() {
    return matchingAll();
}
static ExampleMatcher matchingAll() {
    return (new TypedExampleMatcher()).withMode(ExampleMatcher.MatchMode.ALL);
}

自定匹配器規(guī)則

@Test
public void contextLoads() {
    User user = new User();
    user.setUsername("y");
    user.setAddress("sh");
    user.setPassword("admin");
    ExampleMatcher matcher = ExampleMatcher.matching()
            .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.startsWith())//模糊查詢匹配開頭,即{username}%
            .withMatcher("address" ,ExampleMatcher.GenericPropertyMatchers.contains())//全部模糊查詢,即%{address}%
            .withIgnorePaths("password");//忽略字段,即不管password是什么值都不加入查詢條件
    Example<User> example = Example.of(user ,matcher);
    List<User> list = userRepository.findAll(example);
    System.out.println(list);
}

打印的sql語(yǔ)句如下:

select
    user0_.id as id1_0_,
    user0_.address as address2_0_,
    user0_.email as email3_0_,
    user0_.password as password4_0_,
    user0_.phone as phone5_0_,
    user0_.username as username6_0_ 
from
    t_user user0_ 
where
    (
        user0_.username like ?
    ) 
    and (
        user0_.address like ?
    )

參數(shù)如下:

2018-03-24 13:26:57.425 TRACE 5880 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [y%]
2018-03-24 13:26:57.425 TRACE 5880 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [%sh%]

補(bǔ)充

官方創(chuàng)建ExampleMatcher例子(1.8 lambda)

ExampleMatcher matcher = ExampleMatcher.matching()
  .withMatcher("firstname", match -> match.endsWith())
  .withMatcher("firstname", match -> match.startsWith());
}

StringMatcher 參數(shù)

Matching 生成的語(yǔ)句 說(shuō)明
DEFAULT (case-sensitive) firstname = ?0 默認(rèn)(大小寫敏感)
DEFAULT (case-insensitive) LOWER(firstname) = LOWER(?0) 默認(rèn)(忽略大小寫)
EXACT (case-sensitive) firstname = ?0 精確匹配(大小寫敏感)
EXACT (case-insensitive) LOWER(firstname) = LOWER(?0) 精確匹配(忽略大小寫)
STARTING (case-sensitive) firstname like ?0 + ‘%' 前綴匹配(大小寫敏感)
STARTING (case-insensitive) LOWER(firstname) like LOWER(?0) + ‘%' 前綴匹配(忽略大小寫)
ENDING (case-sensitive) firstname like ‘%' + ?0 后綴匹配(大小寫敏感)
ENDING (case-insensitive) LOWER(firstname) like ‘%' + LOWER(?0) 后綴匹配(忽略大小寫)
CONTAINING (case-sensitive) firstname like ‘%' + ?0 + ‘%' 模糊查詢(大小寫敏感)
CONTAINING (case-insensitive) LOWER(firstname) like ‘%' + LOWER(?0) + ‘%' 模糊查詢(忽略大小寫)

說(shuō)明:

1. 在默認(rèn)情況下(沒(méi)有調(diào)用withIgnoreCase())都是大小寫敏感的。

2. api之中還有個(gè)regex,但是我在mysql下測(cè)試報(bào)錯(cuò),不了解具體作用。

總結(jié)

通過(guò)在使用springdata jpa時(shí)可以通過(guò)Example來(lái)快速的實(shí)現(xiàn)動(dòng)態(tài)查詢,同時(shí)配合Pageable可以實(shí)現(xiàn)快速的分頁(yè)查詢功能。

對(duì)于非字符串屬性的只能精確匹配,比如想查詢?cè)谀硞€(gè)時(shí)間段內(nèi)注冊(cè)的用戶信息,就不能通過(guò)Example來(lái)查詢

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

相關(guān)文章

  • Spring Boot配置Thymeleaf(gradle)的簡(jiǎn)單使用

    Spring Boot配置Thymeleaf(gradle)的簡(jiǎn)單使用

    今天小編就為大家分享一篇關(guān)于Spring Boot配置Thymeleaf(gradle)的簡(jiǎn)單使用,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • IDEA 中使用 Big Data Tools 連接大數(shù)據(jù)組件

    IDEA 中使用 Big Data Tools 連接大數(shù)據(jù)組件

    本文主要介紹了IDEA 中使用 Big Data Tools 連接大數(shù)據(jù)組件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • Java8優(yōu)雅的字符串拼接工具類StringJoiner實(shí)例代碼

    Java8優(yōu)雅的字符串拼接工具類StringJoiner實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于Java8優(yōu)雅的字符串拼接工具類StringJoiner的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Java 判斷IP地址的合法性實(shí)例詳解

    Java 判斷IP地址的合法性實(shí)例詳解

    這篇文章主要介紹了Java 判斷IP地址的合法性實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Netty分布式抽象編碼器MessageToByteEncoder邏輯分析

    Netty分布式抽象編碼器MessageToByteEncoder邏輯分析

    這篇文章主要介紹了Netty分布式抽象編碼器MessageToByteEncoder的抽象邏輯分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • Spring核心IoC容器的依賴注入接口和層級(jí)包命名規(guī)范

    Spring核心IoC容器的依賴注入接口和層級(jí)包命名規(guī)范

    這篇文章主要介紹了Spring核心IoC容器的依賴注入接口和層級(jí)包命名規(guī)范,IOC又名控制反轉(zhuǎn),把對(duì)象創(chuàng)建和對(duì)象之間的調(diào)用過(guò)程,交給Spring進(jìn)行管理,目的是為了降低耦合度,需要的朋友可以參考下
    2023-05-05
  • SpringBoot整合Javamail實(shí)現(xiàn)郵件發(fā)送功能

    SpringBoot整合Javamail實(shí)現(xiàn)郵件發(fā)送功能

    郵件發(fā)送是一個(gè)很普遍的功能,springboot整合了相關(guān)的starter,本文給大家介紹了可以實(shí)現(xiàn)一個(gè)簡(jiǎn)單的郵件發(fā)送功能的實(shí)例,文中通過(guò)代碼給大家介紹的非常詳細(xì),感興趣的朋友可以參考下
    2023-12-12
  • 教你如何使用Java輸出各種形狀

    教你如何使用Java輸出各種形狀

    本文小編將向大家介紹的是如何利用Java輸出各種不同的形狀,本文一共介紹了七種有趣的形狀,感興趣的小伙伴趕快收藏起來(lái)吧
    2021-09-09
  • java新手入門——String類詳解

    java新手入門——String類詳解

    在java、C#中,String類是不可變的,對(duì)String類的任何改變,都是返回一個(gè)新的String類對(duì)象。string>是C++標(biāo)準(zhǔn)程序庫(kù)中的一個(gè)頭文件
    2021-06-06
  • 解決spring cloud gateway 獲取body內(nèi)容并修改的問(wèn)題

    解決spring cloud gateway 獲取body內(nèi)容并修改的問(wèn)題

    這篇文章主要介紹了解決spring cloud gateway 獲取body內(nèi)容并修改的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12

最新評(píng)論