目录
- Bean Searcher简介
- 开始在Spring Boot项目中使用Bean Searcher
- 数据库表
- 单表查询
- 实体类
- Controller
- 开始检索
- 无参请求
- 分页查询(page,size)
- 排序查询(sort,order)
- 指定(排除)字段查询(onlySelect | selectExclude)
- 字段过滤-等于([field]-op=eq)
- 字段过滤-不相等([field]-op=ne)
- 字段过滤-大于等于([field]-op=ge)
- 字段过滤-小于等于([field]-op=le)
- 字段过滤-大于([field]-op=gt)
- 字段过滤-小于([field]-op=lt)
- 字段过滤-between([field]-op=bt)
- 字段过滤-in([field]-op=il)
- 字段过滤-包含…([field]-op=ct)
- 字段过滤-以…开头([field]-op=sw)
- 字段过滤-以…结尾([field]-op=ew)
- 字段过滤-空或null([field]-op=ey)
- 字段过滤-非空([field]-op=ny)
- 忽略大小写([field]-ic=true)
- 多表连接查询
Bean Searcher简介
什么是Bean Searcher
Bean Searcher 是一个轻量级数据库条件检索引擎,是直接与数据库有跨表映射关系的 VO,不依赖具体的WEB框架,不依赖具体的ORM框架,减少后端模板代码的开发,极大提高开发效率,节省开发时间。
为什么Bean Searcher可以一行代码实现复杂查询
普通的 VO 无法与数据库直接映射,需要业务代码处理检索条件,从而实现检索;
而Bean Searcher是直接与数据库有跨表映射关系的 VO,它既可以与页面数据一一对应,又可以直接映射到数据库里的多张数据表。Bean Searcher中的字段和参数可以直接表达检索条件,映射成查询语句,返回的 SearchBean也不需要转换。因此,Bean Searcher可以极大提高效率,可通过一行代码实现复杂查询。
推荐的使用场景
推荐在非事务性的动态检索场景中使用
开始在Spring Boot项目中使用Bean Searcher
Bean Seacher可与任意框架集成,本文仅以Spring Boot为例
添加依赖:
<dependency>
<groupId>com.ejlchina</groupId>
<artifactId>bean-searcher-boot-starter</artifactId>
<version>3.8.2</version>
</dependency>
SpringBoot中,仅需添加此依赖便完成集成
数据库表
student:
teacher:
在单表查询中,仅以student表为例
单表查询
实体类
import com.ejlchina.searcher.bean.DbField;
import com.ejlchina.searcher.bean.SearchBean;
import lombok.Data;
@Data
@SearchBean(tables = "student") //单表查询时注解可省略
public class Student {
@DbField("id")
private String id;
@DbField("sname")
private String sname;
@DbField("classId")
private String classId;
@DbField("birthday")
private String birthday;
@DbField("email")
private String email;
}
Controller
@RestController
@RequestMapping("/demo14")
public class StudentController {
@Autowired
private MapSearcher mapSearcher; //注入Map检索器,检索出来的数据以Map对象形式呈现
/**
* Bean Searcher查询
* @param request
* @return
*/
@RequestMapping("/bs")
public SearchResult<Map<String, Object>> index(HttpServletRequest request) {
return mapSearcher.search(Student.class, MapUtils.flat(request.getParameterMap()));
}
}
内容已完成,此时可进行单表查询操作
开始检索
本文使用postman进行查询测试
无参请求
URL:localhost:8080/demo14/bs
分页查询(page,size)
page:返回的页数
size:每页返回的条数
排序查询(sort,order)
sort:需要排序的字段
order:排序方式
URL:localhost:8080/demo14/bs?sort=id&order=desc(结果按照id倒序排列)
指定(排除)字段查询(onlySelect | selectExclude)
onlySelect :需要查询的字段
selectExclude:需要排除的字段(结果显示排除字段外的所有字段)
URL:localhost:8080/demo14/bs?onlySelect=id,sname
(只查询表中id,sname字段)
URL:localhost:8080/demo14/bs?selectExclude=email,birthday
(查询表中除email,birthday外的字段)
字段过滤-等于([field]-op=eq)
后缀-op可以自定义(下同)
eq(Equal):相等
在相等的条件中,[field]-op=eq可省略
URL:localhost:8080/demo14/bs? id=2
或:localhost:8080/demo14/bs?id=2&id-op=eq(搜索表中id=2的数据)
字段过滤-不相等([field]-op=ne)
ne(Not Equal):不相等
URL:localhost:8080/demo14/bs?id=4&id-op=ne(搜索表中id不等于4的数据)
字段过滤-大于等于([field]-op=ge)
ge(GreateEqual):大于等于
URL:localhost:8080/demo14/bs?id=3&id-op=ge(搜索表中id大于等于3的数据)
字段过滤-小于等于([field]-op=le)
le(LessEqual):小于等于
URL:localhost:8080/demo14/bs?id=2&id-op=le(搜索表中id小于等于2的数据)
字段过滤-大于([field]-op=gt)
gt(GreateThan):大于
URL:localhost:8080/demo14/bs?id=3&id-op=gt(搜索表中id大于3的数据)
字段过滤-小于([field]-op=lt)
rt(LessThan):小于
URL:localhost:8080/demo14/bs?id=3&id-op=lt(搜索表中id小于3的数据)
字段过滤-between([field]-op=bt)
bt(between):返回在字段在两值之间的结果
URL:localhost:8080/demo14/bs?id-0=2&id-1=4&id-op=bt
字段过滤-in([field]-op=il)
il(inList):返回字段在所给值中的结果
URL:localhost:8080/demo14/bs?id-0=1&id-2=3&id-3=4&id-op=il
字段过滤-包含…([field]-op=ct)
ct(Contain):返回字段中包含指定值的结果
URL:localhost:8080/demo14/bs?sname=小&sname-op=ct
字段过滤-以…开头([field]-op=sw)
sw(StartWith):返回字段中以指定值开头的结果
URL:localhost:8080/demo14/bs?classId=10&classId-op=sw
字段过滤-以…结尾([field]-op=ew)
ew(EndWith):返回字段中以指定值结尾的结果
URL:localhost:8080/demo14/bs?classId=1&classId-op=ew
字段过滤-空或null([field]-op=ey)
ey(Empty):返回指定字段中为空或null的数据
向student表中插入一条含空值的数据
URL:localhost:8080/demo14/bs?birthday-op=ey
字段过滤-非空([field]-op=ny)
ny(NotEmpty):返回指定字段中非空的数据
URL:localhost:8080/demo14/bs?birthday-op=ny
忽略大小写([field]-ic=true)
ic(IgnoreCase):指定值忽略大小写
URL:localhost:8080/demo14/bs?email=5@163.COM&email-ic=true
多表连接查询
在实体类中,通过@SearchBean注解实现多表连接查询
左连接
实体类:
@SearchBean(tables = "student s left join teacher t on s.classId=t.classId")
public class StudentTeacher {
@DbField("s.id")
private String id;
@DbField("t.tname")
private String tname;
//……
}
右连接
实体类:
@SearchBean(tables = "student s right join teacher t on s.classId=t.classId")
public class StudentTeacher {
@DbField("s.id")
private String id;
@DbField("t.tname")
private String tname;
//……
}
内连接
实体类:
@SearchBean(tables = "student s inner join teacher t on s.classId=t.classId")
public class StudentTeacher {
@DbField("s.id")
private String id;
@DbField("t.tname")
private String tname;
//……
}
或:
实体类:
@SearchBean(tables = "student s,teacher t",
where="s.classId=t.classId")
public class StudentTeacher {
@DbField("s.id")
private String id;
@DbField("t.tname")
private String tname;
//……
}
持续更新中