Commit 192cf209 authored by hewei's avatar hewei

bugfix[issues#24]:在单独使用某些插件,Mapper接口没有import的问题;

parent a50ef78c
...@@ -16,10 +16,7 @@ ...@@ -16,10 +16,7 @@
package com.itfsw.mybatis.generator.plugins; package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin; import com.itfsw.mybatis.generator.plugins.utils.*;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*; import org.mybatis.generator.api.dom.java.*;
...@@ -88,7 +85,7 @@ public class BatchInsertPlugin extends BasePlugin { ...@@ -88,7 +85,7 @@ public class BatchInsertPlugin extends BasePlugin {
); );
commentGenerator.addGeneralMethodComment(mBatchInsert, introspectedTable); commentGenerator.addGeneralMethodComment(mBatchInsert, introspectedTable);
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mBatchInsert); FormatTools.addMethodWithBestPosition(interfaze, mBatchInsert);
logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsert方法。"); logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsert方法。");
// 2. batchInsertSelective // 2. batchInsertSelective
...@@ -102,7 +99,7 @@ public class BatchInsertPlugin extends BasePlugin { ...@@ -102,7 +99,7 @@ public class BatchInsertPlugin extends BasePlugin {
); );
commentGenerator.addGeneralMethodComment(mBatchInsertSelective, introspectedTable); commentGenerator.addGeneralMethodComment(mBatchInsertSelective, introspectedTable);
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mBatchInsertSelective); FormatTools.addMethodWithBestPosition(interfaze, mBatchInsertSelective);
logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsertSelective方法。"); logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsertSelective方法。");
return true; return true;
......
package com.itfsw.mybatis.generator.plugins; package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin; import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import com.itfsw.mybatis.generator.plugins.utils.FormatTools;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools; import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
import com.itfsw.mybatis.generator.plugins.utils.enhanced.InnerInterface; import com.itfsw.mybatis.generator.plugins.utils.enhanced.InnerInterface;
import com.itfsw.mybatis.generator.plugins.utils.enhanced.InnerInterfaceWrapperToInnerClass; import com.itfsw.mybatis.generator.plugins.utils.enhanced.InnerInterfaceWrapperToInnerClass;
...@@ -91,7 +92,7 @@ public class ExampleEnhancedPlugin extends BasePlugin { ...@@ -91,7 +92,7 @@ public class ExampleEnhancedPlugin extends BasePlugin {
exampleMethod, exampleMethod,
"return this.example;" "return this.example;"
); );
innerClass.addMethod(exampleMethod); FormatTools.addMethodWithBestPosition(innerClass, exampleMethod);
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "增加工厂方法example"); logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "增加工厂方法example");
} }
...@@ -116,7 +117,7 @@ public class ExampleEnhancedPlugin extends BasePlugin { ...@@ -116,7 +117,7 @@ public class ExampleEnhancedPlugin extends BasePlugin {
new Parameter(innerClass.getType(), "add") new Parameter(innerClass.getType(), "add")
); );
commentGenerator.addGeneralMethodComment(addMethod, introspectedTable); commentGenerator.addGeneralMethodComment(addMethod, introspectedTable);
criteriaAddInterface.addMethod(addMethod); FormatTools.addMethodWithBestPosition(criteriaAddInterface, addMethod);
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "." + criteriaAddInterface.getType().getShortName() + "增加方法add"); logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "." + criteriaAddInterface.getType().getShortName() + "增加方法add");
InnerClass innerClassWrapper = new InnerInterfaceWrapperToInnerClass(criteriaAddInterface); InnerClass innerClassWrapper = new InnerInterfaceWrapperToInnerClass(criteriaAddInterface);
...@@ -140,7 +141,7 @@ public class ExampleEnhancedPlugin extends BasePlugin { ...@@ -140,7 +141,7 @@ public class ExampleEnhancedPlugin extends BasePlugin {
"}", "}",
"return this;" "return this;"
); );
innerClass.addMethod(andIfMethod); FormatTools.addMethodWithBestPosition(innerClass, andIfMethod);
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "增加方法andIf"); logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "增加方法andIf");
} }
...@@ -163,7 +164,7 @@ public class ExampleEnhancedPlugin extends BasePlugin { ...@@ -163,7 +164,7 @@ public class ExampleEnhancedPlugin extends BasePlugin {
"this.setOrderByClause(orderByClause);", "this.setOrderByClause(orderByClause);",
"return this;" "return this;"
); );
topLevelClass.addMethod(orderByMethod); FormatTools.addMethodWithBestPosition(topLevelClass, orderByMethod);
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "增加方法orderBy"); logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "增加方法orderBy");
// 添加orderBy // 添加orderBy
...@@ -187,7 +188,7 @@ public class ExampleEnhancedPlugin extends BasePlugin { ...@@ -187,7 +188,7 @@ public class ExampleEnhancedPlugin extends BasePlugin {
"return this;" "return this;"
); );
topLevelClass.addMethod(orderByMethod1); FormatTools.addMethodWithBestPosition(topLevelClass, orderByMethod1);
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "增加方法orderBy(String ... orderByClauses)"); logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "增加方法orderBy(String ... orderByClauses)");
} }
} }
...@@ -177,11 +177,11 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH ...@@ -177,11 +177,11 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
mInc.addBodyLine("this.value = value;"); mInc.addBodyLine("this.value = value;");
mInc.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "value")); mInc.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "value"));
commentGenerator.addGeneralMethodComment(mInc, introspectedTable); commentGenerator.addGeneralMethodComment(mInc, introspectedTable);
eIncrements.addMethod(mInc); FormatTools.addMethodWithBestPosition(eIncrements, mInc);
Method mValue = JavaElementGeneratorTools.generateGetterMethod(fValue); Method mValue = JavaElementGeneratorTools.generateGetterMethod(fValue);
commentGenerator.addGeneralMethodComment(mValue, introspectedTable); commentGenerator.addGeneralMethodComment(mValue, introspectedTable);
eIncrements.addMethod(mValue); FormatTools.addMethodWithBestPosition(eIncrements, mValue);
builderClass.addInnerEnum(eIncrements); builderClass.addInnerEnum(eIncrements);
// 增加field // 增加field
...@@ -198,10 +198,10 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH ...@@ -198,10 +198,10 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
// getter&setter // getter&setter
Method mGetter = JavaElementGeneratorTools.generateGetterMethod(fIncrements); Method mGetter = JavaElementGeneratorTools.generateGetterMethod(fIncrements);
commentGenerator.addGetterComment(mGetter, introspectedTable, null); commentGenerator.addGetterComment(mGetter, introspectedTable, null);
topLevelClass.addMethod(mGetter); FormatTools.addMethodWithBestPosition(topLevelClass, mGetter);
Method mSetter = JavaElementGeneratorTools.generateSetterMethod(fIncrements); Method mSetter = JavaElementGeneratorTools.generateSetterMethod(fIncrements);
commentGenerator.addSetterComment(mSetter, introspectedTable, null); commentGenerator.addSetterComment(mSetter, introspectedTable, null);
topLevelClass.addMethod(mSetter); FormatTools.addMethodWithBestPosition(topLevelClass, mSetter);
// 增加判断方法 // 增加判断方法
Method mHasIncsForColumn = JavaElementGeneratorTools.generateMethod( Method mHasIncsForColumn = JavaElementGeneratorTools.generateMethod(
IncrementsPlugin.METHOD_INC_CHECK, IncrementsPlugin.METHOD_INC_CHECK,
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package com.itfsw.mybatis.generator.plugins; package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin; import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import com.itfsw.mybatis.generator.plugins.utils.FormatTools;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools; import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*; import org.mybatis.generator.api.dom.java.*;
...@@ -41,8 +42,8 @@ public class LimitPlugin extends BasePlugin { ...@@ -41,8 +42,8 @@ public class LimitPlugin extends BasePlugin {
@Override @Override
public boolean validate(List<String> warnings) { public boolean validate(List<String> warnings) {
// 该插件只支持MYSQL // 该插件只支持MYSQL
if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false){ if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false) {
warnings.add("itfsw:插件"+this.getClass().getTypeName()+"只支持MySQL数据库!"); warnings.add("itfsw:插件" + this.getClass().getTypeName() + "只支持MySQL数据库!");
return false; return false;
} }
return super.validate(warnings); return super.validate(warnings);
...@@ -51,7 +52,6 @@ public class LimitPlugin extends BasePlugin { ...@@ -51,7 +52,6 @@ public class LimitPlugin extends BasePlugin {
/** /**
* ModelExample Methods 生成 * ModelExample Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param topLevelClass * @param topLevelClass
* @param introspectedTable * @param introspectedTable
* @return * @return
...@@ -77,25 +77,25 @@ public class LimitPlugin extends BasePlugin { ...@@ -77,25 +77,25 @@ public class LimitPlugin extends BasePlugin {
); );
commentGenerator.addFieldComment(rowsField, introspectedTable); commentGenerator.addFieldComment(rowsField, introspectedTable);
topLevelClass.addField(rowsField); topLevelClass.addField(rowsField);
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加offset和rows字段"); logger.debug("itfsw(MySQL分页插件):" + topLevelClass.getType().getShortName() + "增加offset和rows字段");
// 增加getter && setter 方法 // 增加getter && setter 方法
Method mSetOffset = JavaElementGeneratorTools.generateSetterMethod(offsetField); Method mSetOffset = JavaElementGeneratorTools.generateSetterMethod(offsetField);
commentGenerator.addGeneralMethodComment(mSetOffset, introspectedTable); commentGenerator.addGeneralMethodComment(mSetOffset, introspectedTable);
topLevelClass.addMethod(mSetOffset); FormatTools.addMethodWithBestPosition(topLevelClass, mSetOffset);
Method mGetOffset = JavaElementGeneratorTools.generateGetterMethod(offsetField); Method mGetOffset = JavaElementGeneratorTools.generateGetterMethod(offsetField);
commentGenerator.addGeneralMethodComment(mGetOffset, introspectedTable); commentGenerator.addGeneralMethodComment(mGetOffset, introspectedTable);
topLevelClass.addMethod(mGetOffset); FormatTools.addMethodWithBestPosition(topLevelClass, mGetOffset);
Method mSetRows = JavaElementGeneratorTools.generateSetterMethod(rowsField); Method mSetRows = JavaElementGeneratorTools.generateSetterMethod(rowsField);
commentGenerator.addGeneralMethodComment(mSetRows, introspectedTable); commentGenerator.addGeneralMethodComment(mSetRows, introspectedTable);
topLevelClass.addMethod(mSetRows); FormatTools.addMethodWithBestPosition(topLevelClass, mSetRows);
Method mGetRows = JavaElementGeneratorTools.generateGetterMethod(rowsField); Method mGetRows = JavaElementGeneratorTools.generateGetterMethod(rowsField);
commentGenerator.addGeneralMethodComment(mGetRows, introspectedTable); commentGenerator.addGeneralMethodComment(mGetRows, introspectedTable);
topLevelClass.addMethod(mGetRows); FormatTools.addMethodWithBestPosition(topLevelClass, mGetRows);
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加offset和rows的getter和setter实现。"); logger.debug("itfsw(MySQL分页插件):" + topLevelClass.getType().getShortName() + "增加offset和rows的getter和setter实现。");
// 提供几个快捷方法 // 提供几个快捷方法
Method setLimit = JavaElementGeneratorTools.generateMethod( Method setLimit = JavaElementGeneratorTools.generateMethod(
...@@ -110,7 +110,7 @@ public class LimitPlugin extends BasePlugin { ...@@ -110,7 +110,7 @@ public class LimitPlugin extends BasePlugin {
"this.rows = rows;", "this.rows = rows;",
"return this;" "return this;"
); );
topLevelClass.addMethod(setLimit); FormatTools.addMethodWithBestPosition(topLevelClass, setLimit);
Method setLimit2 = JavaElementGeneratorTools.generateMethod( Method setLimit2 = JavaElementGeneratorTools.generateMethod(
"limit", "limit",
...@@ -126,8 +126,8 @@ public class LimitPlugin extends BasePlugin { ...@@ -126,8 +126,8 @@ public class LimitPlugin extends BasePlugin {
"this.rows = rows;", "this.rows = rows;",
"return this;" "return this;"
); );
topLevelClass.addMethod(setLimit2); FormatTools.addMethodWithBestPosition(topLevelClass, setLimit2);
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加limit方法。"); logger.debug("itfsw(MySQL分页插件):" + topLevelClass.getType().getShortName() + "增加limit方法。");
Method setPage = JavaElementGeneratorTools.generateMethod( Method setPage = JavaElementGeneratorTools.generateMethod(
"page", "page",
...@@ -143,16 +143,16 @@ public class LimitPlugin extends BasePlugin { ...@@ -143,16 +143,16 @@ public class LimitPlugin extends BasePlugin {
"this.rows = pageSize;", "this.rows = pageSize;",
"return this;" "return this;"
); );
topLevelClass.addMethod(setPage); FormatTools.addMethodWithBestPosition(topLevelClass, setPage);
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"增加page方法"); logger.debug("itfsw(MySQL分页插件):" + topLevelClass.getType().getShortName() + "增加page方法");
// !!! clear 方法增加 offset 和 rows的清理 // !!! clear 方法增加 offset 和 rows的清理
List<Method> methodList = topLevelClass.getMethods(); List<Method> methodList = topLevelClass.getMethods();
for (Method method: methodList){ for (Method method : methodList) {
if (method.getName().equals("clear")){ if (method.getName().equals("clear")) {
method.addBodyLine("rows = null;"); method.addBodyLine("rows = null;");
method.addBodyLine("offset = null;"); method.addBodyLine("offset = null;");
logger.debug("itfsw(MySQL分页插件):"+topLevelClass.getType().getShortName()+"修改clear方法,增加rows和offset字段的清空"); logger.debug("itfsw(MySQL分页插件):" + topLevelClass.getType().getShortName() + "修改clear方法,增加rows和offset字段的清空");
} }
} }
...@@ -162,7 +162,6 @@ public class LimitPlugin extends BasePlugin { ...@@ -162,7 +162,6 @@ public class LimitPlugin extends BasePlugin {
/** /**
* SQL Map Methods 生成 * SQL Map Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param element * @param element
* @param introspectedTable * @param introspectedTable
* @return * @return
...@@ -176,7 +175,6 @@ public class LimitPlugin extends BasePlugin { ...@@ -176,7 +175,6 @@ public class LimitPlugin extends BasePlugin {
/** /**
* SQL Map Methods 生成 * SQL Map Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param element * @param element
* @param introspectedTable * @param introspectedTable
* @return * @return
...@@ -189,11 +187,10 @@ public class LimitPlugin extends BasePlugin { ...@@ -189,11 +187,10 @@ public class LimitPlugin extends BasePlugin {
/** /**
* 生成limit节点 * 生成limit节点
*
* @param element * @param element
* @param introspectedTable * @param introspectedTable
*/ */
public void generateLimitElement(XmlElement element, IntrospectedTable introspectedTable){ public void generateLimitElement(XmlElement element, IntrospectedTable introspectedTable) {
XmlElement ifLimitNotNullElement = new XmlElement("if"); XmlElement ifLimitNotNullElement = new XmlElement("if");
ifLimitNotNullElement.addAttribute(new Attribute("test", "rows != null")); ifLimitNotNullElement.addAttribute(new Attribute("test", "rows != null"));
...@@ -209,6 +206,6 @@ public class LimitPlugin extends BasePlugin { ...@@ -209,6 +206,6 @@ public class LimitPlugin extends BasePlugin {
element.addElement(ifLimitNotNullElement); element.addElement(ifLimitNotNullElement);
logger.debug("itfsw(MySQL分页插件):"+introspectedTable.getMyBatis3XmlMapperFileName()+"selectByExample方法增加分页条件。"); logger.debug("itfsw(MySQL分页插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "selectByExample方法增加分页条件。");
} }
} }
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package com.itfsw.mybatis.generator.plugins; package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin; import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import com.itfsw.mybatis.generator.plugins.utils.FormatTools;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools; import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
import com.itfsw.mybatis.generator.plugins.utils.PluginTools; import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
import com.itfsw.mybatis.generator.plugins.utils.enhanced.InnerTypeFullyQualifiedJavaType; import com.itfsw.mybatis.generator.plugins.utils.enhanced.InnerTypeFullyQualifiedJavaType;
...@@ -116,7 +117,7 @@ public class ModelBuilderPlugin extends BasePlugin { ...@@ -116,7 +117,7 @@ public class ModelBuilderPlugin extends BasePlugin {
commentGenerator.addGeneralMethodComment(builder, introspectedTable); commentGenerator.addGeneralMethodComment(builder, introspectedTable);
builder.setStatic(true); builder.setStatic(true);
builder.addBodyLine("return new " + builderType.getShortName() + "();"); builder.addBodyLine("return new " + builderType.getShortName() + "();");
topLevelClass.addMethod(builder); FormatTools.addMethodWithBestPosition(topLevelClass, builder);
commentGenerator.addClassComment(innerClass, introspectedTable); commentGenerator.addClassComment(innerClass, introspectedTable);
logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + "增加内部Builder类。"); logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + "增加内部Builder类。");
...@@ -132,7 +133,7 @@ public class ModelBuilderPlugin extends BasePlugin { ...@@ -132,7 +133,7 @@ public class ModelBuilderPlugin extends BasePlugin {
constructor.setConstructor(true); constructor.setConstructor(true);
constructor.addBodyLine(new StringBuilder("this.obj = new ").append(topLevelClass.getType().getShortName()).append("();").toString()); constructor.addBodyLine(new StringBuilder("this.obj = new ").append(topLevelClass.getType().getShortName()).append("();").toString());
commentGenerator.addGeneralMethodComment(constructor, introspectedTable); commentGenerator.addGeneralMethodComment(constructor, introspectedTable);
innerClass.addMethod(constructor); FormatTools.addMethodWithBestPosition(innerClass, constructor);
logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加的构造方法。"); logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加的构造方法。");
for (IntrospectedColumn introspectedColumn : columns) { for (IntrospectedColumn introspectedColumn : columns) {
...@@ -154,7 +155,7 @@ public class ModelBuilderPlugin extends BasePlugin { ...@@ -154,7 +155,7 @@ public class ModelBuilderPlugin extends BasePlugin {
// hook // hook
if (PluginTools.getHook(IModelBuilderPluginHook.class).modelBuilderSetterMethodGenerated(method, topLevelClass, innerClass, introspectedColumn, introspectedTable)) { if (PluginTools.getHook(IModelBuilderPluginHook.class).modelBuilderSetterMethodGenerated(method, topLevelClass, innerClass, introspectedColumn, introspectedTable)) {
innerClass.addMethod(method); FormatTools.addMethodWithBestPosition(innerClass, method);
logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加" + method.getName() + "方法(复合主键)。"); logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加" + method.getName() + "方法(复合主键)。");
} }
} }
...@@ -166,7 +167,7 @@ public class ModelBuilderPlugin extends BasePlugin { ...@@ -166,7 +167,7 @@ public class ModelBuilderPlugin extends BasePlugin {
); );
build.addBodyLine("return this.obj;"); build.addBodyLine("return this.obj;");
commentGenerator.addGeneralMethodComment(build, introspectedTable); commentGenerator.addGeneralMethodComment(build, introspectedTable);
innerClass.addMethod(build); FormatTools.addMethodWithBestPosition(innerClass, build);
logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加build方法。"); logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加build方法。");
// hook // hook
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package com.itfsw.mybatis.generator.plugins; package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin; import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import com.itfsw.mybatis.generator.plugins.utils.FormatTools;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools; import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.IntrospectedTable;
...@@ -110,22 +111,22 @@ public class ModelColumnPlugin extends BasePlugin { ...@@ -110,22 +111,22 @@ public class ModelColumnPlugin extends BasePlugin {
mValue.setReturnType(FullyQualifiedJavaType.getStringInstance()); mValue.setReturnType(FullyQualifiedJavaType.getStringInstance());
mValue.addBodyLine("return this.column;"); mValue.addBodyLine("return this.column;");
commentGenerator.addGeneralMethodComment(mValue, introspectedTable); commentGenerator.addGeneralMethodComment(mValue, introspectedTable);
innerEnum.addMethod(mValue); FormatTools.addMethodWithBestPosition(innerEnum, mValue);
Method mGetValue = new Method("getValue"); Method mGetValue = new Method("getValue");
mGetValue.setVisibility(JavaVisibility.PUBLIC); mGetValue.setVisibility(JavaVisibility.PUBLIC);
mGetValue.setReturnType(FullyQualifiedJavaType.getStringInstance()); mGetValue.setReturnType(FullyQualifiedJavaType.getStringInstance());
mGetValue.addBodyLine("return this.column;"); mGetValue.addBodyLine("return this.column;");
commentGenerator.addGeneralMethodComment(mGetValue, introspectedTable); commentGenerator.addGeneralMethodComment(mGetValue, introspectedTable);
innerEnum.addMethod(mGetValue); FormatTools.addMethodWithBestPosition(innerEnum, mGetValue);
Method mGetJavaProperty = JavaElementGeneratorTools.generateGetterMethod(javaPropertyField); Method mGetJavaProperty = JavaElementGeneratorTools.generateGetterMethod(javaPropertyField);
commentGenerator.addGeneralMethodComment(mGetJavaProperty, introspectedTable); commentGenerator.addGeneralMethodComment(mGetJavaProperty, introspectedTable);
innerEnum.addMethod(mGetJavaProperty); FormatTools.addMethodWithBestPosition(innerEnum, mGetJavaProperty);
Method mGetJdbcType = JavaElementGeneratorTools.generateGetterMethod(jdbcTypeField); Method mGetJdbcType = JavaElementGeneratorTools.generateGetterMethod(jdbcTypeField);
commentGenerator.addGeneralMethodComment(mGetJdbcType, introspectedTable); commentGenerator.addGeneralMethodComment(mGetJdbcType, introspectedTable);
innerEnum.addMethod(mGetJdbcType); FormatTools.addMethodWithBestPosition(innerEnum, mGetJdbcType);
Method constructor = new Method(ENUM_NAME); Method constructor = new Method(ENUM_NAME);
constructor.setConstructor(true); constructor.setConstructor(true);
...@@ -136,7 +137,7 @@ public class ModelColumnPlugin extends BasePlugin { ...@@ -136,7 +137,7 @@ public class ModelColumnPlugin extends BasePlugin {
constructor.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "javaProperty")); constructor.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "javaProperty"));
constructor.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "jdbcType")); constructor.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "jdbcType"));
commentGenerator.addGeneralMethodComment(constructor, introspectedTable); commentGenerator.addGeneralMethodComment(constructor, introspectedTable);
innerEnum.addMethod(constructor); FormatTools.addMethodWithBestPosition(innerEnum, constructor);
logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + ".Column增加构造方法和column属性。"); logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + ".Column增加构造方法和column属性。");
// Enum枚举 // Enum枚举
...@@ -163,14 +164,14 @@ public class ModelColumnPlugin extends BasePlugin { ...@@ -163,14 +164,14 @@ public class ModelColumnPlugin extends BasePlugin {
desc.setReturnType(FullyQualifiedJavaType.getStringInstance()); desc.setReturnType(FullyQualifiedJavaType.getStringInstance());
desc.addBodyLine("return this.column + \" DESC\";"); desc.addBodyLine("return this.column + \" DESC\";");
commentGenerator.addGeneralMethodComment(desc, introspectedTable); commentGenerator.addGeneralMethodComment(desc, introspectedTable);
innerEnum.addMethod(desc); FormatTools.addMethodWithBestPosition(innerEnum, desc);
Method asc = new Method("asc"); Method asc = new Method("asc");
asc.setVisibility(JavaVisibility.PUBLIC); asc.setVisibility(JavaVisibility.PUBLIC);
asc.setReturnType(FullyQualifiedJavaType.getStringInstance()); asc.setReturnType(FullyQualifiedJavaType.getStringInstance());
asc.addBodyLine("return this.column + \" ASC\";"); asc.addBodyLine("return this.column + \" ASC\";");
commentGenerator.addGeneralMethodComment(asc, introspectedTable); commentGenerator.addGeneralMethodComment(asc, introspectedTable);
innerEnum.addMethod(asc); FormatTools.addMethodWithBestPosition(innerEnum, asc);
logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + ".Column增加asc()和desc()方法。"); logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + ".Column增加asc()和desc()方法。");
return innerEnum; return innerEnum;
......
...@@ -16,10 +16,7 @@ ...@@ -16,10 +16,7 @@
package com.itfsw.mybatis.generator.plugins; package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin; import com.itfsw.mybatis.generator.plugins.utils.*;
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
import com.itfsw.mybatis.generator.plugins.utils.hook.IUpsertPluginHook; import com.itfsw.mybatis.generator.plugins.utils.hook.IUpsertPluginHook;
import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.IntrospectedTable;
...@@ -96,7 +93,7 @@ public class UpsertPlugin extends BasePlugin { ...@@ -96,7 +93,7 @@ public class UpsertPlugin extends BasePlugin {
); );
commentGenerator.addGeneralMethodComment(mUpsert, introspectedTable); commentGenerator.addGeneralMethodComment(mUpsert, introspectedTable);
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mUpsert); FormatTools.addMethodWithBestPosition(interfaze, mUpsert);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsert方法。"); logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsert方法。");
// ====================================== upsertWithBLOBs ====================================== // ====================================== upsertWithBLOBs ======================================
...@@ -110,7 +107,7 @@ public class UpsertPlugin extends BasePlugin { ...@@ -110,7 +107,7 @@ public class UpsertPlugin extends BasePlugin {
); );
commentGenerator.addGeneralMethodComment(mUpsertWithBLOBs, introspectedTable); commentGenerator.addGeneralMethodComment(mUpsertWithBLOBs, introspectedTable);
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mUpsertWithBLOBs); FormatTools.addMethodWithBestPosition(interfaze, mUpsertWithBLOBs);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsert方法。"); logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsert方法。");
} }
...@@ -127,7 +124,7 @@ public class UpsertPlugin extends BasePlugin { ...@@ -127,7 +124,7 @@ public class UpsertPlugin extends BasePlugin {
// hook // hook
if (PluginTools.getHook(IUpsertPluginHook.class).clientUpsertSelectiveMethodGenerated(mUpsertSelective, interfaze, introspectedTable)) { if (PluginTools.getHook(IUpsertPluginHook.class).clientUpsertSelectiveMethodGenerated(mUpsertSelective, interfaze, introspectedTable)) {
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mUpsertSelective); FormatTools.addMethodWithBestPosition(interfaze, mUpsertSelective);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertSelective方法。"); logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertSelective方法。");
} }
...@@ -142,7 +139,7 @@ public class UpsertPlugin extends BasePlugin { ...@@ -142,7 +139,7 @@ public class UpsertPlugin extends BasePlugin {
); );
commentGenerator.addGeneralMethodComment(mUpsertByExample, introspectedTable); commentGenerator.addGeneralMethodComment(mUpsertByExample, introspectedTable);
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mUpsertByExample); FormatTools.addMethodWithBestPosition(interfaze, mUpsertByExample);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExample方法。"); logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExample方法。");
// ====================================== upsertByExampleWithBLOBs ====================================== // ====================================== upsertByExampleWithBLOBs ======================================
...@@ -157,7 +154,7 @@ public class UpsertPlugin extends BasePlugin { ...@@ -157,7 +154,7 @@ public class UpsertPlugin extends BasePlugin {
); );
commentGenerator.addGeneralMethodComment(mUpsertByExampleWithBLOBs, introspectedTable); commentGenerator.addGeneralMethodComment(mUpsertByExampleWithBLOBs, introspectedTable);
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mUpsertByExampleWithBLOBs); FormatTools.addMethodWithBestPosition(interfaze, mUpsertByExampleWithBLOBs);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExample方法。"); logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExample方法。");
} }
...@@ -173,7 +170,7 @@ public class UpsertPlugin extends BasePlugin { ...@@ -173,7 +170,7 @@ public class UpsertPlugin extends BasePlugin {
// hook // hook
if (PluginTools.getHook(IUpsertPluginHook.class).clientUpsertByExampleSelectiveMethodGenerated(mUpsertByExampleSelective, interfaze, introspectedTable)) { if (PluginTools.getHook(IUpsertPluginHook.class).clientUpsertByExampleSelectiveMethodGenerated(mUpsertByExampleSelective, interfaze, introspectedTable)) {
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mUpsertByExampleSelective); FormatTools.addMethodWithBestPosition(interfaze, mUpsertByExampleSelective);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExampleSelective方法。"); logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExampleSelective方法。");
} }
} }
......
...@@ -16,12 +16,10 @@ ...@@ -16,12 +16,10 @@
package com.itfsw.mybatis.generator.plugins.utils; package com.itfsw.mybatis.generator.plugins.utils;
import com.itfsw.mybatis.generator.plugins.ModelColumnPlugin;
import org.mybatis.generator.api.CommentGenerator; import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.InnerClass; import org.mybatis.generator.api.dom.java.*;
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.Attribute; import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Element; import org.mybatis.generator.api.dom.xml.Element;
import org.mybatis.generator.api.dom.xml.TextElement; import org.mybatis.generator.api.dom.xml.TextElement;
...@@ -29,6 +27,8 @@ import org.mybatis.generator.api.dom.xml.XmlElement; ...@@ -29,6 +27,8 @@ import org.mybatis.generator.api.dom.xml.XmlElement;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.TreeSet;
/** /**
* --------------------------------------------------------------------------- * ---------------------------------------------------------------------------
...@@ -54,9 +54,43 @@ public class FormatTools { ...@@ -54,9 +54,43 @@ public class FormatTools {
* @param method * @param method
*/ */
public static void addMethodWithBestPosition(Interface interfacz, Method method) { public static void addMethodWithBestPosition(Interface interfacz, Method method) {
// import
Set<FullyQualifiedJavaType> importTypes = new TreeSet<>();
// 返回
if (method.getReturnType() != null) {
importTypes.add(method.getReturnType());
importTypes.addAll(method.getReturnType().getTypeArguments());
}
// 参数 比较特殊的是ModelColumn生成的Column
for (Parameter parameter : method.getParameters()) {
boolean flag = true;
for (String annotation : parameter.getAnnotations()) {
if (annotation.startsWith("@Param")) {
importTypes.add(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param"));
if (annotation.matches(".*selective.*") && parameter.getType().getShortName().equals(ModelColumnPlugin.ENUM_NAME)) {
flag = false;
}
}
}
if (flag) {
importTypes.add(parameter.getType());
importTypes.addAll(parameter.getType().getTypeArguments());
}
}
interfacz.addImportedTypes(importTypes);
addMethodWithBestPosition(method, interfacz.getMethods()); addMethodWithBestPosition(method, interfacz.getMethods());
} }
/**
* 在最佳位置添加方法
* @param innerEnum
* @param method
*/
public static void addMethodWithBestPosition(InnerEnum innerEnum, Method method) {
addMethodWithBestPosition(method, innerEnum.getMethods());
}
/** /**
* 在最佳位置添加方法 * 在最佳位置添加方法
* @param topLevelClass * @param topLevelClass
......
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