1 Star 1 Fork 254

houzhanwu / bean-searcher

forked from Troy / bean-searcher 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Artistic-2.0

Bean Searcher

Maven Central License Troy.Zhou

中文 | English

✨ 特性

  • 支持 实体多表映射
  • 支持 动态字段运算符
  • 支持 分组聚合 查询
  • 支持 Select | Where | From 子查询
  • 支持 实体类嵌入参数
  • 支持 字段转换器
  • 支持 Sql 拦截器
  • 支持 数据库 Dialect 扩展
  • 支持 多数据源 与 动态数据源
  • 支持 注解缺省 与 自定义
  • 支持 JDK 模块机制
  • 等等

⁉️为什么用

这绝不是一个重复的轮子

虽然 增删改 是 hibernate 和 mybatis、data-jdbc 等等 ORM 的强项,但查询,特别是有 多条件联表分页排序 的复杂的列表查询,却一直是它们的弱项。

传统的 ORM 很难用较少的代码实现一个复杂的列表检索,但 Bean Searcher 却在这方面下足了功夫,这些复杂的查询,几乎只用一行代码便可以解决。

  • 例如,这样的一个典型的需求:

后端需要写一个检索接口,而如果用传统的 ORM 来写,代码之复杂是可以想象的。

而 Bean Searcher 却可以:

💥 只一行代码实现以上功能

首先,你有一个实体类:

@SearchBean(tables="user u, role r", joinCond="u.role_id = r.id", autoMapTo="u")
public class User {
  private long id;
  private String username;
  private int status;
  private int age;
  private String gender;
  private Date joinDate;
  private int roleId;
  @DbField("r.name")
  private String roleName;
  // Getters and setters...
}

然后你就可以用一行代码实现这个用户检索接口:

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private BeanSearcher beanSearcher;              // 注入 BeanSearcher 的检索器

    @GetMapping("/index")
    public SearchResult<User> index(HttpServletRequest request) {
        // 这里只写一行代码
        return beanSearcher.search(User.class, MapUtils.flat(request.getParameterMap()));
    }
	
}

这一行代码实现了以下功能:

  • 多表联查
  • 分页搜索
  • 组合过滤
  • 任意字段排序
  • 字段统计

例如,该接口支持如下请求:

  • GET: /user/index

    无参请求(默认分页):

    {
      "dataList": [
        {
          "id": 1,
          "username": "Jack",
          "status": 1,
          "level": 1,
          "age": 25,
          "gender": "Male",
          "joinDate": "2021-10-01"
        },
        ...     // 默认返回 15 条数据
      ],
      "totalCount": 100,
      "summaries": [
        2500    // age 字段统计
      ]
    }
  • GET: /user/index? page=1 & size=10

    指定分页参数

  • GET: /user/index? status=1

    返回 status = 1 的用户

  • GET: /user/index? name=Jac & name-op=sw

    返回 nameJac 开头的用户

  • GET: /user/index? name=Jack & name-ic=true

    返回 name = Jack(忽略大小写)的用户

  • GET: /user/index? sort=age & order=desc

    按字段 age 降序查询

  • GET: /user/index? onlySelect=username,age

    只检索 usernameage 两个字段:

    {
      "dataList": [
        {
          "username": "Jack",
          "age": 25
        },
        ...
      ],
      "totalCount": 100,
      "summaries": [
        2500
      ]
    }
  • GET: /user/index? selectExclude=joinDate

    检索时排除 joinDate 字段

✨ 参数构建器

Map<String, Object> params = MapUtils.builder()
        .selectExclude(User::getJoinDate)                 // 排除 joinDate 字段
        .field(User::getStatus, 1)                        // 过滤:status = 1
        .field(User::getName, "Jack").ic()                // 过滤:name = 'Jack' (case ignored)
        .field(User::getAge, 20, 30).op(Opetator.Between) // 过滤:age between 20 and 30
        .orderBy(User::getAge, "asc")                     // 排序:年龄,从小到大
        .page(0, 15)                                      // 分页:第 0 页, 每页 15 条
        .build();
List<User> users = beanSearcher.searchList(User.class, params);

DEMO 快速体验

🚀 快速开发

使用 Bean Searcher 可以极大地节省后端的复杂列表检索接口的开发时间!

  • 普通的复杂列表查询只需一行代码
  • 单表检索可复用原有 Domain,无需定义 SearchBean

🌱 集成简单

可以和任意 Java Web 框架集成,如:SpringBoot、Spring MVC、Grails、Jfinal 等等。

Spring Boot 项目,添加依赖即集成完毕:

implementation 'com.ejlchina:bean-searcher-boot-stater:3.1.2'

接着便可在 ControllerService 里注入检索器:

/**
 * 注入 Map 检索器,它检索出来的数据以 Map 对象呈现
 */
@Autowired
private MapSearcher mapSearcher;

/**
 * 注入 Bean 检索器,它检索出来的数据以 泛型 对象呈现
 */
@Autowired
private BeanSearcher beanSearcher;

其它框架,使用如下依赖:

implementation 'com.ejlchina:bean-searcher:3.1.2'

然后可以使用 SearcherBuilder 构建一个检索器:

DataSource dataSource = ...     // 拿到应用的数据源

// DefaultSqlExecutor 也支持多数据源
SqlExecutor sqlExecutor = new DefaultSqlExecutor(dataSource);

// 构建 Map 检索器
MapSearcher mapSearcher = SearcherBuilder.mapSearcher()
        .sqlExecutor(sqlExecutor)
        .build();

// 构建 Bean 检索器
BeanSearcher beanSearcher = SearcherBuilder.beanSearcher()
        .sqlExecutor(sqlExecutor)
        .build();

🔨 扩展性强

面向接口设计,用户可自定义扩展 Bean Searcher 中的任何组件!

比如你可以:

  • 自定义 DbMapping 来实现自定义注解,或让 Bean Searcher 识别其它 ORM 的注解
  • 自定义 ParamResolver 来支持 JSON 形式的检索参数
  • 自定义 FieldConvertor 来支持任意的 字段类型
  • 自定义 Dialect 来支持更多的数据库
  • 等等..

📚 详细文档

参阅:https://searcher.ejlchina.com/

文档已完善!

🤝 友情接链

[ Sa-Token ] 一个轻量级 Java 权限认证框架,让鉴权变得简单、优雅!

[ OkHttps ] 轻量却强大的 HTTP 客户端,前后端通用,支持 WebSocket 与 Stomp 协议

[ hrun4j ] 接口自动化测试解决方案 --工具选得好,下班回家早;测试用得对,半夜安心睡

[ JsonKit ] 超轻量级 JSON 门面工具,用法简单,不依赖具体实现,让业务代码与 Jackson、Gson、Fastjson 等解耦!

[ Free UI ] 基于 Vue3 + TypeScript,一个非常轻量炫酷的 UI 组件库 !

❤️ 参与贡献

  1. Star and Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
Artistic License 2.0 Copyright (c) <year> <fullname> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble This license establishes the terms under which a given free software Package may be copied, modified, distributed, and/or redistributed. The intent is that the Copyright Holder maintains some artistic control over the development of that Package while still keeping the Package available as open source and free software. You are always permitted to make arrangements wholly outside of this license directly with the Copyright Holder of a given Package. If the terms of this license do not permit the full use that you propose to make of the Package, you should contact the Copyright Holder and seek a different licensing arrangement. Definitions "Copyright Holder" means the individual(s) or organization(s) named in the copyright notice for the entire Package. "Contributor" means any party that has contributed code or other material to the Package, in accordance with the Copyright Holder's procedures. "You" and "your" means any person who would like to copy, distribute, or modify the Package. "Package" means the collection of files distributed by the Copyright Holder, and derivatives of that collection and/or of those files. A given Package may consist of either the Standard Version, or a Modified Version. "Distribute" means providing a copy of the Package or making it accessible to anyone else, or in the case of a company or organization, to others outside of your company or organization. "Distributor Fee" means any fee that you charge for Distributing this Package or providing support for this Package to another party. It does not mean licensing fees. "Standard Version" refers to the Package if it has not been modified, or has been modified only in ways explicitly requested by the Copyright Holder. "Modified Version" means the Package, if it has been changed, and such changes were not explicitly requested by the Copyright Holder. "Original License" means this Artistic License as Distributed with the Standard Version of the Package, in its current version or as it may be modified by The Perl Foundation in the future. "Source" form means the source code, documentation source, and configuration files for the Package. "Compiled" form means the compiled bytecode, object code, binary, or any other form resulting from mechanical transformation or translation of the Source form. Permission for Use and Modification Without Distribution (1) You are permitted to use the Standard Version and create and use Modified Versions for any purpose without restriction, provided that you do not Distribute the Modified Version. Permissions for Redistribution of the Standard Version (2) You may Distribute verbatim copies of the Source form of the Standard Version of this Package in any medium without restriction, either gratis or for a Distributor Fee, provided that you duplicate all of the original copyright notices and associated disclaimers. At your discretion, such verbatim copies may or may not include a Compiled form of the Package. (3) You may apply any bug fixes, portability changes, and other modifications made available from the Copyright Holder. The resulting Package will still be considered the Standard Version, and as such will be subject to the Original License. Distribution of Modified Versions of the Package as Source (4) You may Distribute your Modified Version as Source (either gratis or for a Distributor Fee, and with or without a Compiled form of the Modified Version) provided that you clearly document how it differs from the Standard Version, including, but not limited to, documenting any non-standard features, executables, or modules, and provided that you do at least ONE of the following: (a) make the Modified Version available to the Copyright Holder of the Standard Version, under the Original License, so that the Copyright Holder may include your modifications in the Standard Version. (b) ensure that installation of your Modified Version does not prevent the user installing or running the Standard Version. In addition, the Modified Version must bear a name that is different from the name of the Standard Version. (c) allow anyone who receives a copy of the Modified Version to make the Source form of the Modified Version available to others under (i) the Original License or (ii) a license that permits the licensee to freely copy, modify and redistribute the Modified Version using the same licensing terms that apply to the copy that the licensee received, and requires that the Source form of the Modified Version, and of any works derived from it, be made freely available in that license fees are prohibited but Distributor Fees are allowed. Distribution of Compiled Forms of the Standard Version or Modified Versions without the Source (5) You may Distribute Compiled forms of the Standard Version without the Source, provided that you include complete instructions on how to get the Source of the Standard Version. Such instructions must be valid at the time of your distribution. If these instructions, at any time while you are carrying out such distribution, become invalid, you must provide new instructions on demand or cease further distribution. If you provide valid instructions or cease distribution within thirty days after you become aware that the instructions are invalid, then you do not forfeit any of your rights under this license. (6) You may Distribute a Modified Version in Compiled form without the Source, provided that you comply with Section 4 with respect to the Source of the Modified Version. Aggregating or Linking the Package (7) You may aggregate the Package (either the Standard Version or Modified Version) with other packages and Distribute the resulting aggregation provided that you do not charge a licensing fee for the Package. Distributor Fees are permitted, and licensing fees for other components in the aggregation are permitted. The terms of this license apply to the use and Distribution of the Standard or Modified Versions as included in the aggregation. (8) You are permitted to link Modified and Standard Versions with other works, to embed the Package in a larger work of your own, or to build stand-alone binary or bytecode versions of applications that include the Package, and Distribute the result without restriction, provided the result does not expose a direct interface to the Package. Items That are Not Considered Part of a Modified Version (9) Works (including, but not limited to, modules and scripts) that merely extend or make use of the Package, do not, by themselves, cause the Package to be a Modified Version. In addition, such works are not considered parts of the Package itself, and are not subject to the terms of this license. General Provisions (10) Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license. (11) If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license. (12) This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder. (13) This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed. (14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

简介

🔥🔥🔥 比 MyBatis 开发速度快 100 倍的条件检索引擎,天生支持联表,使一行代码实现复杂列表检索成为可能! 展开 收起
Java
Artistic-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/houzhanwu/bean-searcher.git
git@gitee.com:houzhanwu/bean-searcher.git
houzhanwu
bean-searcher
bean-searcher
master

搜索帮助