Bean?Searcher配合SpringBoot的使用詳解
先吐槽一下,現(xiàn)在的Bean Searcher操作手冊(cè)的指引弱的可憐…
對(duì)我這樣的小白及其不友好
話(huà)不多說(shuō)直入主題
1、首先肯定是得引入依賴(lài)
<dependency>
<groupId>com.ejlchina</groupId>
<artifactId>bean-searcher-boot-starter</artifactId>
<version>${searcher.version}</version>
</dependency>2、再配置一下設(shè)置
bean-searcher:
params:
pagination:
start: 1其他的依賴(lài)、數(shù)據(jù)源啥的比較常用這里就不展出
3、然后就是創(chuàng)建實(shí)體類(lèi)
由于我為了快速就用了之前使用MyBatis做持久化的一個(gè)項(xiàng)目,所以會(huì)有Mapper啥的,不過(guò)看官方文檔和Demo上的例子,好像也沒(méi)用到所以應(yīng)該沒(méi)影響
?。?!為了直觀我直接Copy源代碼上來(lái),可以先跳過(guò)這個(gè)源碼直接看重點(diǎn)介紹
package com.so2.core.model.entity;
import com.baomidou.mybatisplus.annotation.*;
import java.io.Serializable;
import java.util.Date;
import com.ejlchina.searcher.bean.BeanAware;
import com.ejlchina.searcher.bean.DbField;
import com.ejlchina.searcher.bean.SearchBean;
import lombok.Data;
/**
1. 用戶(hù)表
2. @author Lynn
3. @TableName user
*/
@TableName(value ="user")
@Data
@SearchBean( tables = "user")
public class User implements Serializable, BeanAware {
/**
*
*/
@TableId(type = IdType.AUTO)
@DbField("id")
private Integer id;
/**
* 用戶(hù)名
*/
@DbField("name")
private String name;
/**
* 用戶(hù) id
*/
@DbField("userId")
private Integer userid;
/**
* 用戶(hù)郵箱
*/
@DbField("email")
private String email;
/**
* 用戶(hù)密碼
*/
@DbField("password")
private String password;
/**
* 用戶(hù)是否被封禁, 0-未封禁,1-已封禁
*/
@DbField
private Byte delflag;
/**
* 用戶(hù)權(quán)限, 0-游客, 1-普通用戶(hù), 2-會(huì)員用戶(hù), 3-管理員
*/
@DbField("role")
private Byte role;
/**
* 注冊(cè)日期
*/
@DbField("registerTime")
private Date registertime;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
User other = (User) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
&& (this.getUserid() == null ? other.getUserid() == null : this.getUserid().equals(other.getUserid()))
&& (this.getEmail() == null ? other.getEmail() == null : this.getEmail().equals(other.getEmail()))
&& (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword()))
&& (this.getDelflag() == null ? other.getDelflag() == null : this.getDelflag().equals(other.getDelflag()))
&& (this.getRole() == null ? other.getRole() == null : this.getRole().equals(other.getRole()))
&& (this.getRegistertime() == null ? other.getRegistertime() == null : this.getRegistertime().equals(other.getRegistertime()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
result = prime * result + ((getUserid() == null) ? 0 : getUserid().hashCode());
result = prime * result + ((getEmail() == null) ? 0 : getEmail().hashCode());
result = prime * result + ((getPassword() == null) ? 0 : getPassword().hashCode());
result = prime * result + ((getDelflag() == null) ? 0 : getDelflag().hashCode());
result = prime * result + ((getRole() == null) ? 0 : getRole().hashCode());
result = prime * result + ((getRegistertime() == null) ? 0 : getRegistertime().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", name=").append(name);
sb.append(", userid=").append(userid);
sb.append(", email=").append(email);
sb.append(", password=").append(password);
sb.append(", delflag=").append(delflag);
sb.append(", role=").append(role);
sb.append(", registertime=").append(registertime);
sb.append("]");
return sb.toString();
}
@Override
public void afterAssembly() {
System.out.println("--------使用了afterAssembly方法----------");
}
}
創(chuàng)建實(shí)體類(lèi)需要注意幾點(diǎn)
- 類(lèi),要在類(lèi)名加上 @SearchBean( tables = “定義表名”) 注解,而且必須加上表名,我之前不加表名會(huì)報(bào)錯(cuò),而且加上表名在進(jìn)行多表查詢(xún)時(shí)才能復(fù)用
- 實(shí)現(xiàn)接口,必須要實(shí)現(xiàn) BeanAware或者ParamAware接口,重寫(xiě)的方法可以不做任何改動(dòng)。
- 字段,必須要在需要得到響應(yīng)的字段或查詢(xún)的字段上加上**@DbField(“自定義字段名”)** 注解,而且必須指定字段名,且加上注解的字段必須 大于0 | 大于被查詢(xún)數(shù)
- get set,加上LomBok的 @Data 注解,或使用idea的快速生成
4、最后就是編寫(xiě)Controller層
先粘上源碼
同上,也可先跳過(guò)源碼直接看注意事項(xiàng)
package com.so2.core.controller;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ejlchina.searcher.MapSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.Searcher;
import com.ejlchina.searcher.util.MapUtils;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.so2.core.service.impl.UserServiceImpl;
import com.so2.core.model.entity.User;
import com.so2.core.base.BaseResponse;
import com.so2.core.base.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.util.RequestUtil;
import org.apache.ibatis.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 描述:測(cè)試類(lèi)
*
* @author: Lynn
* @date: 2021/12/3
*/
@Api(tags = "測(cè)試接口類(lèi)")
@ApiSupport(author = "Lynn", order = 07)
@RestController
@RequestMapping("/test")
public class HealthController {
//MyBatis
@Resource
private UserServiceImpl user;
//注入MapSearcher
@Autowired
private MapSearcher mapSearcher;
// 先使用MyBatis方法做個(gè)對(duì)照組
// 提示:BaseResponse是我寫(xiě)的響應(yīng)類(lèi),而ResultUtils是返回工具類(lèi),返回的結(jié)果包含了響應(yīng)碼、響應(yīng)數(shù)據(jù)、控制臺(tái)提示
// 千萬(wàn)別加這個(gè),一旦加了就會(huì)報(bào)空指針異常
// @ApiImplicitParam(name = "userName", value = "用戶(hù)賬號(hào)名", required = true)
@ApiOperation(value = "通過(guò)用戶(hù)賬號(hào)名獲取信息")
@GetMapping("/getN")
@ResponseBody
public BaseResponse<List<User>> testGetUser(String userName){
QueryWrapper<User> qw = new QueryWrapper<>();
qw.like("name", userName);
return ResultUtils.success(user.list(qw), "查找成功");
}
// 這個(gè)方法比較多變,官方文檔也有說(shuō)明
@ApiOperation(value = "通過(guò)新的Searcher方法來(lái)進(jìn)行便捷查找")
@ResponseBody
@GetMapping("/searcherGet")
public Object getForSearcher(HttpServletRequest request){
// 可以在builder()后使用其他方法進(jìn)行數(shù)據(jù)篩選
return mapSearcher.search(User.class,
MapUtils.builder()
.build());
}
// 這個(gè)方法可以傳入值進(jìn)行動(dòng)態(tài)查找
@ApiOperation(value = "使用Searcher “動(dòng)態(tài)“ 查找字段")
@ResponseBody
@GetMapping("/searcherGetName")
public BaseResponse<Object> dynamicField(){
Map<String, Object> map = new HashMap<>();
map.put("name", "Lynn");
return ResultUtils.success(mapSearcher.searchList(User.class, map), "------動(dòng)態(tài)查詢(xún)字段成功------");
}
}
Controller層編寫(xiě)的注意事項(xiàng)
- 需要先注入MapSearcher或BeanSearcher(官方文檔那個(gè)提示構(gòu)建構(gòu)造器的步驟坑死我了,還以為要寫(xiě)一個(gè)單例Bean來(lái)進(jìn)行配置)
- 看源碼注釋?zhuān)?/li>
- (dog這個(gè)源碼比較粗糙,看的出來(lái)我并沒(méi)有對(duì)一些可能出現(xiàn)的異常、情況進(jìn)行捕獲。
看看我查詢(xún)返回的數(shù)據(jù)
MyBatis查詢(xún)

Bean Searcher查詢(xún)
返回加了字段名字段的所有信息

查找字段名為name且值為L(zhǎng)ynn的加了字段名字段的信息

結(jié)語(yǔ)
最后要說(shuō)一句,現(xiàn)在網(wǎng)上的文章很多都是抄來(lái)抄去的,有互相抄的也有直接搬官方文檔的,所以大家在學(xué)一樣?xùn)|西之前如果有官方文檔和官方示例一定要先去看,別問(wèn)我為什么懂這個(gè)道理的…
最后附上Bean Searcher官方文檔地址和示例的gitee倉(cāng)庫(kù)
官方文檔地址: https://searcher.ejlchina.com/
官方Gitee倉(cāng)庫(kù)地址:git@gitee.com:ejlchina-zhxu/bean-searcher.git
到此這篇關(guān)于Bean Searcher配合SpringBoot的使用的文章就介紹到這了,更多相關(guān)Bean Searcher配合SpringBoot使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中一維二維數(shù)組的靜態(tài)和動(dòng)態(tài)初始化
今天通過(guò)本文給大家分享Java中的數(shù)組,包括一維數(shù)組和二維數(shù)組的靜態(tài)初始化和動(dòng)態(tài)初始化問(wèn)題,感興趣的朋友一起看看吧2017-10-10
Spring Cloud Zuul路由規(guī)則動(dòng)態(tài)更新解析
這篇文章主要介紹了Spring Cloud Zuul路由規(guī)則動(dòng)態(tài)更新解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Spring security認(rèn)證兩類(lèi)用戶(hù)代碼實(shí)例
這篇文章主要介紹了Spring security認(rèn)證兩類(lèi)用戶(hù)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
詳解Java的readBytes是怎么實(shí)現(xiàn)的
眾所周知,Java是一門(mén)跨平臺(tái)語(yǔ)言,針對(duì)不同的操作系統(tǒng)有不同的實(shí)現(xiàn),下面小編就來(lái)從一個(gè)非常簡(jiǎn)單的api調(diào)用帶大家來(lái)看看Java具體是怎么做的吧2023-07-07
Java語(yǔ)言通過(guò)三種方法實(shí)現(xiàn)隊(duì)列的示例代碼
這篇文章主要介紹了Java語(yǔ)言通過(guò)三種方法來(lái)實(shí)現(xiàn)隊(duì)列的實(shí)例代碼,數(shù)組模擬隊(duì)列,通過(guò)對(duì)定義的了解,發(fā)現(xiàn)隊(duì)列很像我們的數(shù)組,下面我們通過(guò)實(shí)踐給大家詳細(xì)介紹,需要的朋友可以參考下2022-02-02
@RequestParam使用defaultValue屬性設(shè)置默認(rèn)值的操作
這篇文章主要介紹了@RequestParam使用defaultValue屬性設(shè)置默認(rèn)值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
MyBatis-Plus 使用枚舉自動(dòng)關(guān)聯(lián)注入
本文主要介紹了MyBatis-Plus 使用枚舉自動(dòng)關(guān)聯(lián)注入,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06

