原文連接: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
喜歡個人文章,請關注公衆號