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方法增加分页条件。");
} }
} }
...@@ -165,7 +165,7 @@ public class LogicalDeletePlugin extends BasePlugin { ...@@ -165,7 +165,7 @@ public class LogicalDeletePlugin extends BasePlugin {
// 添加方法说明 // 添加方法说明
commentGenerator.addGeneralMethodComment(mLogicalDeleteByExample, introspectedTable); commentGenerator.addGeneralMethodComment(mLogicalDeleteByExample, introspectedTable);
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mLogicalDeleteByExample); FormatTools.addMethodWithBestPosition(interfaze, mLogicalDeleteByExample);
logger.debug("itfsw(逻辑删除插件):" + interfaze.getType().getShortName() + "增加方法logicalDeleteByExample。"); logger.debug("itfsw(逻辑删除插件):" + interfaze.getType().getShortName() + "增加方法logicalDeleteByExample。");
// 2. 判断是否有主键,生成主键删除方法 // 2. 判断是否有主键,生成主键删除方法
...@@ -192,7 +192,7 @@ public class LogicalDeletePlugin extends BasePlugin { ...@@ -192,7 +192,7 @@ public class LogicalDeletePlugin extends BasePlugin {
if (introspectedTable.getRules().generatePrimaryKeyClass()) { if (introspectedTable.getRules().generatePrimaryKeyClass()) {
FullyQualifiedJavaType type1 = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType()); FullyQualifiedJavaType type1 = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
importedTypes.add(type1); importedTypes.add(type1);
mLogicalDeleteByPrimaryKey.addParameter(new Parameter(type1, "key")); mLogicalDeleteByPrimaryKey.addParameter(new Parameter(type1, "key"));
mSelectByPrimaryKey.addParameter(new Parameter(type1, "key", "@Param(\"key\")")); mSelectByPrimaryKey.addParameter(new Parameter(type1, "key", "@Param(\"key\")"));
} else { } else {
// no primary key class - fields are in the base class // no primary key class - fields are in the base class
...@@ -229,7 +229,7 @@ public class LogicalDeletePlugin extends BasePlugin { ...@@ -229,7 +229,7 @@ public class LogicalDeletePlugin extends BasePlugin {
// interface 增加方法 // interface 增加方法
interfaze.addImportedTypes(importedTypes); interfaze.addImportedTypes(importedTypes);
interfaze.addMethod(mLogicalDeleteByPrimaryKey); FormatTools.addMethodWithBestPosition(interfaze, mLogicalDeleteByPrimaryKey);
logger.debug("itfsw(逻辑删除插件):" + interfaze.getType().getShortName() + "增加方法logicalDeleteByPrimaryKey。"); logger.debug("itfsw(逻辑删除插件):" + interfaze.getType().getShortName() + "增加方法logicalDeleteByPrimaryKey。");
// 增强selectByPrimaryKey // 增强selectByPrimaryKey
...@@ -256,13 +256,13 @@ public class LogicalDeletePlugin extends BasePlugin { ...@@ -256,13 +256,13 @@ public class LogicalDeletePlugin extends BasePlugin {
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) { public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
if (this.logicalDeleteColumn != null) { if (this.logicalDeleteColumn != null) {
// 1. 逻辑删除ByExample // 1. 逻辑删除ByExample
XmlElement logicalDeleteByExample = new XmlElement("update"); XmlElement logicalDeleteByExample = new XmlElement("update");
logicalDeleteByExample.addAttribute(new Attribute("id", METHOD_LOGICAL_DELETE_BY_EXAMPLE)); logicalDeleteByExample.addAttribute(new Attribute("id", METHOD_LOGICAL_DELETE_BY_EXAMPLE));
logicalDeleteByExample.addAttribute(new Attribute("parameterType", "map")); //$NON-NLS-2$ logicalDeleteByExample.addAttribute(new Attribute("parameterType", "map")); //$NON-NLS-2$
commentGenerator.addComment(logicalDeleteByExample); commentGenerator.addComment(logicalDeleteByExample);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("update "); sb.append("update ");
sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()); sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
sb.append(" set "); sb.append(" set ");
// 更新逻辑删除字段 // 更新逻辑删除字段
...@@ -294,7 +294,7 @@ public class LogicalDeletePlugin extends BasePlugin { ...@@ -294,7 +294,7 @@ public class LogicalDeletePlugin extends BasePlugin {
// 2. 判断是否有主键,生成主键删除方法 // 2. 判断是否有主键,生成主键删除方法
if (introspectedTable.hasPrimaryKeyColumns()) { if (introspectedTable.hasPrimaryKeyColumns()) {
XmlElement logicalDeleteByPrimaryKey = new XmlElement("update"); XmlElement logicalDeleteByPrimaryKey = new XmlElement("update");
logicalDeleteByPrimaryKey.addAttribute(new Attribute("id", METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY)); logicalDeleteByPrimaryKey.addAttribute(new Attribute("id", METHOD_LOGICAL_DELETE_BY_PRIMARY_KEY));
String parameterClass; String parameterClass;
...@@ -304,7 +304,7 @@ public class LogicalDeletePlugin extends BasePlugin { ...@@ -304,7 +304,7 @@ public class LogicalDeletePlugin extends BasePlugin {
// PK fields are in the base class. If more than on PK // PK fields are in the base class. If more than on PK
// field, then they are coming in a map. // field, then they are coming in a map.
if (introspectedTable.getPrimaryKeyColumns().size() > 1) { if (introspectedTable.getPrimaryKeyColumns().size() > 1) {
parameterClass = "map"; parameterClass = "map";
} else { } else {
parameterClass = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().toString(); parameterClass = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().toString();
} }
...@@ -313,7 +313,7 @@ public class LogicalDeletePlugin extends BasePlugin { ...@@ -313,7 +313,7 @@ public class LogicalDeletePlugin extends BasePlugin {
commentGenerator.addComment(logicalDeleteByPrimaryKey); commentGenerator.addComment(logicalDeleteByPrimaryKey);
StringBuilder sb1 = new StringBuilder(); StringBuilder sb1 = new StringBuilder();
sb1.append("update "); sb1.append("update ");
sb1.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()); sb1.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
sb1.append(" set "); sb1.append(" set ");
// 更新逻辑删除字段 // 更新逻辑删除字段
...@@ -346,7 +346,7 @@ public class LogicalDeletePlugin extends BasePlugin { ...@@ -346,7 +346,7 @@ public class LogicalDeletePlugin extends BasePlugin {
// 3. 增强selectByPrimaryKey // 3. 增强selectByPrimaryKey
XmlElement selectByPrimaryKey = new XmlElement("select"); XmlElement selectByPrimaryKey = new XmlElement("select");
commentGenerator.addComment(selectByPrimaryKey); commentGenerator.addComment(selectByPrimaryKey);
selectByPrimaryKey.addAttribute(new Attribute("id", METHOD_SELECT_BY_PRIMARY_KEY_WITH_LOGICAL_DELETE)); selectByPrimaryKey.addAttribute(new Attribute("id", METHOD_SELECT_BY_PRIMARY_KEY_WITH_LOGICAL_DELETE));
...@@ -361,22 +361,22 @@ public class LogicalDeletePlugin extends BasePlugin { ...@@ -361,22 +361,22 @@ public class LogicalDeletePlugin extends BasePlugin {
context.getCommentGenerator().addComment(selectByPrimaryKey); context.getCommentGenerator().addComment(selectByPrimaryKey);
sb = new StringBuilder(); sb = new StringBuilder();
sb.append("select "); sb.append("select ");
if (stringHasValue(introspectedTable.getSelectByPrimaryKeyQueryId())) { if (stringHasValue(introspectedTable.getSelectByPrimaryKeyQueryId())) {
sb.append('\''); sb.append('\'');
sb.append(introspectedTable.getSelectByPrimaryKeyQueryId()); sb.append(introspectedTable.getSelectByPrimaryKeyQueryId());
sb.append("' as QUERYID,"); sb.append("' as QUERYID,");
} }
selectByPrimaryKey.addElement(new TextElement(sb.toString())); selectByPrimaryKey.addElement(new TextElement(sb.toString()));
selectByPrimaryKey.addElement(XmlElementGeneratorTools.getBaseColumnListElement(introspectedTable)); selectByPrimaryKey.addElement(XmlElementGeneratorTools.getBaseColumnListElement(introspectedTable));
if (introspectedTable.hasBLOBColumns()) { if (introspectedTable.hasBLOBColumns()) {
selectByPrimaryKey.addElement(new TextElement(",")); selectByPrimaryKey.addElement(new TextElement(","));
selectByPrimaryKey.addElement(XmlElementGeneratorTools.getBlobColumnListElement(introspectedTable)); selectByPrimaryKey.addElement(XmlElementGeneratorTools.getBlobColumnListElement(introspectedTable));
} }
sb.setLength(0); sb.setLength(0);
sb.append("from "); sb.append("from ");
sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()); sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
selectByPrimaryKey.addElement(new TextElement(sb.toString())); selectByPrimaryKey.addElement(new TextElement(sb.toString()));
...@@ -436,18 +436,18 @@ public class LogicalDeletePlugin extends BasePlugin { ...@@ -436,18 +436,18 @@ public class LogicalDeletePlugin extends BasePlugin {
* @param topLevelClass * @param topLevelClass
* @param introspectedTable * @param introspectedTable
*/ */
private void generateModelMethodsAndFields(TopLevelClass topLevelClass, IntrospectedTable introspectedTable){ private void generateModelMethodsAndFields(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if (this.logicalDeleteColumn != null) { if (this.logicalDeleteColumn != null) {
// 常量和逻辑删除方法跟随 逻辑删除列走 // 常量和逻辑删除方法跟随 逻辑删除列走
boolean flag = false; boolean flag = false;
for (Field field : topLevelClass.getFields()){ for (Field field : topLevelClass.getFields()) {
if (this.logicalDeleteColumn.getJavaProperty().equals(field.getName())){ if (this.logicalDeleteColumn.getJavaProperty().equals(field.getName())) {
flag = true; flag = true;
break; break;
} }
} }
if (flag){ if (flag) {
ArrayList<Field> fields = (ArrayList<Field>) topLevelClass.getFields(); ArrayList<Field> fields = (ArrayList<Field>) topLevelClass.getFields();
// 添加删除标志位常量 // 添加删除标志位常量
...@@ -505,7 +505,7 @@ public class LogicalDeletePlugin extends BasePlugin { ...@@ -505,7 +505,7 @@ public class LogicalDeletePlugin extends BasePlugin {
); );
commentGenerator.addGeneralMethodComment(mAndLogicalDeleted, introspectedTable); commentGenerator.addGeneralMethodComment(mAndLogicalDeleted, introspectedTable);
Method logicalDeleteSetter = JavaBeansUtil.getJavaBeansSetter(this.logicalDeleteColumn, context, introspectedTable); Method logicalDeleteSetter = JavaBeansUtil.getJavaBeansSetter(this.logicalDeleteColumn, context, introspectedTable);
mAndLogicalDeleted.addBodyLine(logicalDeleteSetter.getName() +"(deleted ? " + this.logicalDeleteConstName + " : " + this.logicalUnDeleteConstName + ");"); mAndLogicalDeleted.addBodyLine(logicalDeleteSetter.getName() + "(deleted ? " + this.logicalDeleteConstName + " : " + this.logicalUnDeleteConstName + ");");
FormatTools.addMethodWithBestPosition(topLevelClass, mAndLogicalDeleted); FormatTools.addMethodWithBestPosition(topLevelClass, mAndLogicalDeleted);
} }
} }
...@@ -544,7 +544,7 @@ public class LogicalDeletePlugin extends BasePlugin { ...@@ -544,7 +544,7 @@ public class LogicalDeletePlugin extends BasePlugin {
StringBuilder equalToMethodName = new StringBuilder(); StringBuilder equalToMethodName = new StringBuilder();
equalToMethodName.append(this.logicalDeleteColumn.getJavaProperty()); equalToMethodName.append(this.logicalDeleteColumn.getJavaProperty());
equalToMethodName.setCharAt(0, Character.toUpperCase(equalToMethodName.charAt(0))); equalToMethodName.setCharAt(0, Character.toUpperCase(equalToMethodName.charAt(0)));
equalToMethodName.insert(0, "and"); equalToMethodName.insert(0, "and");
equalToMethodName.append("EqualTo"); equalToMethodName.append("EqualTo");
sb.append(equalToMethodName); sb.append(equalToMethodName);
sb.append("(" + modelName + "." + this.logicalDeleteConstName + ")"); sb.append("(" + modelName + "." + this.logicalDeleteConstName + ")");
...@@ -555,7 +555,7 @@ public class LogicalDeletePlugin extends BasePlugin { ...@@ -555,7 +555,7 @@ public class LogicalDeletePlugin extends BasePlugin {
StringBuilder notEqualToMethodName = new StringBuilder(); StringBuilder notEqualToMethodName = new StringBuilder();
notEqualToMethodName.append(this.logicalDeleteColumn.getJavaProperty()); notEqualToMethodName.append(this.logicalDeleteColumn.getJavaProperty());
notEqualToMethodName.setCharAt(0, Character.toUpperCase(notEqualToMethodName.charAt(0))); notEqualToMethodName.setCharAt(0, Character.toUpperCase(notEqualToMethodName.charAt(0)));
notEqualToMethodName.insert(0, "and"); notEqualToMethodName.insert(0, "and");
notEqualToMethodName.append("NotEqualTo"); notEqualToMethodName.append("NotEqualTo");
sb.append(notEqualToMethodName); sb.append(notEqualToMethodName);
sb.append("(" + modelName + "." + this.logicalDeleteConstName + ")"); sb.append("(" + modelName + "." + this.logicalDeleteConstName + ")");
...@@ -564,13 +564,13 @@ public class LogicalDeletePlugin extends BasePlugin { ...@@ -564,13 +564,13 @@ public class LogicalDeletePlugin extends BasePlugin {
method.addBodyLine(sb.toString()); method.addBodyLine(sb.toString());
innerClass.addMethod(method); FormatTools.addMethodWithBestPosition(innerClass, method);
// TODO 过期方法 // TODO 过期方法
Method mAndDeleted = new Method(method); Method mAndDeleted = new Method(method);
mAndDeleted.setName("andDeleted"); mAndDeleted.setName("andDeleted");
mAndDeleted.addAnnotation("@Deprecated"); mAndDeleted.addAnnotation("@Deprecated");
innerClass.addMethod(mAndDeleted); FormatTools.addMethodWithBestPosition(innerClass, mAndDeleted);
logger.debug("itfsw(逻辑删除插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "增加andDeleted方法。"); logger.debug("itfsw(逻辑删除插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "增加andDeleted方法。");
} }
} }
......
...@@ -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;
......
...@@ -38,64 +38,73 @@ import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; ...@@ -38,64 +38,73 @@ import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
public class SelectOneByExamplePlugin extends BasePlugin { public class SelectOneByExamplePlugin extends BasePlugin {
public static final String METHOD_SELECT_ONE_BY_EXAMPLE = "selectOneByExample"; // 方法名 public static final String METHOD_SELECT_ONE_BY_EXAMPLE = "selectOneByExample"; // 方法名
public static final String METHOD_SELECT_ONE_BY_EXAMPLE_WITH_BLOBS = "selectOneByExampleWithBLOBs"; // 方法名 public static final String METHOD_SELECT_ONE_BY_EXAMPLE_WITH_BLOBS = "selectOneByExampleWithBLOBs"; // 方法名
private XmlElement selectOneByExampleEle;
private XmlElement selectOneByExampleWithBLOBsEle;
/** /**
* Java Client Methods 生成 * Java Client Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param method
* @param interfaze * @param interfaze
* @param topLevelClass
* @param introspectedTable * @param introspectedTable
* @return * @return
*/ */
@Override @Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
// 方法生成 selectOneByExample // 方法生成 selectOneByExample
Method method = JavaElementGeneratorTools.generateMethod( Method selectOneMethod = JavaElementGeneratorTools.generateMethod(
METHOD_SELECT_ONE_BY_EXAMPLE, METHOD_SELECT_ONE_BY_EXAMPLE_WITH_BLOBS,
JavaVisibility.DEFAULT, JavaVisibility.DEFAULT,
JavaElementGeneratorTools.getModelTypeWithoutBLOBs(introspectedTable), JavaElementGeneratorTools.getModelTypeWithBLOBs(introspectedTable),
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example") new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example")
); );
commentGenerator.addGeneralMethodComment(method, introspectedTable); commentGenerator.addGeneralMethodComment(selectOneMethod, introspectedTable);
// hook // hook
if (PluginTools.getHook(ISelectOneByExamplePluginHook.class).clientSelectOneByExampleWithoutBLOBsMethodGenerated(method, interfaze, introspectedTable)) { if (PluginTools.getHook(ISelectOneByExamplePluginHook.class).clientSelectOneByExampleWithBLOBsMethodGenerated(selectOneMethod, interfaze, introspectedTable)) {
// interface 增加方法 // interface 增加方法
FormatTools.addMethodWithBestPosition(interfaze, method); FormatTools.addMethodWithBestPosition(interfaze, selectOneMethod);
logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExample方法。"); logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExampleWithBLOBs方法。");
} }
return super.clientSelectByExampleWithBLOBsMethodGenerated(method, interfaze, introspectedTable);
}
// 方法生成 selectOneByExampleWithBLOBs !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准 /**
if (introspectedTable.hasBLOBColumns()) { * Java Client Methods 生成
// 方法生成 selectOneByExample * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
Method method1 = JavaElementGeneratorTools.generateMethod( * @param method
METHOD_SELECT_ONE_BY_EXAMPLE_WITH_BLOBS, * @param interfaze
JavaVisibility.DEFAULT, * @param introspectedTable
JavaElementGeneratorTools.getModelTypeWithBLOBs(introspectedTable), * @return
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example") */
); @Override
commentGenerator.addGeneralMethodComment(method1, introspectedTable); public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
// 方法生成 selectOneByExample
Method selectOneMethod = JavaElementGeneratorTools.generateMethod(
METHOD_SELECT_ONE_BY_EXAMPLE,
JavaVisibility.DEFAULT,
JavaElementGeneratorTools.getModelTypeWithoutBLOBs(introspectedTable),
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example")
);
commentGenerator.addGeneralMethodComment(selectOneMethod, introspectedTable);
// hook // hook
if (PluginTools.getHook(ISelectOneByExamplePluginHook.class).clientSelectOneByExampleWithBLOBsMethodGenerated(method1, interfaze, introspectedTable)) { if (PluginTools.getHook(ISelectOneByExamplePluginHook.class).clientSelectOneByExampleWithoutBLOBsMethodGenerated(selectOneMethod, interfaze, introspectedTable)) {
// interface 增加方法 // interface 增加方法
FormatTools.addMethodWithBestPosition(interfaze, method1); FormatTools.addMethodWithBestPosition(interfaze, selectOneMethod);
logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExampleWithBLOBs方法。"); logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExample方法。");
}
} }
return super.clientSelectByExampleWithoutBLOBsMethodGenerated(selectOneMethod, interfaze, introspectedTable);
return true;
} }
/** /**
* SQL Map Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param document * @param element
* @param introspectedTable * @param introspectedTable
* @return * @return
*/ */
@Override @Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) { public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
// ------------------------------------ selectOneByExample ---------------------------------- // ------------------------------------ selectOneByExample ----------------------------------
// 生成查询语句 // 生成查询语句
XmlElement selectOneElement = new XmlElement("select"); XmlElement selectOneElement = new XmlElement("select");
...@@ -132,65 +141,88 @@ public class SelectOneByExamplePlugin extends BasePlugin { ...@@ -132,65 +141,88 @@ public class SelectOneByExamplePlugin extends BasePlugin {
// 只查询一条 // 只查询一条
selectOneElement.addElement(new TextElement("limit 1")); selectOneElement.addElement(new TextElement("limit 1"));
this.selectOneByExampleEle = selectOneElement;
return super.sqlMapSelectByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
}
// hook /**
if (PluginTools.getHook(ISelectOneByExamplePluginHook.class).sqlMapSelectOneByExampleWithoutBLOBsElementGenerated(document, selectOneElement, introspectedTable)) { * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
// 添加到根节点 * @param element
FormatTools.addElementWithBestPosition(document.getRootElement(), selectOneElement); * @param introspectedTable
logger.debug("itfsw(查询单条数据插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加selectOneByExample方法。"); * @return
*/
@Override
public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
// 生成查询语句
XmlElement selectOneWithBLOBsElement = new XmlElement("select");
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
commentGenerator.addComment(selectOneWithBLOBsElement);
// 添加ID
selectOneWithBLOBsElement.addAttribute(new Attribute("id", METHOD_SELECT_ONE_BY_EXAMPLE_WITH_BLOBS));
// 添加返回类型
selectOneWithBLOBsElement.addAttribute(new Attribute("resultMap", introspectedTable.getResultMapWithBLOBsId()));
// 添加参数类型
selectOneWithBLOBsElement.addAttribute(new Attribute("parameterType", introspectedTable.getExampleType()));
// 添加查询SQL
selectOneWithBLOBsElement.addElement(new TextElement("select"));
StringBuilder sb = new StringBuilder();
if (stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
sb.append('\'');
sb.append(introspectedTable.getSelectByExampleQueryId());
sb.append("' as QUERYID,");
selectOneWithBLOBsElement.addElement(new TextElement(sb.toString()));
} }
// ------------------------------------ selectOneByExampleWithBLOBs ---------------------------------- selectOneWithBLOBsElement.addElement(XmlElementGeneratorTools.getBaseColumnListElement(introspectedTable));
// !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准 selectOneWithBLOBsElement.addElement(new TextElement(","));
if (introspectedTable.hasBLOBColumns()) { selectOneWithBLOBsElement.addElement(XmlElementGeneratorTools.getBlobColumnListElement(introspectedTable));
// 生成查询语句
XmlElement selectOneWithBLOBsElement = new XmlElement("select");
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
commentGenerator.addComment(selectOneWithBLOBsElement);
// 添加ID
selectOneWithBLOBsElement.addAttribute(new Attribute("id", METHOD_SELECT_ONE_BY_EXAMPLE_WITH_BLOBS));
// 添加返回类型
selectOneWithBLOBsElement.addAttribute(new Attribute("resultMap", introspectedTable.getResultMapWithBLOBsId()));
// 添加参数类型
selectOneWithBLOBsElement.addAttribute(new Attribute("parameterType", introspectedTable.getExampleType()));
// 添加查询SQL
selectOneWithBLOBsElement.addElement(new TextElement("select"));
sb.setLength(0);
if (stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
sb.append('\'');
sb.append(introspectedTable.getSelectByExampleQueryId());
sb.append("' as QUERYID,");
selectOneWithBLOBsElement.addElement(new TextElement(sb.toString()));
}
selectOneWithBLOBsElement.addElement(XmlElementGeneratorTools.getBaseColumnListElement(introspectedTable)); sb.setLength(0);
selectOneWithBLOBsElement.addElement(new TextElement(",")); sb.append("from ");
selectOneWithBLOBsElement.addElement(XmlElementGeneratorTools.getBlobColumnListElement(introspectedTable)); sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
selectOneWithBLOBsElement.addElement(new TextElement(sb.toString()));
selectOneWithBLOBsElement.addElement(XmlElementGeneratorTools.getExampleIncludeElement(introspectedTable));
sb.setLength(0); XmlElement ifElement1 = new XmlElement("if");
sb.append("from "); ifElement1.addAttribute(new Attribute("test", "orderByClause != null")); //$NON-NLS-2$
sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()); ifElement1.addElement(new TextElement("order by ${orderByClause}"));
selectOneWithBLOBsElement.addElement(new TextElement(sb.toString())); selectOneWithBLOBsElement.addElement(ifElement1);
selectOneWithBLOBsElement.addElement(XmlElementGeneratorTools.getExampleIncludeElement(introspectedTable));
XmlElement ifElement1 = new XmlElement("if"); // 只查询一条
ifElement1.addAttribute(new Attribute("test", "orderByClause != null")); //$NON-NLS-2$ selectOneWithBLOBsElement.addElement(new TextElement("limit 1"));
ifElement1.addElement(new TextElement("order by ${orderByClause}"));
selectOneWithBLOBsElement.addElement(ifElement1);
// 只查询一条 this.selectOneByExampleWithBLOBsEle = selectOneWithBLOBsElement;
selectOneWithBLOBsElement.addElement(new TextElement("limit 1")); return super.sqlMapSelectByExampleWithBLOBsElementGenerated(element, introspectedTable);
}
/**
* SQL Map Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param document
* @param introspectedTable
* @return
*/
@Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
if (selectOneByExampleEle != null) {
// hook // hook
if (PluginTools.getHook(ISelectOneByExamplePluginHook.class).sqlMapSelectOneByExampleWithBLOBsElementGenerated(document, selectOneWithBLOBsElement, introspectedTable)) { if (PluginTools.getHook(ISelectOneByExamplePluginHook.class).sqlMapSelectOneByExampleWithoutBLOBsElementGenerated(document, selectOneByExampleEle, introspectedTable)) {
// 添加到根节点 // 添加到根节点
FormatTools.addElementWithBestPosition(document.getRootElement(), selectOneWithBLOBsElement); FormatTools.addElementWithBestPosition(document.getRootElement(), selectOneByExampleEle);
logger.debug("itfsw(查询单条数据插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加selectOneByExampleWithBLOBs方法。"); logger.debug("itfsw(查询单条数据插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加selectOneByExample方法。");
} }
} }
if (selectOneByExampleWithBLOBsEle != null) {
// hook
if (PluginTools.getHook(ISelectOneByExamplePluginHook.class).sqlMapSelectOneByExampleWithBLOBsElementGenerated(document, selectOneByExampleWithBLOBsEle, introspectedTable)) {
// 添加到根节点
FormatTools.addElementWithBestPosition(document.getRootElement(), selectOneByExampleWithBLOBsEle);
logger.debug("itfsw(查询单条数据插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加selectOneByExampleWithBLOBs方法。");
}
}
return true; return true;
} }
......
...@@ -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