JPA在不寫sql的情況下如何實現(xiàn)模糊查詢
背景介紹
在我們的項目中很多的業(yè)務都會設(shè)計模糊查詢,例如按照姓氏去獲取人員的信息,按照手機號的前三位去獲取人員的信息等。
我們除了正常的手寫模糊查詢的sql語句去獲取信息之外,還可以使用JPA自帶的API來實現(xiàn)任意字段的模糊查詢。
JPA已經(jīng)給我們封裝好了。當我們對模糊查詢非常熟悉了之后直接拿來時候即可。
概念說明
單字段模糊匹配
- 說明:在一個字段中無論關(guān)鍵字出現(xiàn)在什么位置上,只要有關(guān)鍵詞即可。
- 場景:獲取手機號開頭為187的學生
- 應用:
SELECT*FROM table_name WHERE BINARY column_name LIKE'%keyword%';
多字段模糊匹配:
- 說明:在多個字段中無論關(guān)鍵字出現(xiàn)在什么位置上,只要每個字段有每個字段指定的關(guān)鍵詞即可。
- 場景:獲取手機號開頭為187并且姓氏為武的學生
- 應用:
SELECT*FROM table_name WHERE BINARY column1_name LIKE'%keyword1%'AND column2_name LIKE'%keyword2%';
注:
- BINARY函數(shù)是開啟大小寫敏感的函數(shù),底層邏輯是通過Ascii碼的方式比較的。
- 因為數(shù)據(jù)庫默認是對大小寫不敏感的,也就是我們在查詢名稱為wzl數(shù)據(jù)的時候,也會把名稱為Wzl的數(shù)據(jù)也查詢出來。
實現(xiàn)過程
代碼實現(xiàn)
1.寫一個實體類去實現(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);
}
}本文中實現(xiàn)的是and方式的模糊查詢,也可是使用or的方式進行模糊查詢,也就是多個字段中都包含一個關(guān)鍵字。
2.定義一個接口去繼承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è)務類中調(diào)用聲明的接口
@Entity
public List<User> selectToFuzzy(User userInfo){
List<User> userInfoList = userDao.findAll(userInfo);
return userInfoList;
}4.在Controller中直接調(diào)用業(yè)務類中的方法即可
@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)⒍际菍侄沃档囊徊糠謨?nèi)容,phone字段傳入的是187它會把phone字段中包含187的所有數(shù)據(jù)都返回 回來。
同樣兩個字段一起模糊查詢也是一樣。
其他方式
1.使用JPQL進行模糊查詢
使用LIKE關(guān)鍵字結(jié)合通配符(%)進行模糊匹配。
例如:
SELECT e FROM Employee e WHERE e.name LIKE '%John%'
2.使用Spring Data JPA的Query By Example進行模糊查詢
創(chuàng)建一個實體對象作為查詢條件,設(shè)置需要模糊匹配的屬性值。
例如:
ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("name", match -> match.contains()); Example<Employee> example = Example.of(employee, matcher);3.使用Spring Data JPA的@Query注解進行模糊查詢
在Repository接口中使用@Query注解定義自定義的查詢方法。
在查詢方法中使用%通配符進行模糊匹配。
例如:
@Query("SELECT e FROM Employee e WHERE e.name LIKE %?1%") List<Employee> findByName(String name);總結(jié)提升
根據(jù)自己的業(yè)務需求去選擇使用哪一種模糊查詢的方式。底層原理都是一樣的。
JPA封裝了一些公共的內(nèi)容,我們開發(fā)的過程中使用起來就比較容易和簡單。
但是我們在使用的過程中也要明白底層是如何實現(xiàn),不能只停留在會使用的階段中。知其然也要知其所以然。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot vue完成發(fā)送接口請求顯示響應頭信息
這篇文章主要為大家介紹了springboot+vue完成發(fā)送接口請求顯示響應頭信息,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
關(guān)于 Java 的數(shù)據(jù)結(jié)構(gòu)鏈表
這篇文章主要介紹了關(guān)于 Java 的數(shù)據(jù)結(jié)構(gòu)鏈表的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容2021-09-09
java利用url實現(xiàn)網(wǎng)頁內(nèi)容的抓取
本文主要介紹了java利用url實現(xiàn)網(wǎng)頁內(nèi)容抓取的示例。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03

