Commit 23cf1069 authored by hewei's avatar hewei

重构代码插件依赖关系

parent 78e16adb
......@@ -27,7 +27,7 @@ Maven引用:
<dependency>
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>
</dependency>
```
---------------------------------------
......@@ -556,17 +556,57 @@ public class Test {
```
### 10. Selective选择插入更新增强插件
项目中往往需要指定某些字段进行插入或者更新,或者把某些字段进行设置null处理,这种情况下原生xxxSelective方法往往不能达到需求,因为它的判断条件是对象字段是否为null,这种情况下可使用该插件对xxxSelective方法进行增强。
>warning:配置插件时请把插件配置在所有插件末尾最后执行,这样才能把上面提供的某些插件的Selective方法也同时增强!
>warning:配置老版本插件(<=1.1.2)时请把插件配置在所有插件末尾最后执行,这样才能把上面提供的某些插件的Selective方法也同时增强!
>warning:以前老版本(<=1.1.2)插件处理需要指定的列时是放入Model中指定的,但在实际使用过程中有同事反馈这个处理有点反直觉,导致某些新同事不能及时找到对应方法,而且和增强的SelectSelectivePlugin以及UpsertSelective使用方式都不一致,所以统一修改之。
插件:
```xml
<!-- Selective选择插入更新增强插件!请配在所有插件末尾以便最后执行 -->
<!-- Selective选择插入更新增强插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.SelectiveEnhancedPlugin"/>
```
使用:
```java
public class Test {
public static void main(String[] args) {
// ------------------------------ 新版本(SelectiveEnhancedPlugin)--------------------------------
// 1. 指定插入或更新字段
Tb tb = new Tb.Builder()
.field1(1)
.field2("xx2")
.field3(1)
.createTime(new Date())
.build();
// 只插入或者更新field1,field2字段
this.tbMapper.insertSelective(tb, Tb.Column.field1, Tb.Column.field2);
this.tbMapper.updateByExampleSelective(
tb,
new TbExample()
.createCriteria()
.andIdEqualTo(1l)
.example(),
Tb.Column.field1, Tb.Column.field2
);
this.tbMapper.updateByPrimaryKeySelective(tb, Tb.Column.field1, Tb.Column.field2);
this.tbMapper.upsertSelective(tb, Tb.Column.field1, Tb.Column.field2);
this.tbMapper.upsertByExampleSelective(
tb,
new TbExample()
.createCriteria()
.andField3EqualTo(1)
.example(),
Tb.Column.field1, Tb.Column.field2
);
// 2. 更新某些字段为null
this.tbMapper.updateByPrimaryKeySelective(
new Tb.Builder()
.id(1l)
.field1(null) // 方便展示,不用设也可以
.build(),
Tb.Column.field1
);
// ------------------------------ 老版本(SelectiveEnhancedPlugin)--------------------------------
// 1. 指定插入或更新字段
Tb tb = new Tb.Builder()
.field1(1)
......@@ -724,7 +764,7 @@ Mybatis Generator是原生支持自定义注释的(commentGenerator配置type
</plugin>
</xml>
```
使用(参考模板):
使用([参考模板](https://github.com/itfsw/mybatis-generator-plugin/blob/master/src/main/resources/default-comment.ftl)):
```xml
<?xml version="1.0" encoding="UTF-8"?>
<template>
......
......@@ -17,13 +17,16 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.*;
import com.itfsw.mybatis.generator.plugins.utils.hook.IIncrementsPluginHook;
import com.itfsw.mybatis.generator.plugins.utils.hook.IModelBuilderPluginHook;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Element;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import org.mybatis.generator.internal.util.JavaBeansUtil;
import java.util.ArrayList;
......@@ -37,7 +40,7 @@ import java.util.List;
* @time:2017/6/19 15:20
* ---------------------------------------------------------------------------
*/
public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginHook {
public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginHook, IIncrementsPluginHook {
public static final String PRO_INCREMENTS_COLUMNS = "incrementsColumns"; // incrementsColumns property
public static final String FIELD_INC_MAP = "incrementsColumnsInfoMap"; // 为了防止和用户数据库字段冲突,特殊命名
public static final String METHOD_INC_CHECK = "hasIncsForColumn"; // inc 检查方法名称
......@@ -234,6 +237,58 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
return true;
}
// =============================================== IIncrementsPluginHook ===================================================
/**
* 生成增量操作节点
* @param introspectedColumn
* @param prefix
* @param hasComma
* @return
*/
@Override
public List<Element> incrementElementGenerated(IntrospectedColumn introspectedColumn, String prefix, boolean hasComma) {
List<Element> list = new ArrayList<>();
if (incTools.supportColumn(introspectedColumn)){
// 1. column = 节点
list.add(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = "));
// 2. 选择节点
// 条件
XmlElement choose = new XmlElement("choose");
// 没有启用增量操作
XmlElement when = new XmlElement("when");
when.addAttribute(new Attribute(
"test",
(prefix != null ? prefix : "_parameter.") + IncrementsPlugin.METHOD_INC_CHECK
+ "('" + MyBatis3FormattingUtilities.escapeStringForMyBatis3(introspectedColumn.getActualColumnName()) + "')"
));
TextElement spec = new TextElement(
MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn)
+ " ${" + (prefix != null ? prefix : "")
+ IncrementsPlugin.FIELD_INC_MAP + "." + MyBatis3FormattingUtilities.escapeStringForMyBatis3(introspectedColumn.getActualColumnName()) + ".value} "
+ MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
when.addElement(spec);
choose.addElement(when);
// 启用了增量操作
XmlElement otherwise = new XmlElement("otherwise");
TextElement normal = new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
otherwise.addElement(normal);
choose.addElement(otherwise);
list.add(choose);
// 3. 结尾逗号
if (hasComma) {
list.add(new TextElement(","));
}
}
return list;
}
// =================================================== 原生方法的支持 ====================================================
/**
......@@ -255,9 +310,10 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
String columnName = strs[0].trim();
IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
// 查找是否需要进行增量操作
if (incTools.supportColumn(introspectedColumn)) {
List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementElementGenerated(introspectedColumn, hasPrefix ? "record." : null, true);
if (!incrementEles.isEmpty()) {
xmlElement.getElements().clear();
xmlElement.getElements().addAll(incTools.generatedIncrementsElement(introspectedColumn, hasPrefix ? "record." : null, true));
xmlElement.getElements().addAll(incrementEles);
}
}
}
......@@ -284,8 +340,9 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
String columnName = text.split("=")[0].trim();
IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
// 查找判断是否需要进行节点替换
if (incTools.supportColumn(introspectedColumn)) {
newEles.addAll(incTools.generatedIncrementsElement(introspectedColumn, hasPrefix ? "record." : null, text.endsWith(",")));
List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementElementGenerated(introspectedColumn, hasPrefix ? "record." : null, text.endsWith(","));
if (!incrementEles.isEmpty()) {
newEles.addAll(incrementEles);
continue;
}
......
......@@ -21,11 +21,6 @@ import com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Element;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.internal.util.StringUtility;
......@@ -46,10 +41,9 @@ public class IncrementsPluginTools {
/**
* 构造函数
* @param context
* @param introspectedTable
*/
private IncrementsPluginTools(Context context, IntrospectedTable introspectedTable) {
private IncrementsPluginTools(IntrospectedTable introspectedTable) {
this.introspectedTable = introspectedTable;
}
......@@ -61,7 +55,7 @@ public class IncrementsPluginTools {
* @return
*/
public static IncrementsPluginTools getTools(Context context, IntrospectedTable introspectedTable, List<String> warnings) {
IncrementsPluginTools tools = new IncrementsPluginTools(context, introspectedTable);
IncrementsPluginTools tools = new IncrementsPluginTools(introspectedTable);
// 判断是否启用了插件
if (PluginTools.getPluginConfiguration(context, IncrementsPlugin.class) != null) {
String incrementsColumns = introspectedTable.getTableConfigurationProperty(IncrementsPlugin.PRO_INCREMENTS_COLUMNS);
......@@ -119,54 +113,4 @@ public class IncrementsPluginTools {
}
return false;
}
/**
* 生成增量操作节点
* @param introspectedColumn
* @param prefix
* @param hasComma
*/
public List<Element> generatedIncrementsElement(IntrospectedColumn introspectedColumn, String prefix, boolean hasComma) {
List<Element> list = new ArrayList<>();
// 1. column = 节点
list.add(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = "));
// 2. 选择节点
// 条件
XmlElement choose = new XmlElement("choose");
// 没有启用增量操作
XmlElement when = new XmlElement("when");
when.addAttribute(new Attribute(
"test",
(prefix != null ? prefix : "_parameter.") + IncrementsPlugin.METHOD_INC_CHECK
+ "('" + MyBatis3FormattingUtilities.escapeStringForMyBatis3(introspectedColumn.getActualColumnName()) + "')"
));
TextElement spec = new TextElement(
MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn)
+ " ${" + (prefix != null ? prefix : "")
+ IncrementsPlugin.FIELD_INC_MAP + "." + MyBatis3FormattingUtilities.escapeStringForMyBatis3(introspectedColumn.getActualColumnName()) + ".value} "
+ MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
when.addElement(spec);
choose.addElement(when);
// 启用了增量操作
XmlElement otherwise = new XmlElement("otherwise");
TextElement normal = new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
otherwise.addElement(normal);
choose.addElement(otherwise);
list.add(choose);
// 3. 结尾逗号
if (hasComma) {
list.add(new TextElement(","));
}
return list;
}
}
......@@ -43,15 +43,14 @@ public class PluginTools {
* @param <T>
* @return
*/
public static <T> T getHook(Class<T> clazz){
public static <T> T getHook(Class<T> clazz) {
return (T) HookAggregator.getInstance();
}
/**
* 检查插件依赖
*
* @param context 上下文
* @param plugin 插件
* @param context 上下文
* @param plugin 插件
* @return
*/
public static boolean checkDependencyPlugin(Context context, Class plugin) {
......@@ -60,10 +59,8 @@ public class PluginTools {
/**
* 获取插件所在位置
*
* @param context 上下文
* @param plugin 插件
*
* @param plugin 插件
* @return -1:未找到
*/
public static int getPluginIndex(Context context, Class plugin) {
......@@ -97,40 +94,15 @@ public class PluginTools {
/**
* 获取插件配置
*
* @param context 上下文
* @param plugin 插件
* @param plugin 插件
* @return
*/
public static PluginConfiguration getPluginConfiguration(Context context, Class plugin){
public static PluginConfiguration getPluginConfiguration(Context context, Class plugin) {
int index = getPluginIndex(context, plugin);
if (index > -1){
if (index > -1) {
return getConfigPlugins(context).get(index);
}
return null;
}
/**
* 插件位置需要配置在某些插件后面
*
* @param context
* @param plugin
* @param warnings
* @param plugins
* @return
*/
public static boolean shouldAfterPlugins(Context context, Class plugin, List<String> warnings, Class ... plugins){
int index = getPluginIndex(context, plugin);
if (plugins != null){
for (Class cls : plugins){
int index1 = getPluginIndex(context, cls);
if (index1 != -1 && index1 >= index){
warnings.add("itfsw:插件" + plugin.getTypeName() + "插件建议配置在插件"+cls.getTypeName()+"后面,否则某些功能可能得不到增强!");
return false;
}
}
return true;
}
return false;
}
}
......@@ -16,6 +16,7 @@
package com.itfsw.mybatis.generator.plugins.utils;
import com.itfsw.mybatis.generator.plugins.utils.hook.IIncrementsPluginHook;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.OutputUtilities;
......@@ -133,20 +134,6 @@ public class XmlElementGeneratorTools {
}
}
/**
* 移除 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
* @param element
* @param introspectedTable
*/
public static void removeUseGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable) {
GeneratedKey gk = introspectedTable.getGeneratedKey();
if (gk != null) {
removeAttribute(element, "useGeneratedKeys");
removeAttribute(element, "keyProperty");
removeAttribute(element, "keyColumn");
}
}
/**
* 移除属性
* @param element
......@@ -211,7 +198,7 @@ public class XmlElementGeneratorTools {
* @param bracket
* @return
*/
public static List<Element> generateKeys(List<IntrospectedColumn> columns,String prefix, boolean bracket) {
public static List<Element> generateKeys(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
return generateCommColumns(columns, prefix, bracket, 1);
}
......@@ -396,9 +383,20 @@ public class XmlElementGeneratorTools {
switch (type) {
case 3:
sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
sb.append(" = ");
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementElementGenerated(introspectedColumn, prefix, columnIterator.hasNext());
if (incrementEles.isEmpty()) {
// 增量插件支持
if (sb.length() > 0) {
list.add(new TextElement(sb.toString()));
sb.setLength(0);
}
list.addAll(incrementEles);
} else {
sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
sb.append(" = ");
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
}
break;
case 2:
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
......@@ -453,7 +451,7 @@ public class XmlElementGeneratorTools {
* @param bracket
* @return
*/
private static XmlElement generateTrim(boolean bracket){
private static XmlElement generateTrim(boolean bracket) {
XmlElement trimEle = new XmlElement("trim");
if (bracket) {
trimEle.addAttribute(new Attribute("prefix", "("));
......@@ -495,12 +493,20 @@ public class XmlElementGeneratorTools {
* @param element
* @param introspectedColumn
* @param prefix
* @param type 1:key,2:value,3:set
* @param type 1:key,2:value,3:set
*/
private static void generateSelectiveCommColumnTo(XmlElement element, IntrospectedColumn introspectedColumn, String prefix, int type){
private static void generateSelectiveCommColumnTo(XmlElement element, IntrospectedColumn introspectedColumn, String prefix, int type) {
switch (type) {
case 3:
element.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " + MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementElementGenerated(introspectedColumn, prefix, true);
if (incrementEles.isEmpty()) {
// 增量插件支持
for (Element ele : incrementEles) {
element.addElement(ele);
}
} else {
element.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " + MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
}
break;
case 2:
element.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
......
......@@ -24,6 +24,7 @@ import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Element;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.Context;
import org.slf4j.Logger;
......@@ -95,7 +96,17 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo
// ============================================= IIncrementsPluginHook ==============================================
@Override
public List<Element> incrementElementGenerated(IntrospectedColumn introspectedColumn, String prefix, boolean hasComma) {
if (this.getPlugins(IIncrementsPluginHook.class).isEmpty()) {
return new ArrayList<>();
} else {
return this.getPlugins(IIncrementsPluginHook.class).get(0).incrementElementGenerated(introspectedColumn, prefix, hasComma);
}
}
// ============================================ IModelBuilderPluginHook =============================================
@Override
public boolean modelBuilderClassGenerated(TopLevelClass topLevelClass, InnerClass builderClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
for (IModelBuilderPluginHook plugin : this.getPlugins(IModelBuilderPluginHook.class)) {
......@@ -107,6 +118,7 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo
}
// ================================================= IUpsertPluginHook ===============================================
@Override
public boolean clientUpsertSelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
for (IUpsertPluginHook plugin : this.getPlugins(IUpsertPluginHook.class)) {
......
......@@ -16,6 +16,11 @@
package com.itfsw.mybatis.generator.plugins.utils.hook;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.xml.Element;
import java.util.List;
/**
* ---------------------------------------------------------------------------
*
......@@ -25,4 +30,12 @@ package com.itfsw.mybatis.generator.plugins.utils.hook;
* ---------------------------------------------------------------------------
*/
public interface IIncrementsPluginHook {
/**
* 生成增量操作节点
* @param introspectedColumn
* @param prefix
* @param hasComma
* @return
*/
List<Element> incrementElementGenerated(IntrospectedColumn introspectedColumn, String prefix, boolean hasComma);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment