甘肅網(wǎng)站建設(shè)開(kāi)發(fā)百度大搜推廣開(kāi)戶(hù)
目錄
- 背景介紹
- 概念說(shuō)明
- 單字段模糊匹配:
- 多字段模糊匹配:
- 實(shí)現(xiàn)過(guò)程
- 代碼實(shí)現(xiàn)
- 1.寫(xiě)一個(gè)實(shí)體類(lèi)去實(shí)現(xiàn)Specification接口,重寫(xiě)toPredicate方法
- 2.定義一個(gè)接口去繼承JpaRepository接口,并指定返回的類(lèi)型和參數(shù)類(lèi)型
- 3.在業(yè)務(wù)類(lèi)中調(diào)用聲明的接口
- 4.在Controller中直接調(diào)用業(yè)務(wù)類(lèi)中的方法即可
- 執(zhí)行結(jié)果
- 其他方式
- 1.使用JPQL進(jìn)行模糊查詢(xún):
- 2.使用Spring Data JPA的Query By Example進(jìn)行模糊查詢(xún):
- 3.使用Spring Data JPA的@Query注解進(jìn)行模糊查詢(xún):
- 總結(jié)提升
背景介紹
??在我們的項(xiàng)目中很多的業(yè)務(wù)都會(huì)設(shè)計(jì)模糊查詢(xún),例如按照姓氏去獲取人員的信息,按照手機(jī)號(hào)的前三位去獲取人員的信息等。我們除了正常的手寫(xiě)模糊查詢(xún)的sql語(yǔ)句去獲取信息之外,還可以使用JPA自帶的API來(lái)實(shí)現(xiàn)任意字段的模糊查詢(xún)。JPA已經(jīng)給我們封裝好了。當(dāng)我們對(duì)模糊查詢(xún)非常熟悉了之后直接拿來(lái)時(shí)候即可。
概念說(shuō)明
單字段模糊匹配:
說(shuō)明:在一個(gè)字段中無(wú)論關(guān)鍵字出現(xiàn)在什么位置上,只要有關(guān)鍵詞即可。
場(chǎng)景:獲取手機(jī)號(hào)開(kāi)頭為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)開(kāi)頭為187并且姓氏為武的學(xué)生
應(yīng)用:SELECT*FROM table_name WHERE BINARY column1_name LIKE'%keyword1%'AND column2_name LIKE'%keyword2%';
??注:BINARY函數(shù)是開(kāi)啟大小寫(xiě)敏感的函數(shù),底層邏輯是通過(guò)Ascii碼的方式比較的。因?yàn)閿?shù)據(jù)庫(kù)默認(rèn)是對(duì)大小寫(xiě)不敏感的,也就是我們?cè)诓樵?xún)名稱(chēng)為wzl數(shù)據(jù)的時(shí)候,也會(huì)把名稱(chēng)為Wzl的數(shù)據(jù)也查詢(xún)出來(lái)。
實(shí)現(xiàn)過(guò)程
代碼實(shí)現(xiàn)
1.寫(xiě)一個(gè)實(shí)體類(lèi)去實(shí)現(xiàn)Specification接口,重寫(xiě)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@Transientprivate UserDao userDao;@Overridepublic 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方式的模糊查詢(xún),也可是使用or的方式進(jìn)行模糊查詢(xún),也就是多個(gè)字段中都包含一個(gè)關(guān)鍵字。
2.定義一個(gè)接口去繼承JpaRepository接口,并指定返回的類(lèi)型和參數(shù)類(lèi)型
@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ù)類(lèi)中調(diào)用聲明的接口
@Entitypublic List<User> selectToFuzzy(User userInfo){List<User> userInfoList = userDao.findAll(userInfo);return userInfoList;}
4.在Controller中直接調(diào)用業(yè)務(wù)類(lèi)中的方法即可
@RequestMapping(value="selectToFuzzy",method= RequestMethod.POST)//模糊查詢(xún)用戶(hù)的信息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è)字段一起模糊查詢(xún)也是一樣。
其他方式
1.使用JPQL進(jìn)行模糊查詢(xú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)行模糊查詢(xún):
創(chuàng)建一個(gè)實(shí)體對(duì)象作為查詢(xún)條件,設(shè)置需要模糊匹配的屬性值。
例如:ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("name", match -> match.contains()); Example<Employee> example = Example.of(employee, matcher);
3.使用Spring Data JPA的@Query注解進(jìn)行模糊查詢(xún):
在Repository接口中使用@Query注解定義自定義的查詢(xún)方法。
在查詢(xún)方法中使用%通配符進(jìn)行模糊匹配。
例如:@Query("SELECT e FROM Employee e WHERE e.name LIKE %?1%") List<Employee> findByName(String name);
總結(jié)提升
??根據(jù)自己的業(yè)務(wù)需求去選擇使用哪一種模糊查詢(xún)的方式。底層原理都是一樣的。JPA封裝了一些公共的內(nèi)容,我們開(kāi)發(fā)的過(guò)程中使用起來(lái)就比較容易和簡(jiǎn)單。但是我們?cè)谑褂玫倪^(guò)程中也要明白底層是如何實(shí)現(xiàn),不能只停留在會(huì)使用的階段中。知其然也要知其所以然。