ibit-mybatis 2.x 介紹

原文連接:ibit-mybatis 2.x 介紹html

概述

ibit-mybatis 是一個 Mybatis 的加強工具,在 Mybatis 的基礎上增長了新的特性與功能,志在簡化開發流程、提升開發效率。java

特性

  • 無侵入,引入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操做。git

核心 sql 接口

詳細 api 文檔參考:ibit-mybatis 2.x API 文檔github

說明 接口
搜索 QuerySql
計數 CountSql
刪除 DeleteSql
插入 InsertSql
更新 UpdateSql

sql 接口支持

不一樣類型的 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 語句

sql 工廠類

工廠類:tech.ibit.mybatis.sqlbuilder.SqlFactory,通常不直接使用,繼承 RawMapper 的 Mapper 可直接建立 QuerySqlCountSqlDeleteSqlInsertSqlUpdateSql 對應實例。spring

Mapper 說明

Mapper 基礎支持

ibit-mybatis 定義了 4 種 Mapper,分別是 RawMapperNoIdMapperSingleIdMapperMultipleIdMapper。如下分別說明。sql

Mapper 類型 父接口 說明
RawMapper / 定義最原始的增、刪、改、查和 Sql 實例建立
NoIdMapper RawMapper 擴展無主鍵表的增
SingleIdMapper NoIdMapper 擴展單主鍵表的根據id增、刪、改、查
MultipleIdMapper NoIdMapper 擴展多主鍵表的根據id增、刪、改、查

使用 ibit-mybatis-generator 2.x 版本,會根據表主鍵數量,繼承不一樣的 Mapper。數據庫

Mapper 結合 Sql 自定義增、刪、改、查

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();
}

用法

相關引用

Gradle

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

Maven

<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轉換),其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

相關項目項目

公衆號

喜歡個人文章,請關注公衆號

IBIT程序猿

相關文章
相關標籤/搜索