首页 > 其他分享 >Bean Search 超级好用的搜索工具

Bean Search 超级好用的搜索工具

时间:2023-05-21 19:22:46浏览次数:31  
标签:Search name age label Bean params key english 好用

1、引入依赖

<dependency>
    <groupId>cn.zhxu</groupId>
    <artifactId>bean-searcher-boot-starter</artifactId>
    <version>4.1.2</version>
</dependency>

2、定义实体类

  • autoMapTo: 若不指定别名,自动映射的表
  • orderBy:排序字段,如果数据量大,不建议加,因为他是全表排序后再取页数
  • JsonFormat:日期格式化
@SearchBean(tables = "staff_dict s left join dept_dict d on d.dept_code=s.dept_code",
        autoMapTo = "s",
        orderBy="name",
        sortType = SortType.ALLOW_PARAM)
@Data
public class Staff {
    private String empNo;
    private String name;
    private String userName;
    @DbField("getpwd(password)")
    private String password;
    @DbField("d.dept_name")
    private String deptName;
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createDate;
}

3、controller

@RestController
@RequestMapping("emp")
public class TestController {
    @Autowired
    private BeanSearcher beanSearcher;

    @GetMapping("index")
    public Object list(@RequestParam Map<String, Object> params){
        // 组合检索、排序、分页 和 统计 都在这一句代码中实现了
        return beanSearcher.search(Emp.class, params, Emp::getSal);
    }

    @GetMapping("staff")
    public Object staff(@RequestParam Map<String, Object> params){
        // 组合检索、排序、分页 和 统计 都在这一句代码中实现了
        return beanSearcher.search(Staff.class, params,Staff::getEmpNo);
    }
}

4、生成的sql

select *
  from (select row_.*, rownum rownum_
          from (select s.emp_no c_0,
                       s.name c_1,
                       s.user_name c_2,
                       getpwd(password) c_3,
                       d.dept_name c_4
                  from staff_dict s
                  left join dept_dict d
                    on d.dept_code = s.dept_code
                 order by name) row_
         where rownum <= 30) table_
 where table_.rownum_ > 10

5、返回值

{
    "totalCount": 893,
    "dataList": [
        {
            "empNo": "1012",
            "name": "张三",
            "userName": "1012",
            "password": "1",
            "deptName": "外科疗区",
            "createDate": "2022-04-13 17:12:56"
        }
    ],
    "summaries": [
        2322726
    ]
}

6、注意点

  • 指定起始页,不配置默认为0,这里配置为1,是为了兼容element UI的分页组件
  • 默认分页使用的是mysql,其他分页请指定方言
bean-searcher:
 params:
   pagination:
     # 起始页,不配置默认为0,这里配置为1,是为了兼容element UI的分页组件
     start: 1
 sql:
   dialect: Oracle

7、请求简写说明

配置键名 含义 可选值
bean-searcher.params.separator 字段参数名分隔符 字符串 -
bean-searcher.params.operator-key 字段运算符参数名后缀字符串 op
bean-searcher.params.ignore-case-key 是否忽略大小写字段参数名后缀 字符串 ic
// 更多参考 https://bs.zhxu.cn/guide/latest/params.html#%E5%AD%97%E6%AE%B5%E8%BF%90%E7%AE%97%E7%AC%A6
numOps: [
            { key: 'eq', label: '等于',english:'Equal'},
            { key: 'ne', label: '不等于',english:'NotEqual'},
            { key: 'gt', label: '大于',english:'GreateThan'},
            { key: 'lt', label: '小于',english:'LessThan'},
            { key: 'ge', label: '大于等于',english:'GreateEqual'},
            { key: 'le', label: '小于等于',english:'LessEqual'},
            { key: 'bt', label: '区间',english:'Between'},
            { key: 'in', label: '包含',english:'in'},
            { key: 'ct', label: '包含',english:'Contain'},
            { key: 'ey', label: '空 或 null',english:'Empty'},
            { key: 'ny', label: '非空',english:'NotEmpty'},
            { key: 'sw', label: '以...开始',english:'StartWith'},
            { key: 'ew', label: '以...结束',english:'EndWith'}
        ],

8、请求格式

  1. GET /user/index:无参数,默认返回第 1 页,默认分页大小为 15 (可配置)bean-searcher.params.pagination.pagebean-searcher.params.pagination.size
  2. GET /user/index? page=2 & size=10 返回结果:结构同 (1)(只是每页 10 条,返回第 2 页)
  3. GET /user/index? sort=age & order=desc 返回结果:结构同 (1)(只是 dataList 数据列表以 age 字段降序输出)
  4. GET /user/index? age=20 & age-op=eq 返回结果:结构同 (1)(但只返回 age=20 的数据) age-op=eq 也可以省略
  5. GET /user/index? age=20 & age-op=ne 返回结果:结构同 (1)(但只返回 age != 20 的数据,ne 是 NotEqual 的缩写)
  6. GET /user/index? age-0=20 & age-1=30 & age-op=bt 返回结果:结构同 (1)(但只返回 20 <= age <= 30 的数据,bt 是 Between 的缩写)
  7. GET /user/index? age-0=20 & age-1=30 & age-2=40 & age-op=il 返回结果:结构同 (1)(但只返回 age in (20, 30, 40) 的数据,il 是 InList 的缩写)
  8. GET /user/index? name-op=ey 返回结果:结构同 (1)(但只返回 name 为空 或为 null 的数据,ey 是 Empty 的缩写)
  9. GET /user/index? name=Jack & name-ic=true 返回结果:结构同 (1)(但只返回 name 等于 Jack (忽略大小写) 的数据,ic 是 IgnoreCase 的缩写)

9、打印日志

  • 包名必须为:cn.zhxu.bs 不能修改
logging:
  level:
    cn.zhxu.bs: DEBUG
  • 如果需要日志文件也打印,配置logback.xml
<logger name="cn.zhxu.bs" level="DEBUG" additivity="false">
	<appender-ref ref="console" />
	<appender-ref ref="file" />
</logger>

10、其他检索

  • searchAll:所有数据 []
  • searchCount:数据量 纯数值
  • searchSum:求和 纯数值
  • search:带分页的查询(包含数据总数以及合计域)
  • searchList:带分页的查询,返回值是数据集的数组 []
  • searchFirst:返回满足条件的第一条,{}

标签:Search,name,age,label,Bean,params,key,english,好用
From: https://www.cnblogs.com/his365/p/17418817.html

相关文章

  • Nas Docker 安装个人记账web项目:firefly_iii &beancount-gs
    NasDocker安装个人记账web项目:firefly_iii&beancount-gs1.经过搜索以及GPT的询问,通过预览界面感觉firefly_iii官方示例demo:https://demo.firefly-iii.org/官方安装文档:https://docs.firefly-iii.org/firefly-iii/installation/docker/本人采用的是群晖Nasdocker安装:这个......
  • 推荐几个好用的在线代码编译器
    程序员最喜欢用的在线IDE代码编译器,功能很强大,类别很全!1.网址https://tech.io/snippet支持20+种编程语言,页面上没有杂七杂八的东西,非常简约,非常干净,另外,它上面的代码段还可以嵌入到网页之中。2.网址https://www.tutorialspoint.com/codingground.htm它其实并不单单是一个在线......
  • elasticsearch核心知识篇1 索引curd mapping query
    1,索引的curdGET_search{"query":{"match_all":{}}}#创建indexPUT/product#查询GET/product/_search#新增数据1PUT/product/_doc/1{"name":"xiaomiphone","desc":"shoujihongdezhandouji&q......
  • Field userClient in com.demo.order.service.OrderService required a bean of type'
    在SpringCloud项目中使用Feign进行远程调用遇到的错误。原因是因为UserClient在com.demo.feign.clients包下面,而order-service的@EnableFeignClientd注解却在com.demo.order包下面,这两个不在同一个包下,无法扫描到UserClient。解决方法有两种1.指定Feign应该扫描的包@EnableFeig......
  • 使用 Elasticsearch 的 REST API 来查询节点的内存使用情况
    curl-XGET'http://172.18.10.96:9200/_nodes/node-1/stats?pretty&human&filter_path=nodes.*.jvm.mem.heap_used_percent'{"nodes":{"WKECtNqYSuCKgHu-HNJTfg":{"jvm":{"mem":......
  • elasticsearch 启动报错 SearchPhaseExecutionException[Failed to execute phase [qu
    Elasticsearch启动报错:[2023-05-19T22:39:32,161][DEBUG][o.e.a.s.TransportSearchAction][X-111.ecs]Allshardsfailedforphase:[query][2020-05-19T22:39:32,162][WARN][r.suppressed][X-111.ecs]path:/.kibana_task_manager/_search,params:{ign......
  • Questions Whlie Reading A Research Paper
    Givemeabriefsummaryofthebackgroundandcontextoftheresearch.Givemeabriefsummaryofthebackgroundandcontextoftheresearch.Whataretheresearchquestionsaddressedinthepaper.Whataretheresearchquestionsaddressedinthepape......
  • Jmeter函数助手11-BeanShell
    BeanShell函数用于简单的计算或者运行编程脚本。表达式求值:填入脚本代码或脚本文件${__BeanShell(source(“test.bsh”))}存储结果的变量名(可选) 1、填入一个计算公式返回计算结果88/22=4,${__BeanShell(88/22,)} ......
  • Spring Boot |如何让你的 bean 在其他 bean 之前完成加载
    本文围绕SpringBoot中如何让你的bean在其他bean之前完成加载展开讨论。问题今天有个小伙伴给我出了一个难题:在SpringBoot中如何让自己的某个指定的Bean在其他Bean前完成被Spring加载?我听到这个问题的第一反应是,为什么会有这样奇怪的需求?Talkischeap,sho......
  • Spring Boot |如何让你的 bean 在其他 bean 之前完成加载
    本文围绕SpringBoot中如何让你的bean在其他bean之前完成加载展开讨论。问题今天有个小伙伴给我出了一个难题:在SpringBoot中如何让自己的某个指定的Bean在其他Bean前完成被Spring加载?我听到这个问题的第一反应是,为什么会有这样奇怪的需求?Talkischeap,sho......