mybatis-metamodelmybatis 元數(shù)據(jù)模型生成插件
mybatis-metamodel 是 mybatis-plus 和 tk mapper 的元數(shù)據(jù)模型生成插件,目的是解決它們?cè)趶?fù)雜場(chǎng)景下自定義 SQL 時(shí)的緊耦合問(wèn)題。
用途
我們都知道 mybatis-plus 和 tk mapper 開(kāi)源項(xiàng)目是 mybatis 優(yōu)秀的增強(qiáng)插件,它們通過(guò)重定義 MappedStatement 實(shí)現(xiàn)了通用增刪改查。它們所提供的增刪改查的功能,極大提高了開(kāi)發(fā)者的開(kāi)發(fā)效率。
但是,在一些復(fù)雜的應(yīng)用場(chǎng)景下,它們的通用增刪改查不能滿足我們的開(kāi)發(fā)需求,為了實(shí)現(xiàn)需求,需要通過(guò)它們內(nèi)置的條件查詢組件自定義查詢語(yǔ)句。在這種復(fù)雜場(chǎng)景下,就無(wú)法避免使用字符串表示表名或者字段名,這在復(fù)雜的項(xiàng)目中如果修改了字段名,很容易造成后期運(yùn)行時(shí)錯(cuò)誤。
為了避免應(yīng)用在運(yùn)行時(shí)發(fā)生錯(cuò)誤,本項(xiàng)目將在編譯期提供數(shù)據(jù)庫(kù)的元數(shù)據(jù),可以幫助應(yīng)用在編譯期就感知原有數(shù)據(jù)庫(kù)元數(shù)據(jù)的修改。
舉個(gè)li'zi:
我們需要自定義 SQL 查詢用戶信息。引入本項(xiàng)目后,可以在編譯期自動(dòng)生成元數(shù)據(jù)模型 UserInfo_.class,元數(shù)據(jù)模型里面包含每個(gè) table field 的信息,除此之外,還可以通過(guò) MetaModelContext 獲取對(duì)應(yīng)的表信息。
Mapper
// 根據(jù) example 條件查詢
String queryUserName = "hugo_1";
Example userInfoQueryExample = new Example(UserInfo.class);
Example.Criteria criteria = userInfoQueryExample.createCriteria();
criteria.andEqualTo(UserInfo_.username.getColumn(), queryUserName);
List<UserInfo> userInfosByQuery = userInfoMapper.selectByExample(userInfoQueryExample);
Assert.assertEquals(1, userInfosByQuery.size());
//獲取table名
String tableName = metaModelContext.getTableName(UserInfo_.class);
Assert.assertEquals("user_info", tableName);
mybatis-plus
// 根據(jù) map 查詢
// 查詢username為hugo_1的數(shù)據(jù)
Map<String, Object> map = new HashMap<>();
map.put(UserInfo_.username.getColumn(), "hugo_1"); //獲取元數(shù)據(jù)
userInfo = userInfoMapper.selectByMap(map).get(0);
// wrapper查詢
// 查詢address為中國(guó)的列表
List<UserInfo> userList = userInfoMapper.selectList(
new QueryWrapper<UserInfo>().eq(UserInfo_.address.getColumn(), "中國(guó)")
);
//獲取table名
String tableName = metaModelContext.getTableName(UserInfo_.class);
支持特性
mapper
支持MAPPER版本1.1.5+
1. 支持是否使用原語(yǔ)類型配置usePrimitiveType,默認(rèn)為false,建議不要開(kāi)啟,沒(méi)有意義
2. 支持配置useSimpleType,默認(rèn)為true
3. 支持配置enumAsSimpleType
4. 支持?jǐn)?shù)據(jù)庫(kù)的catalog和schema配置
5. 支持全局字段轉(zhuǎn)換方式style
6. 支持@Table注解
7. 支持@NameStyle注解
8. 支持@Transient、@Column、@ColumnType注解
9. 支持忽略static/transient修飾詞的字段
mybatis-plus
支持mybatis-plus版本3.2.0+
1. 支持全局配置tableUnderline、capitalMode
2. 支持忽略static/transient修飾詞的字段
3. 支持@TableName注解
4. 支持@TableId、@TableField注解
不支持特性
mapper
1. 不支持動(dòng)態(tài)表名 IDynamicTableName
2. 不支持方法上使用注解, 即 enableMethodAnnotation 全局配置
TODO
mapper
mybatis-plus
1. 支持配置keepGlobalFormat
2. 支持配置tablePrefix
3. 支持配置columnFormat
4. 支持配置propertyFormat
使用說(shuō)明
參考下面的wiki
gitee
