原文连接:ibit-mybatis 2.x 介绍html
ibit-mybatis
是一个 Mybatis 的加强工具,在 Mybatis 的基础上增长了新的特性与功能,志在简化开发流程、提升开发效率。java
ibit-mybatis
对现有工程不会产生影响。sql-builder
定义动态SQL的生成规则,用来实现单表的CRUD操做。git
详细 api 文档参考:ibit-mybatis 2.x API 文档github
说明 | 接口 |
---|---|
搜索 | QuerySql |
计数 | CountSql |
删除 | DeleteSql |
插入 | InsertSql |
更新 | UpdateSql |
不一样类型的 sql, 其语句的约束不同,下表列举全部的语句支持。web
接口 | 支持方法 | 说明 |
---|---|---|
ColumnSupport | column columnPo |
SELECT column1[, column2...] 语句 |
DeleteSupport | delete | DELETE t1.* 语句 |
DistinctSupport | distinct | DISTINCT 语句 |
FromSupport | from | FROM table1 t1[, table2 t2...] 语句 |
GroupBySupport | groupBy | GROUP BY t1.column1[, t2.column2, ...] 语句 |
HavingSupport | having andHaving orHaving |
HAVING 语句 |
InsertTableSupport | insert | INSERT INTO table1 t1 语句, t1表示 "表别名" |
JoinOnSupport | joinOn leftJoinOn rightJoinOn fullJoinOn innerJoinOn complexLeftJoinOn complexRightJoinOn complexFullJoinOn complexInnerJoinOn |
[LEFT\|RIGHT\|FULL\|INNER] JOIN ON 语句 |
LimitSupport | limit | LIMIT #{start}, #{limit} 语句 |
OrderBySupport | orderBy | ORDER BY 语句 |
SetSupport | set | SET 条件语句 |
UpdateTableSupport | update | UPDATE table1 t1[, table2 t2...] 语句,t1,t2表示"表别名" |
ValuesSupport | values | (column1, column2, ...) VALUES(?, ?, ...) 语句 |
WhereSupport | where andWhere orWhere |
WHERE 语句 |
工厂类:tech.ibit.mybatis.sqlbuilder.SqlFactory
,通常不直接使用,继承 RawMapper
的 Mapper 可直接建立 QuerySql
、CountSql
、DeleteSql
、InsertSql
和 UpdateSql
对应实例。spring
ibit-mybatis
定义了 4 种 Mapper,分别是 RawMapper
,NoIdMapper
,SingleIdMapper
,MultipleIdMapper
。如下分别说明。sql
Mapper 类型 | 父接口 | 说明 |
---|---|---|
RawMapper | / | 定义最原始的增、删、改、查和 Sql 实例建立 |
NoIdMapper | RawMapper | 扩展无主键表的增 |
SingleIdMapper | NoIdMapper | 扩展单主键表的根据id增、删、改、查 |
MultipleIdMapper | NoIdMapper | 扩展多主键表的根据id增、删、改、查 |
使用 ibit-mybatis-generator 2.x 版本,会根据表主键数量,继承不一样的 Mapper。数据库
Mapper 建立 Sql 实例方法 | 实例类型 | 实例执行方法说明 |
---|---|---|
createQuery | QuerySql | executeQueryPage:查询(包含分页信息) executeQuery:查询列表 executeQueryOne:查询单条 executeQueryDefaultPage:查询基本类型(包含分页信息) executeQueryDefault:查询基本类型 |
createCount | CountSql | executeCount:计数 |
createDelete | DeleteSql | executeDelete:执行删除 |
createInsert | InsertSql | executeInsert:执行插入 executeInsertWithGenerateKeys:执行插入并生成主键 |
createUpdate | UpdateSql | executeUpdate:执行更新 |
自定义查询例子:api
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(); }
compile 'tech.ibit:ibit-mybatis:${lastest}'
<dependency> <groupId>tech.ibit</groupId> <artifactId>ibit-mybatis</artifactId> <version>${latest}</version> </dependency>
说明: 将 "${latest}" 替换成 2.0
以上版本。springboot
须要将 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转换),其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
喜欢个人文章,请关注公众号