ibit-mybatis 是一个 Mybatis 的增强工具,在 Mybatis 的基础上增加了新的特性与功能,志在简化开发流程、提高开发效率。
sql-builder定义动态SQL的生成规则,用来实现单表的CRUD操作。
详细 api 文档参考:ibit-mybatis 2.x API 文档
说明接口搜索QuerySql计数CountSql删除DeleteSql插入InsertSql更新UpdateSql不同类型的 sql, 其语句的约束不一样,下表列举所有的语句支持。
接口支持方法说明ColumnSupportcolumn columnPoSELECT column1[, column2...] 语句DeleteSupportdeleteDELETE t1.* 语句DistinctSupportdistinctDISTINCT 语句FromSupportfromFROM table1 t1[, table2 t2...] 语句GroupBySupportgroupByGROUP BY t1.column1[, t2.column2, ...]语句HavingSupporthaving andHaving orHavingHAVING语句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 orWhereWHERE 语句工厂类:tech.ibit.mybatis.sqlbuilder.SqlFactory,一般不直接使用,继承 RawMapper 的 Mapper 可直接创建 QuerySql、CountSql、DeleteSql、InsertSql 和 UpdateSql 对应实例。
ibit-mybatis 定义了 4 种 Mapper,分别是 RawMapper,NoIdMapper,SingleIdMapper,MultipleIdMapper。以下分别说明。
Mapper 类型父接口说明RawMapper/定义最原始的增、删、改、查和 Sql 实例创建NoIdMapperRawMapper扩展无主键表的增SingleIdMapperNoIdMapper扩展单主键表的根据id增、删、改、查MultipleIdMapperNoIdMapper扩展多主键表的根据id增、删、改、查使用 ibit-mybatis-generator 2.x 版本,会根据表主键数量,继承不同的 Mapper。
自定义查询例子:
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(); }说明: 将 “${latest}” 替换成 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=trueibit-mybatis 定义了枚举类型(CommonEnum,枚举-Integer转换),其TypeHandler为 CommonEnumTypeHandler。
如果使用 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喜欢我的文章,请关注公众号