Commit 1b151673 authored by hewei's avatar hewei

1. 代码精简;

2. 增加SelectiveEnhancedPlugin插件;
parent fa2867dd
......@@ -16,7 +16,7 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommTools;
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
......@@ -171,7 +171,7 @@ public class BatchInsertOldPlugin extends PluginAdapter {
CommentTools.addComment(element);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
CommTools.useGeneratedKeys(element, introspectedTable);
XmlElementGeneratorTools.useGeneratedKeys(element, introspectedTable);
// choose 节点
XmlElement choose = new XmlElement("choose");
......@@ -246,7 +246,7 @@ public class BatchInsertOldPlugin extends PluginAdapter {
CommentTools.addComment(element);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
CommTools.useGeneratedKeys(element, introspectedTable);
XmlElementGeneratorTools.useGeneratedKeys(element, introspectedTable);
// 普通插入语句
this.addNormalBatchInsertXml(element, introspectedTable);
......
......@@ -16,8 +16,8 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommTools;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
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.IntrospectedTable;
......@@ -29,8 +29,6 @@ import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.ListUtilities;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.PluginConfiguration;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -71,27 +69,9 @@ public class BatchInsertPlugin extends PluginAdapter {
// 插件使用前提是使用了ModelColumnPlugin插件
try {
Context ctx = getContext();
// 利用反射获取pluginConfigurations属性
java.lang.reflect.Field field = Context.class.getDeclaredField("pluginConfigurations");
field.setAccessible(true);
List<PluginConfiguration> list = (List<PluginConfiguration>) field.get(ctx);
// 检查是否配置了ModelColumnPlugin插件
boolean unFind = true;
for (PluginConfiguration config: list) {
if (ModelColumnPlugin.class.getName().equals(config.getConfigurationType())){
unFind = false;
}
}
// 建议使用ModelColumnPlugin插件
if (unFind){
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!");
return false;
}
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
if (!PluginTools.checkDependencyPlugin(ModelColumnPlugin.class, null)) {
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!");
return false;
}
// TODO
......@@ -161,7 +141,7 @@ public class BatchInsertPlugin extends PluginAdapter {
CommentTools.addComment(batchInsertEle);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
CommTools.useGeneratedKeys(batchInsertEle, introspectedTable);
XmlElementGeneratorTools.useGeneratedKeys(batchInsertEle, introspectedTable);
batchInsertEle.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
batchInsertEle.addElement(XmlElementGeneratorTools.generateKeys(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())));
......@@ -192,7 +172,7 @@ public class BatchInsertPlugin extends PluginAdapter {
CommentTools.addComment(element);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
CommTools.useGeneratedKeys(element, introspectedTable);
XmlElementGeneratorTools.useGeneratedKeys(element, introspectedTable);
element.addElement(new TextElement("insert into "+introspectedTable.getFullyQualifiedTableNameAtRuntime()+" ("));
......
......@@ -36,7 +36,7 @@ import java.util.List;
* @time:2017/1/17 11:20
* ---------------------------------------------------------------------------
*/
public class ModelColumnPlugin extends PluginAdapter {
public class ModelColumnPlugin extends PluginAdapter {
public static final String ENUM_NAME = "Column"; // 内部Enum名
private static final Logger logger = LoggerFactory.getLogger(ModelColumnPlugin.class);
......@@ -46,8 +46,8 @@ public class ModelColumnPlugin extends PluginAdapter {
@Override
public boolean validate(List<String> warnings) {
// 插件使用前提是targetRuntime为MyBatis3
if (StringUtility.stringHasValue(getContext().getTargetRuntime()) && "MyBatis3".equalsIgnoreCase(getContext().getTargetRuntime()) == false ){
logger.warn("itfsw:插件"+this.getClass().getTypeName()+"要求运行targetRuntime必须为MyBatis3!");
if (StringUtility.stringHasValue(getContext().getTargetRuntime()) && "MyBatis3".equalsIgnoreCase(getContext().getTargetRuntime()) == false) {
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "要求运行targetRuntime必须为MyBatis3!");
return false;
}
return true;
......@@ -56,7 +56,6 @@ public class ModelColumnPlugin extends PluginAdapter {
/**
* Model Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param topLevelClass
* @param introspectedTable
* @return
......@@ -70,7 +69,7 @@ public class ModelColumnPlugin extends PluginAdapter {
innerEnum.setVisibility(JavaVisibility.PUBLIC);
innerEnum.setStatic(true);
CommentTools.addInnerEnumComment(innerEnum, introspectedTable);
logger.debug("itfsw(数据Model属性对应Column获取插件):"+topLevelClass.getType().getShortName()+"增加内部Builder类。");
logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + "增加内部Builder类。");
// 生成属性和构造函数
Field columnField = new Field("column", FullyQualifiedJavaType.getStringInstance());
......@@ -99,7 +98,7 @@ public class ModelColumnPlugin extends PluginAdapter {
constructor.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "column"));
CommentTools.addGeneralMethodComment(constructor, introspectedTable);
innerEnum.addMethod(constructor);
logger.debug("itfsw(数据Model属性对应Column获取插件):"+topLevelClass.getType().getShortName()+".Column增加构造方法和column属性。");
logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + ".Column增加构造方法和column属性。");
// Enum枚举
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
......
......@@ -14,53 +14,59 @@
* limitations under the License.
*/
package com.itfsw.mybatis.generator.plugins.utils;
package com.itfsw.mybatis.generator.plugins;
import org.mybatis.generator.api.IntrospectedColumn;
import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.GeneratedKey;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* ---------------------------------------------------------------------------
* 一些通用工具集
* Selective 增强插件
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/3/22 14:03
* @time:2017/4/20 15:39
* ---------------------------------------------------------------------------
*/
public class CommTools {
public class SelectiveEnhancedPlugin extends PluginAdapter {
private static final Logger logger = LoggerFactory.getLogger(SelectiveEnhancedPlugin.class);
/**
* 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
*
* @param element
* @param introspectedTable
* {@inheritDoc}
*/
public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable){
useGeneratedKeys(element, introspectedTable, null);
@Override
public boolean validate(List<String> warnings) {
// 插件使用前提是targetRuntime为MyBatis3
if (StringUtility.stringHasValue(getContext().getTargetRuntime()) && "MyBatis3".equalsIgnoreCase(getContext().getTargetRuntime()) == false) {
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "要求运行targetRuntime必须为MyBatis3!");
return false;
}
// 插件使用前提是使用了ModelColumnPlugin插件
if (!PluginTools.checkDependencyPlugin(ModelColumnPlugin.class, null)) {
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!");
return false;
}
return true;
}
/**
* 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
*
* @param element
* Model Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param topLevelClass
* @param introspectedTable
* @param prefix
* @return
*/
public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable, String prefix){
GeneratedKey gk = introspectedTable.getGeneratedKey();
if (gk != null) {
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
// if the column is null, then it's a configuration error. The
// warning has already been reported
if (introspectedColumn != null) {
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
element.addAttribute(new Attribute("useGeneratedKeys", "true")); //$NON-NLS-1$ //$NON-NLS-2$
element.addAttribute(new Attribute("keyProperty", (prefix == null ? "" : prefix) + introspectedColumn.getJavaProperty())); //$NON-NLS-1$
element.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName())); //$NON-NLS-1$
}
}
@Override
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
return true;
}
}
......@@ -16,7 +16,6 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommTools;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
import org.mybatis.generator.api.IntrospectedTable;
......@@ -168,7 +167,7 @@ public class UpsertPlugin extends PluginAdapter {
eleUpsert.addAttribute(new Attribute("parameterType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
CommTools.useGeneratedKeys(eleUpsert, introspectedTable);
XmlElementGeneratorTools.useGeneratedKeys(eleUpsert, introspectedTable);
// insert
eleUpsert.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
......@@ -191,7 +190,7 @@ public class UpsertPlugin extends PluginAdapter {
eleUpsertSelective.addAttribute(new Attribute("parameterType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
CommTools.useGeneratedKeys(eleUpsertSelective, introspectedTable);
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertSelective, introspectedTable);
// insert
eleUpsertSelective.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
......@@ -214,7 +213,7 @@ public class UpsertPlugin extends PluginAdapter {
CommentTools.addComment(eleUpsertByExample);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
CommTools.useGeneratedKeys(eleUpsertByExample, introspectedTable, "record.");
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertByExample, introspectedTable, "record.");
// insert
eleUpsertByExample.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
......@@ -243,7 +242,7 @@ public class UpsertPlugin extends PluginAdapter {
CommentTools.addComment(eleUpsertByExampleSelective);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
CommTools.useGeneratedKeys(eleUpsertByExampleSelective, introspectedTable, "record.");
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertByExampleSelective, introspectedTable, "record.");
// insert
eleUpsertByExampleSelective.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
......
/*
* Copyright (c) 2017.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.itfsw.mybatis.generator.plugins.utils;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.PluginConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.util.List;
/**
* ---------------------------------------------------------------------------
* 插件工具集
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/4/20 15:43
* ---------------------------------------------------------------------------
*/
public class PluginTools {
private static final Logger logger = LoggerFactory.getLogger(PluginTools.class);
/**
* 检查插件依赖
*
* @param plugin 插件
* @param ctx 上下文
* @return
*/
public static boolean checkDependencyPlugin(Class plugin, Context ctx){
try {
// 利用反射获取pluginConfigurations属性
Field field = Context.class.getDeclaredField("pluginConfigurations");
field.setAccessible(true);
List<PluginConfiguration> list = (List<PluginConfiguration>) field.get(ctx);
// 检查是否配置了ModelColumnPlugin插件
for (PluginConfiguration config: list) {
if (plugin.getName().equals(config.getConfigurationType())){
return true;
}
}
} catch (Exception e) {
logger.error("插件检查反射异常", e);
}
return false;
}
}
......@@ -49,11 +49,11 @@ public class XmlElementGeneratorTools {
String identityColumnType = introspectedColumn
.getFullyQualifiedJavaType().getFullyQualifiedName();
XmlElement answer = new XmlElement("selectKey"); //$NON-NLS-1$
answer.addAttribute(new Attribute("resultType", identityColumnType)); //$NON-NLS-1$
XmlElement answer = new XmlElement("selectKey");
answer.addAttribute(new Attribute("resultType", identityColumnType));
answer.addAttribute(new Attribute(
"keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
answer.addAttribute(new Attribute("order", //$NON-NLS-1$
"keyProperty", introspectedColumn.getJavaProperty()));
answer.addAttribute(new Attribute("order",
generatedKey.getMyBatis3Order()));
answer.addElement(new TextElement(generatedKey
......@@ -63,25 +63,25 @@ public class XmlElementGeneratorTools {
}
public static Element getBaseColumnListElement(IntrospectedTable introspectedTable) {
XmlElement answer = new XmlElement("include"); //$NON-NLS-1$
answer.addAttribute(new Attribute("refid", //$NON-NLS-1$
XmlElement answer = new XmlElement("include");
answer.addAttribute(new Attribute("refid",
introspectedTable.getBaseColumnListId()));
return answer;
}
public static Element getBlobColumnListElement(IntrospectedTable introspectedTable) {
XmlElement answer = new XmlElement("include"); //$NON-NLS-1$
answer.addAttribute(new Attribute("refid", //$NON-NLS-1$
XmlElement answer = new XmlElement("include");
answer.addAttribute(new Attribute("refid",
introspectedTable.getBlobColumnListId()));
return answer;
}
public static Element getExampleIncludeElement(IntrospectedTable introspectedTable) {
XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
ifElement.addAttribute(new Attribute("test", "_parameter != null")); //$NON-NLS-1$ //$NON-NLS-2$
XmlElement ifElement = new XmlElement("if");
ifElement.addAttribute(new Attribute("test", "_parameter != null"));
XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
includeElement.addAttribute(new Attribute("refid", //$NON-NLS-1$
XmlElement includeElement = new XmlElement("include");
includeElement.addAttribute(new Attribute("refid",
introspectedTable.getExampleWhereClauseId()));
ifElement.addElement(includeElement);
......@@ -89,17 +89,49 @@ public class XmlElementGeneratorTools {
}
public static Element getUpdateByExampleIncludeElement(IntrospectedTable introspectedTable) {
XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
ifElement.addAttribute(new Attribute("test", "_parameter != null")); //$NON-NLS-1$ //$NON-NLS-2$
XmlElement ifElement = new XmlElement("if");
ifElement.addAttribute(new Attribute("test", "_parameter != null"));
XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
includeElement.addAttribute(new Attribute("refid", //$NON-NLS-1$
XmlElement includeElement = new XmlElement("include");
includeElement.addAttribute(new Attribute("refid",
introspectedTable.getMyBatis3UpdateByExampleWhereClauseId()));
ifElement.addElement(includeElement);
return ifElement;
}
/**
* 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
*
* @param element
* @param introspectedTable
*/
public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable){
useGeneratedKeys(element, introspectedTable, null);
}
/**
* 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
*
* @param element
* @param introspectedTable
* @param prefix
*/
public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable, String prefix){
GeneratedKey gk = introspectedTable.getGeneratedKey();
if (gk != null) {
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
// if the column is null, then it's a configuration error. The
// warning has already been reported
if (introspectedColumn != null) {
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
element.addAttribute(new Attribute("useGeneratedKeys", "true"));
element.addAttribute(new Attribute("keyProperty", (prefix == null ? "" : prefix) + introspectedColumn.getJavaProperty()));
element.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName()));
}
}
}
/**
* 生成keys Ele
* @param columns
......
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