JPA在不寫sql的情況下如何實(shí)現(xiàn)模糊查詢
背景介紹
在我們的項(xiàng)目中很多的業(yè)務(wù)都會(huì)設(shè)計(jì)模糊查詢,例如按照姓氏去獲取人員的信息,按照手機(jī)號(hào)的前三位去獲取人員的信息等。
我們除了正常的手寫模糊查詢的sql語(yǔ)句去獲取信息之外,還可以使用JPA自帶的API來(lái)實(shí)現(xiàn)任意字段的模糊查詢。
JPA已經(jīng)給我們封裝好了。當(dāng)我們對(duì)模糊查詢非常熟悉了之后直接拿來(lái)時(shí)候即可。
概念說(shuō)明
單字段模糊匹配
- 說(shuō)明:在一個(gè)字段中無(wú)論關(guān)鍵字出現(xiàn)在什么位置上,只要有關(guān)鍵詞即可。
- 場(chǎng)景:獲取手機(jī)號(hào)開頭為187的學(xué)生
- 應(yīng)用:
SELECT*FROM table_name WHERE BINARY column_name LIKE'%keyword%';
多字段模糊匹配:
- 說(shuō)明:在多個(gè)字段中無(wú)論關(guān)鍵字出現(xiàn)在什么位置上,只要每個(gè)字段有每個(gè)字段指定的關(guān)鍵詞即可。
- 場(chǎng)景:獲取手機(jī)號(hào)開頭為187并且姓氏為武的學(xué)生
- 應(yīng)用:
SELECT*FROM table_name WHERE BINARY column1_name LIKE'%keyword1%'AND column2_name LIKE'%keyword2%';
注:
- BINARY函數(shù)是開啟大小寫敏感的函數(shù),底層邏輯是通過Ascii碼的方式比較的。
- 因?yàn)閿?shù)據(jù)庫(kù)默認(rèn)是對(duì)大小寫不敏感的,也就是我們?cè)诓樵兠Q為wzl數(shù)據(jù)的時(shí)候,也會(huì)把名稱為Wzl的數(shù)據(jù)也查詢出來(lái)。
實(shí)現(xiàn)過程
代碼實(shí)現(xiàn)
1.寫一個(gè)實(shí)體類去實(shí)現(xiàn)Specification接口,重寫toPredicate方法
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.tfjybj.dao.UserDao;
import com.tfjybj.utils.SnowflakeIdWorker;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @BelongsProject: incentive
* @BelongsPackage: com.tfjybj.service
* @Author: Wuzilong
* @Description: 描述什么人干什么事兒
* @CreateTime: 2023-08-28 14:48
* @Version: 1.0
*/
@Table
@Entity
@Service
@Data
public class User implements Specification<User> {
@Id
@JsonSerialize(using = com.fasterxml.jackson.databind.ser.std.ToStringSerializer.class)
private Long id;
private String account;
private String password;
private String phone;
private Date createTime;
private Date updateTime;
private Integer isDelete;
@Resource
@Transient
private UserDao userDao;
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<String> nonNullFields = new ArrayList<>();
Field[] declaredFields = this.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
try {
Object value = field.get(this);
if (value != null) {
nonNullFields.add(field.getName());
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
Predicate[] predicates = new Predicate[nonNullFields.size()+1];
for (int i = 0; i < nonNullFields.size(); i++) {
try {
predicates[i] = criteriaBuilder.like(root.get(nonNullFields.get(i)), "%" + this.getClass().getDeclaredField(nonNullFields.get(i)).get(this) + "%");
} catch (Exception e) {
e.printStackTrace();
}
}
// 添加額外的條件,排除isdelete=1的數(shù)據(jù)
predicates[nonNullFields.size()] = criteriaBuilder.notEqual(root.get("isDelete"), 1);
return criteriaBuilder.and(predicates);
}
}本文中實(shí)現(xiàn)的是and方式的模糊查詢,也可是使用or的方式進(jìn)行模糊查詢,也就是多個(gè)字段中都包含一個(gè)關(guān)鍵字。
2.定義一個(gè)接口去繼承JpaRepository接口,并指定返回的類型和參數(shù)類型
@Entity
import com.tfjybj.service.User;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @BelongsProject: incentive
* @BelongsPackage: com.tfjybj.dao
* @Author: Wuzilong
* @Description: 描述什么人干什么事兒
* @CreateTime: 2023-08-28 14:48
* @Version: 1.0
*/
@Repository
public interface UserDao extends JpaRepository<User, Long> {
List<User> findAll(Specification<User> userInfo);
}3.在業(yè)務(wù)類中調(diào)用聲明的接口
@Entity
public List<User> selectToFuzzy(User userInfo){
List<User> userInfoList = userDao.findAll(userInfo);
return userInfoList;
}4.在Controller中直接調(diào)用業(yè)務(wù)類中的方法即可
@RequestMapping(value="selectToFuzzy",method= RequestMethod.POST)
//模糊查詢用戶的信息
public List<User> selectToFuzzy(@RequestBody User userInfo){
List<User> users = user.selectToFuzzy(userInfo);
return users;
}執(zhí)行結(jié)果


可以看到我們的入?yún)⒍际菍?duì)應(yīng)字段值的一部分內(nèi)容,phone字段傳入的是187它會(huì)把phone字段中包含187的所有數(shù)據(jù)都返回 回來(lái)。
同樣兩個(gè)字段一起模糊查詢也是一樣。
其他方式
1.使用JPQL進(jìn)行模糊查詢
使用LIKE關(guān)鍵字結(jié)合通配符(%)進(jìn)行模糊匹配。
例如:
SELECT e FROM Employee e WHERE e.name LIKE '%John%'
2.使用Spring Data JPA的Query By Example進(jìn)行模糊查詢
創(chuàng)建一個(gè)實(shí)體對(duì)象作為查詢條件,設(shè)置需要模糊匹配的屬性值。
例如:
ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("name", match -> match.contains()); Example<Employee> example = Example.of(employee, matcher);3.使用Spring Data JPA的@Query注解進(jìn)行模糊查詢
在Repository接口中使用@Query注解定義自定義的查詢方法。
在查詢方法中使用%通配符進(jìn)行模糊匹配。
例如:
@Query("SELECT e FROM Employee e WHERE e.name LIKE %?1%") List<Employee> findByName(String name);總結(jié)提升
根據(jù)自己的業(yè)務(wù)需求去選擇使用哪一種模糊查詢的方式。底層原理都是一樣的。
JPA封裝了一些公共的內(nèi)容,我們開發(fā)的過程中使用起來(lái)就比較容易和簡(jiǎn)單。
但是我們?cè)谑褂玫倪^程中也要明白底層是如何實(shí)現(xiàn),不能只停留在會(huì)使用的階段中。知其然也要知其所以然。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA中StackOverflowError錯(cuò)誤的解決
這篇文章主要介紹了JAVA中StackOverflowError錯(cuò)誤的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
IDEA中Mybatis的xml文件報(bào)錯(cuò)問題及解決
在IntelliJ IDEA中,MyBatis的XML文件報(bào)錯(cuò),提示'expected statement, got 'id'',通過找到并修改LanguageInjections的Mybatis相關(guān)配置,去掉最前面的sql前綴,禁用默認(rèn)配置文件,解決了每次重啟IDEA后配置失效的問題2025-12-12
MyBatis自定義攔截器實(shí)現(xiàn)優(yōu)化SQL日志輸出
這篇文章主要介紹了優(yōu)化MyBatisPlus SQL日志輸出的方案,針對(duì)默認(rèn)日志格式存在的不足,例如缺少時(shí)間、可讀性差、存儲(chǔ)成本高等,希望對(duì)大家有所幫助2026-03-03
詳解Spring中Bean的生命周期和作用域及實(shí)現(xiàn)方式
這篇文章主要給大家介紹了Spring中Bean的生命周期和作用域及實(shí)現(xiàn)方式的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03
Spring實(shí)現(xiàn)資源的動(dòng)態(tài)加載和卸載的方法小結(jié)
這篇文章主要介紹了Spring實(shí)現(xiàn)資源的動(dòng)態(tài)加載和卸載的方法小結(jié),文中通過代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06
mybatis-plus動(dòng)態(tài)表名的實(shí)現(xiàn)示例
這篇文章主要介紹了mybatis-plus動(dòng)態(tài)表名的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

