BeetlSQL 自 2015 年启动研发,所有核心模块及周边生态组件均为自主研发。其设计目标是打造一个开发便捷、维护简单、运行高效的数据库访问框架,具备广泛的适用性、高度的可定制能力以及极低的学习门槛。

查阅文档 源码仓库 示例项目 在线试用 多数据库支持 性能压测 插件体系

本次版本更新内容如下:

  • 修复 SQL Server 在特定场景下无法正确读取表元数据的问题
  • Fetch 功能新增对多对多关系的自动加载支持
  • 升级底层模板引擎 Beetl 至最新版 3.20.1

Fetch 新增注解 @FetchMany2Many,配套提供 FetchMany2ManyAction 类(供 @Builder 注解使用),可通过中间表完成多对多关联数据的自动拉取。

@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD, ElementType.FIELD})
@Builder(FetchMany2ManyAction.class)
public @interface FetchMany2Many {
    // 中间表对应的实体类
    Class tableClass();
    String fromAttr(); // 当前实体在中间表中的外键字段名
    String toAttr();     // 关联实体在中间表中的外键字段名
    String enableOn() default ""; // 启用条件表达式(可选)
}

使用示例如下:

@Table(name="sys_user")
public class MyUser {
    @AutoID
    Integer id;
    String name;
    @FetchMany2Many(tableClass = UserRole.class, fromAttr = "userId", toAttr = "roleId")
    List<Role> roles;
}

BeetlSQL 核心模块说明

BeetlSQL 核心 功能
sql-core 基础核心包,封装 JDBC 调用、SQL 文件管理、注解解析等核心能力
sql-fetch 提供类似 Hibernate 的 `@Fetch`、`@FetchMany` 等懒加载/预加载注解支持
sql-mapper Mapper 接口定义与扩展机制,支持用户自定义 Mapper 及其专属注解
sql-intergration 集成主流框架(如 Spring、Solon、JFinal)的适配器与完整示例
sql-query Query 与 LambdaQuery 接口,支持通过 Java API 构建并执行 SQL
sql-mapping 单表/多表与 POJO 的双向映射能力,支持 JSON、XML 或约定优先等多种配置方式
sql-template 基于 Beetl 模板引擎实现 SQL 渲染,同时兼容其他模板语言扩展
sql-gen 代码生成抽象层,内置默认实现,可一键生成 DAO、SQL 文件、接口文档等
sql-db-support 覆盖 33+ 种数据库的兼容性测试与驱动验证
sql-samples 涵盖上百个典型业务场景的实战案例
sql-test 包含数百个高质量单元测试用例,保障稳定性与可靠性

BeetlSQL 官方扩展插件列表

BeetlSQL 扩展包 功能
sql-xml 仿 MyBatis XML 风格语法支持,适合习惯用 XML 编写 SQL 的开发者
sql-accelerator 性能增强插件,通过反射优化、缓存策略等手段,使性能提升 50%–200%,逼近手写 JDBC 效率
sql-firewall SQL 防御模块,防止误操作导致的数据破坏或敏感信息泄露
sql-dynamic-table 动态表访问支持,让程序像操作静态表一样处理运行时创建的表结构
sql-bean-encrypt 字段级加解密支持,内置 `@MD5`、`@AES` 等常用加密注解
sql-rewrite SQL 重写引擎,支持单表多租户、逻辑删除、数据权限控制等功能
SAGA(实验性) 基于 SAGA 模式的分布式事务实现,服务于微服务架构下的事务一致性需求

启用加速插件后的性能对比:最高可达原生 JDBC 性能的一半左右

查询类型 测试场景 BeetlSQL(ops/ms) 纯 JDBC MyBatis JPA
beetlsqlExecuteJdbc 直接执行 JDBC 语句 318 678 / 64
beetlsqlExecuteTemplate 执行 Beetl SQL 模板 268 / 44 66
beetlsqlFile SQL 统一存放于文件中管理 266 / 41 /
beetlsqlInsert 单条记录插入 129 248 43 59
beetlsqlGetAll 全量数据查询 13 40 4 5
beet

lsqlLambdaQuery
使用 LambdaQuery 构建并执行查询 196 / 9 /
beetlsqlPageQuery 分页查询操作 159 / 17 59
beetlsqlSelectById 根据主键查询单条记录 259 670 43 61

JMH 基准测试结果(单位:ops/ms)

Benchmark                                  Mode  Cnt     Score      Error   Units
Beetl.addEntity                           thrpt    5   133.190 ±   38.548  ops/ms
Beetl.complexMapping                      thrpt    5   236.289 ±  288.370  ops/ms
Beetl.executeJdbcSql                      thrpt    5   298.657 ±   80.480  ops/ms
Beetl.executeTemplateSql                  thrpt    5   251.418 ±   77.757  ops/ms
Beetl.getAll                              thrpt    5    14.531 ±    0.605  ops/ms
Beetl.getEntity                           thrpt    5   359.275 ±   85.099  ops/ms
Beetl.lambdaQuery                         thrpt    5   187.228 ±   59.697  ops/ms
Beetl.one2Many                            thrpt    5   178.041 ±  181.829  ops/ms
Beetl.pageQuery                           thrpt    5   148.982 ±   39.327  ops/ms
Beetl.sqlFile                             thrpt    5   249.206 ±  100.083  ops/ms
DBVisitor.addEntity                       thrpt    5    99.294 ±   33.564  ops/ms
DBVisitor.executeJdbcSql                  thrpt    5   185.177 ±   50.993  ops/ms
DBVisitor.executeTemplateSql              thrpt    5     3.189 ±    0.748  ops/ms
DBVisitor.getAll                          thrpt    5     3.163 ±    0.980  ops/ms
DBVisitor.getEntity                       thrpt    5   125.683 ±   44.190  ops/ms
DBVisitor.lambdaQuery                     thrpt    5    94.314 ±   38.499  ops/ms
DBVisitor.pageQuery                       thrpt    5    49.954 ±   18.254  ops/ms
EasyQuery.addEntity                       thrpt    5    98.904 ±   39.890  ops/ms
EasyQuery.complexMapping                  thrpt    5    67.397 ±   59.919  ops/ms
EasyQuery.executeJdbcSql                  thrpt    5   243.160 ±   48.080  ops/ms
EasyQuery.getAll                          thrpt    5    14.896 ±    2.219  ops/ms
EasyQuery.getEntity                       thrpt    5   134.370 ±   67.962  ops/ms
EasyQuery.lambdaQuery                     thrpt    5   137.292 ±   53.438  ops/ms
EasyQuery.one2Many                        thrpt    5    75.083 ±   53.738  ops/ms
EasyQuery.pageQuery                       thrpt    5    84.596 ±   46.942  ops/ms
Flex.addEntity                            thrpt    5    86.484 ±   40.022  ops/ms
Flex.getAll                               thrpt    5     2.926 ±    0.993  ops/ms
Flex.getEntity                            thrpt    5    73.445 ±   39.437  ops/ms
Flex.pageQuery                            thrpt    5    33.999 ±   26.776  ops/ms
Jdbc.addEntity                            thrpt    5   225.223 ±  304.585  ops/ms
Jdbc.executeJdbcSql                       thrpt    5   691.616 ±  145.280  ops/ms
Jdbc.getAll                               thrpt    5    35.827 ±    7.295  ops/ms
Jdbc.getEntity                            thrpt    5   678.791 ±  165.527  ops/ms
Jpa.addEntity                             thrpt    5    53.219 ±   78.543  ops/ms
Jpa.executeJdbcSql                        thrpt    5    60.734 ±   61.299  ops/ms
Jpa.executeTemplateSql                    thrpt    5    64.478 ±   48.688  ops/ms
Jpa.getAll                                thrpt    5     4.710 ±    2.062  ops/ms
Jpa.getEntity                             thrpt    5    56.325 ±   53.744  ops/ms
Jpa.one2Many                              thrpt    5    92.168 ±  111.591  ops/ms
Jpa.pageQuery                             thrpt    5    58.756 ±   48.056  ops/ms
MyBatis.addEntity                         thrpt    5    40.533 ±   12.461  ops/ms
MyBatis.complexMapping                    thrpt    5   106.348 ±   57.688  ops/ms
MyBatis.executeTemplateSql                thrpt    5    46.953 ±   13.541  ops/ms
MyBatis.getAll                            thrpt    5     5.394 ±    0.502  ops/ms
MyBatis.getEntity                         thrpt    5    45.303 ±   11.928  ops/ms
MyBatis.lambdaQuery                       thrpt    5    23.756 ±   13.613  ops/ms
MyBatis.pageQuery                         thrpt    5    16.190 ±    5.682  ops/ms
MyBatis.sqlFile                           thrpt    5    42.558 ±   18.025  ops/ms
SqlUtils.addEntity                        thrpt    5   232.501 ±  332.656  ops/ms
SqlUtils.executeJdbcSql                   thrpt    5   682.610 ±  171.506  ops/ms
SqlUtils.getAll                           thrpt    5    44.353 ±    4.346  ops/ms
SqlUtils.getEntity                        thrpt    5   643.873 ±  131.587  ops/ms
Wood.addEntity                            thrpt    5   105.097 ±   79.961  ops/ms
Wood.executeJdbcSql                       thrpt    5   127.020 ±   48.142  ops/ms
Wood.executeTemplateSql                   thrpt    5   129.193 ±   44.455  ops/ms
Wood.getAll                               thrpt    5     2.051 ±    0.524  ops/ms
Wood.getEntity                            thrpt    5   126.104 ±   41.976  ops/ms
Wood.lambdaQuery                          thrpt    5   125.013 ±   43.991  ops/ms
Wood.pageQuery                            thrpt    5   220.101 ±   90.028  ops/ms
Wood.sqlFile                              thrpt    5   132.431 ±   50.141  ops/ms

源码获取地址:点击下载