2626* [ 表重命名配置插件(TableRenameConfigurationPlugin)] ( #18-表重命名配置插件 )
2727* [ Lombok插件(LombokPlugin)] ( #19-Lombok插件 )
2828* [ 数据ModelCloneable插件(ModelCloneablePlugin)] ( #20-数据ModelCloneable插件 )
29+ * [ 状态枚举生成插件(EnumTypeStatusPlugin)] ( #21-状态枚举生成插件 )
2930
3031---------------------------------------
3132Maven引用:
3233``` xml
3334<dependency >
3435 <groupId >com.itfsw</groupId >
3536 <artifactId >mybatis-generator-plugin</artifactId >
36- <version >1.2.12 </version >
37+ <version >1.2.14 </version >
3738</dependency >
3839```
3940---------------------------------------
@@ -88,7 +89,7 @@ targetCompatibility = 1.8
8889
8990
9091def mybatisGeneratorCore = 'org.mybatis.generator:mybatis-generator-core:1.3.7'
91- def itfswMybatisGeneratorPlugin = 'com.itfsw:mybatis-generator-plugin:1.2.12 '
92+ def itfswMybatisGeneratorPlugin = 'com.itfsw:mybatis-generator-plugin:1.2.14 '
9293
9394mybatisGenerator {
9495 verbose = false
@@ -280,16 +281,19 @@ public class Test {
280281```
281282### 4. Example 增强插件(example,andIf,orderBy)
282283* Criteria的快速返回example()方法。
283- * Criteria链式调用增强,以前如果有按条件增加的查询语句会打乱链式查询构建,现在有了andIf(boolean ifAdd, CriteriaAdd add)方法可一直使用链式调用下去。
284- * Example增强了setOrderByClause方法,新增orderBy(String orderByClause)方法直接返回example,增强链式调用,可以一路.下去了。
285- * 继续增强orderBy(String orderByClause)方法,增加orderBy(String ... orderByClauses)方法,配合数据Model属性对应Column获取插件(ModelColumnPlugin)使用效果更佳。
284+ * ~~ Criteria链式调用增强,以前如果有按条件增加的查询语句会打乱链式查询构建,现在有了andIf(boolean ifAdd, CriteriaAdd add)方法可一直使用链式调用下去。~~
285+ * Example增强了setOrderByClause方法,新增orderBy(String orderByClause)、orderBy(String ... orderByClauses)方法直接返回example,增强链式调用,配合数据Model属性对应Column获取插件(ModelColumnPlugin)使用效果更佳。
286286* 增加基于column的操作,当配置了[ 数据Model属性对应Column获取插件(ModelColumnPlugin)] ( #8-数据model属性对应column获取插件 ) 插件时,提供column之间的比对操作。
287287* 增加createCriteria静态方法newAndCreateCriteria简写example的创建。
288+ * 增加when方法(Example和Criteria都有),方便根据不同条件附加对应操作。
288289
289290插件:
290291``` xml
291292<!-- Example Criteria 增强插件 -->
292- <plugin type =" com.itfsw.mybatis.generator.plugins.ExampleEnhancedPlugin" />
293+ <plugin type =" com.itfsw.mybatis.generator.plugins.ExampleEnhancedPlugin" >
294+ <!-- 是否支持已经过时的andIf方法(推荐使用when代替),默认支持 -->
295+ <property name =" enableAndIf" value =" true" />
296+ </plugin >
293297```
294298使用:
295299``` java
@@ -305,7 +309,7 @@ public class Test {
305309 .example()
306310 );
307311
308- // ----------------------------------- andIf-------------- ---------------------
312+ // ----------------- andIf (@Deprecated 尽量使用when代替) ---------------------
309313 // Criteria增强了链式调用,现在一些按条件增加的查询条件不会打乱链式调用了
310314 // old
311315 TbExample oldEx = new TbExample ();
@@ -342,6 +346,25 @@ public class Test {
342346 .example()
343347 );
344348
349+ // -----------------------------------when-----------------------------------
350+ this . tbMapper. selectByExample(
351+ TbExample . newAndCreateCriteria()
352+ // 如果随机数大于1,附加Field3查询条件
353+ .when(Math . random() > 1 , new TbExample .ICriteriaWhen () {
354+ @Override
355+ public void criteria (TbExample .Criteria criteria ) {
356+ criteria. andField3EqualTo(2 );
357+ }
358+ })
359+ // 当然最简洁的写法是采用java8的Lambda表达式,当然你的项目是Java8+
360+ .when(Math . random() > 1 , criteria - > criteria. andField3EqualTo(2 ))
361+ // 也支持 if else 这种写法
362+ .when(Math . random() > 1 , criteria - > criteria. andField3EqualTo(2 ), criteria - > criteria. andField3EqualTo(3 ))
363+ .example()
364+ // example上也支持 when 方法
365+ .when(true , example - > example. orderBy(" field1 DESC" ))
366+ );
367+
345368 // -----------------------------------orderBy-----------------------------------
346369 // old
347370 TbExample ex = new TbExample ();
@@ -1393,4 +1416,109 @@ public class Test {
13931416 <!-- 数据ModelCloneable插件 -->
13941417 <plugin type =" com.itfsw.mybatis.generator.plugins.ModelCloneablePlugin" />
13951418</xml >
1419+ ```
1420+ ### 21. 状态枚举生成插件
1421+ 数据库中经常会定义一些状态字段,该工具可根据约定的注释格式生成对应的枚举类,方便使用。
1422+ ``` xml
1423+ <xml >
1424+ <!-- 状态枚举生成插件 -->
1425+ <plugin type =" com.itfsw.mybatis.generator.plugins.EnumTypeStatusPlugin" >
1426+ <!-- 这里可以定义全局需要检查生成枚举类的列名 -->
1427+ <property name =" enumColumns" value =" type, status" />
1428+ </plugin >
1429+ <table tableName =" tb" >
1430+ <!-- 也可以为单独某个table增加配置 -->
1431+ <property name =" enumColumns" value =" user_type" />
1432+ </table >
1433+ </xml >
1434+ ```
1435+ > warning: 约定的注释检查规则的正则表达式如下
1436+ ``` java
1437+ public class EnumTypeStatusPlugin {
1438+ public final static String REMARKS_PATTERN = " .*\\ s*\\ [\\ s*(\\ w+\\ s*\\ (\\ s*[\\ u4e00-\\ u9fa5_a-zA-Z0-9]+\\ s*\\ )\\ s*:\\ s*[\\ u4e00-\\ u9fa5_a-zA-Z0-9]+\\ s*\\ ,?\\ s*)+\\ s*\\ ]\\ s*.*" ;
1439+ }
1440+
1441+ ```
1442+ 使用
1443+ ``` sql
1444+ CREATE TABLE `tb ` (
1445+ ` type` smallint (3 ) COMMENT ' 注释[success(0):成功, fail(1):失败]' ,
1446+ ` status` bigint (3 ) COMMENT ' 换行的注释
1447+ [
1448+ login_success(0):登录成功,
1449+ login_fail(1):登录失败
1450+ ]' ,
1451+ ` other_type` varchar (20 ) COMMENT ' 具体注释的写法是比较宽泛的,只要匹配上面正则就行
1452+ [ success ( 我是具体值 ) : 我是值的描述_我可以是中英文数字和下划线_xxx_123, fail_xx_3
1453+ (1 ) : 失败] 后面也可以跟注释'
1454+ );
1455+ ```
1456+ ``` java
1457+ public class Tb {
1458+ public enum Type {
1459+ SUCCESS ((short )0 , " 成功" ),
1460+ FAIL ((short )1 , " 失败" );
1461+
1462+ private final Short value;
1463+ private final String name;
1464+
1465+ Type (Short value , String name ) {
1466+ this . value = value;
1467+ this . name = name;
1468+ }
1469+ public Short getValue () {
1470+ return this . value;
1471+ }
1472+ public Short value () {
1473+ return this . value;
1474+ }
1475+ public String getName () {
1476+ return this . name;
1477+ }
1478+ }
1479+
1480+ public enum Status {
1481+ LOGIN_SUCCESS (0L , " 登录成功" ),
1482+ LOGIN_FAIL (1L , " 登录失败" );
1483+
1484+ private final Long value;
1485+ private final String name;
1486+
1487+ Status (Long value , String name ) {
1488+ this . value = value;
1489+ this . name = name;
1490+ }
1491+ public Long getValue () {
1492+ return this . value;
1493+ }
1494+ public Long value () {
1495+ return this . value;
1496+ }
1497+ public String getName () {
1498+ return this . name;
1499+ }
1500+ }
1501+
1502+ public enum OtherType {
1503+ SUCCESS (" 我是具体值" , " 我是值的描述_我可以是中英文数字和下划线_xxx_123" ),
1504+ FAIL_XX_3 (" 1" , " 失败" );
1505+
1506+ private final String value;
1507+ private final String name;
1508+
1509+ OtherType (String value , String name ) {
1510+ this . value = value;
1511+ this . name = name;
1512+ }
1513+ public String getValue () {
1514+ return this . value;
1515+ }
1516+ public String value () {
1517+ return this . value;
1518+ }
1519+ public String getName () {
1520+ return this . name;
1521+ }
1522+ }
1523+ }
13961524```
0 commit comments