ibit-mybatis 2.x 介绍

iBit程序猿 2020年07月01日 1,101次浏览

概述

ibit-mybatis 是一个 Mybatis 的增强工具,在 Mybatis 的基础上增加了新的特性与功能,志在简化开发流程、提高开发效率。

特性

  • 无侵入,引入ibit-mybatis对现有工程不会产生影响。
  • 无 xml 配置,基于注解的方式实现
  • 灵活的CRUD(增、删、改、查)操作,Mapper,支持常用的单表CRUD操作,更有强大的SQL构造器(sql-builder),满足更为复杂的操作(如聚合函数、分组、连表、分页),为了让sql-builder更好的支持 ibit-mybatis,从 ibit-mybatis 2.0 开始,sql-builder 合并到 ibit-mybatis 中。
  • 内置代码生成器(ibit-mybatis-generator),指定数据库表,自动生成Mapper(无主键、单主键和多主键 Mapper)、Entity、Properties等基础类,减少重复或者相似代码编写。
  • 扩展支持,数据脱敏(后续支持)

sql-builder描述

sql-builder定义动态SQL的生成规则,用来实现单表的CRUD操作。

核心 sql 接口

详细 api 文档参考:ibit-mybatis 2.x API 文档

说明接口
搜索QuerySql
计数CountSql
删除DeleteSql
插入InsertSql
更新UpdateSql

sql 接口支持

不同类型的 sql, 其语句的约束不一样,下表列举所有的语句支持。

接口支持方法说明
ColumnSupportcolumn
columnPo
SELECT column1[, column2...] 语句
DeleteSupportdeleteDELETE t1.* 语句
DistinctSupportdistinctDISTINCT 语句
FromSupportfromFROM table1 t1[, table2 t2...] 语句
GroupBySupportgroupByGROUP BY t1.column1[, t2.column2, ...]语句
HavingSupporthaving
andHaving
orHaving
HAVING语句
InsertTableSupportinsertINSERT INTO table1 t1 语句, t1表示 "表别名"
JoinOnSupportjoinOn
leftJoinOn
rightJoinOn
fullJoinOn
innerJoinOn
complexLeftJoinOn
complexRightJoinOn
complexFullJoinOn
complexInnerJoinOn
[LEFT\|RIGHT\|FULL\|INNER] JOIN ON语句
LimitSupportlimitLIMIT #{start}, #{limit}语句
OrderBySupportorderByORDER BY 语句
SetSupportsetSET 条件语句
UpdateTableSupportupdateUPDATE table1 t1[, table2 t2...]语句,t1,t2表示"表别名"
ValuesSupportvalues(column1, column2, ...) VALUES(?, ?, ...)语句
WhereSupportwhere
andWhere
orWhere
WHERE 语句

sql 工厂类

工厂类:tech.ibit.mybatis.sqlbuilder.SqlFactory,一般不直接使用,继承 RawMapper 的 Mapper 可直接创建 QuerySqlCountSqlDeleteSqlInsertSqlUpdateSql 对应实例。

Mapper 说明

Mapper 基础支持

ibit-mybatis 定义了 5 种 Mapper,分别是 RawMapperMapperNoIdMapperSingleIdMapperMultipleIdMapper。以下分别说明。

Mapper 类型父接口说明
RawMapper/基于注解方式增、删、改、查
MapperRawMapper在 RawMapper 增加 Sql 实例创建,unique key 查询默认方法
NoIdMapperMapper扩展无主键表的增
SingleIdMapperMapper扩展单主键表的根据id增、删、改、查
MultipleIdMapperMapper扩展多主键表的根据id增、删、改、查

使用 ibit-mybatis-generator 2.x 版本,会根据表主键数量,继承不同的 Mapper。

Mapper 结合 Sql 自定义增、删、改、查

Mapper 创建 Sql 实例方法实例类型实例执行方法说明
createQueryQuerySqlexecuteQueryPage:查询(包含分页信息)
executeQuery:查询列表
executeQueryOne:查询单条
executeQueryDefaultPage:查询基本类型(包含分页信息)
executeQueryDefault:查询基本类型
createCountCountSqlexecuteCount:计数
createDeleteDeleteSqlexecuteDelete:执行删除
createInsertInsertSqlexecuteInsert:执行插入
executeInsertWithGenerateKeys:执行插入并生成主键
createUpdateUpdateSqlexecuteUpdate:执行更新

自定义查询例子:

public User getByUsername(String username) {
    if (StringUtils.isBlank(username)) {
        return null;
    }
    return mapper
            .createQuery()
            .columnPo(User.class)
            .from(UserProperties.TABLE)
            .andWhere(UserProperties.username.eq(username))
            .limit(1)
            .executeQueryOne();
}

// 如果 mapper 的实体类型为User,则可以简化为以下方式

public User getByUsername(String username) {
    if (StringUtils.isBlank(username)) {
        return null;
    }
    return mapper
            .createQuery()
            .andWhere(UserProperties.username.eq(username))
            .limit(1)
            .executeQueryOne();
}

用法

相关引用

Gradle

compile 'tech.ibit:ibit-mybatis:${lastest}'

Maven

<dependency>
  <groupId>tech.ibit</groupId>
  <artifactId>ibit-mybatis</artifactId>
  <version>${latest}</version>
</dependency>

说明: 将 "$" 替换成 2.0 以上版本。

配置说明

需要将 Mybatis Configuration 的 mapUnderscoreToCamelCase 的值设置为 true。

方式1:使用 mybatis-config.xml

<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>    

方式2:java 代码方式

Configuration configuration = new Configuration(environment);
configuration.setMapUnderscoreToCamelCase(true);

方式3:使用了 mybatis-spring-boot-starter,修改配置如下

# 字段映射驼峰
mybatis.configuration.map-underscore-to-camel-case=true

其他说明

ibit-mybatis 定义了枚举类型(CommonEnum,枚举-Integer转换),其TypeHandlerCommonEnumTypeHandler

如果使用 CommonEnum 作为系统通用枚举类,则需要做以下改造。

a. 新的枚举需要实现CommonEnum#getValue方法。

b. SqlProvider 需要做配置

SqlProvider.setValueFormatter(new LinkedHashMap<Class, Function<Object, Object>>() {{
    put(tech.ibit.mybatis.CommonEnum.class, o -> ((tech.ibit.mybatis.CommonEnum) o).getValue());
}});

c. 修改默认的枚举 TypeHandler

方式1:使用 mybatis-config.xml

<configuration>
    <settings>
        <setting name="defaultEnumTypeHandler" value="tech.ibit.mybatis.CommonEnumTypeHandler"/>
    </settings>
</configuration>    

方式2:java 代码方式

Configuration configuration = new Configuration(environment);
configuration.setDefaultEnumTypeHandler(tech.ibit.mybatis.CommonEnumTypeHandler.class);

方式3:使用了 mybatis-spring-boot-starter,修改配置如下

# 指定默认的枚举处理类
mybatis.configuration.default-enum-type-handler=tech.ibit.mybatis.CommonEnumTypeHandler

相关项目项目