bugfix[issues#10]:修正在配置autoDelimitKeywords时,由于正则获取columnName异常问题
This commit is contained in:
@@ -16,10 +16,7 @@
|
||||
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.IncrementsPluginTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.*;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.TopLevelClass;
|
||||
@@ -184,7 +181,7 @@ public class IncrementsPlugin extends BasePlugin {
|
||||
TextElement textEle = (TextElement) textEles.get(0);
|
||||
String[] strs = textEle.getContent().split("=");
|
||||
String columnName = strs[0].trim();
|
||||
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(columnName);
|
||||
IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
|
||||
// 查找是否需要进行增量操作
|
||||
if (incTools.supportColumn(introspectedColumn)) {
|
||||
xmlElement.getElements().clear();
|
||||
@@ -213,7 +210,7 @@ public class IncrementsPlugin extends BasePlugin {
|
||||
// 清理 set 操作
|
||||
text = text.replaceFirst("set\\s", "").trim();
|
||||
String columnName = text.split("=")[0].trim();
|
||||
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(columnName);
|
||||
IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
|
||||
// 查找判断是否需要进行节点替换
|
||||
if (incTools.supportColumn(introspectedColumn)) {
|
||||
newEles.addAll(incTools.generatedIncrementsElement(introspectedColumn, hasPrefix, text.endsWith(",")));
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.IntrospectedTableTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
@@ -220,12 +221,14 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
|
||||
columnName = text.replaceAll(",", "");
|
||||
}
|
||||
// bug fixed: 修正使用autoDelimitKeywords过滤关键词造成的field前后加了特殊字符的问题
|
||||
columnName = columnName.trim().replaceAll("`", "").replaceAll("\"", "").replaceAll("'", "");
|
||||
// columnName = columnName.trim().replaceAll("`", "").replaceAll("\"", "").replaceAll("'", "");
|
||||
}
|
||||
|
||||
IntrospectedColumn column = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
|
||||
|
||||
XmlElement ifEle = new XmlElement("if");
|
||||
|
||||
ifEle.addAttribute(new Attribute("test", prefix + "isSelective(\'" + MyBatis3FormattingUtilities.getEscapedColumnName(introspectedTable.getColumn(columnName)) + "\')"));
|
||||
ifEle.addAttribute(new Attribute("test", prefix + "isSelective(\'" + MyBatis3FormattingUtilities.getEscapedColumnName(column) + "\')"));
|
||||
for (Element ifChild : xmlElement.getElements()){
|
||||
ifEle.addElement(ifChild);
|
||||
}
|
||||
|
||||
@@ -16,10 +16,7 @@
|
||||
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.IncrementsPluginTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.*;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
@@ -438,8 +435,8 @@ public class UpsertPlugin extends BasePlugin {
|
||||
// 获取if节点
|
||||
for (Element element : trimXmlElement.getElements()) {
|
||||
String text = ((TextElement) (((XmlElement) element).getElements().get(0))).getContent();
|
||||
String columnName = text.split("=")[0].trim().replaceAll("`", "").replaceAll("\"", "").replaceAll("'", "");
|
||||
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(columnName);
|
||||
String columnName = text.split("=")[0];
|
||||
IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
|
||||
if (incTools.supportColumn(introspectedColumn)) {
|
||||
// if 节点数据替换
|
||||
((XmlElement) element).getElements().clear();
|
||||
@@ -465,8 +462,8 @@ public class UpsertPlugin extends BasePlugin {
|
||||
if (incTools.support()) {
|
||||
// 获取column
|
||||
String text = element.getContent().trim();
|
||||
String columnName = text.split("=")[0].trim().replaceAll("`", "").replaceAll("\"", "").replaceAll("'", "");
|
||||
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(columnName);
|
||||
String columnName = text.split("=")[0];
|
||||
IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
|
||||
if (incTools.supportColumn(introspectedColumn)) {
|
||||
xmlElement.getElements().addAll(incTools.generatedIncrementsElement(introspectedColumn, hasPrefix, text.endsWith(",")));
|
||||
continue;
|
||||
|
||||
@@ -74,7 +74,7 @@ public class IncrementsPluginTools {
|
||||
// 切分
|
||||
String[] incrementsColumnsStrs = incrementsColumns.split(",");
|
||||
for (String incrementsColumnsStr : incrementsColumnsStrs) {
|
||||
IntrospectedColumn column = introspectedTable.getColumn(incrementsColumnsStr.trim());
|
||||
IntrospectedColumn column = IntrospectedTableTools.safeGetColumn(introspectedTable, incrementsColumnsStr);
|
||||
if (column == null) {
|
||||
warnings.add("itfsw:插件" + IncrementsPlugin.class.getTypeName() + "插件没有找到column为" + incrementsColumnsStr.trim() + "的字段!");
|
||||
} else {
|
||||
@@ -146,12 +146,12 @@ public class IncrementsPluginTools {
|
||||
when.addAttribute(new Attribute(
|
||||
"test",
|
||||
(hasPrefix ? "record." : "_parameter.") + IncrementsPlugin.METHOD_INC_CHECK
|
||||
+ "('" + MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + "')"
|
||||
+ "('" + MyBatis3FormattingUtilities.escapeStringForMyBatis3(introspectedColumn.getActualColumnName()) + "')"
|
||||
));
|
||||
TextElement spec = new TextElement(
|
||||
MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn)
|
||||
+ " ${" + (hasPrefix ? "record." : "")
|
||||
+ IncrementsPlugin.FIELD_INC_MAP + "." + MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + ".value} "
|
||||
+ IncrementsPlugin.FIELD_INC_MAP + "." + MyBatis3FormattingUtilities.escapeStringForMyBatis3(introspectedColumn.getActualColumnName()) + ".value} "
|
||||
+ MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, hasPrefix ? "record." : null));
|
||||
when.addElement(spec);
|
||||
choose.addElement(when);
|
||||
|
||||
+27
-1
@@ -18,10 +18,12 @@ package com.itfsw.mybatis.generator.plugins.utils;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin;
|
||||
import org.mybatis.generator.api.FullyQualifiedTable;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.config.Context;
|
||||
import org.mybatis.generator.config.JavaModelGeneratorConfiguration;
|
||||
import org.mybatis.generator.config.PluginConfiguration;
|
||||
import org.mybatis.generator.internal.util.StringUtility;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
@@ -31,6 +33,7 @@ import java.lang.reflect.Method;
|
||||
* ---------------------------------------------------------------------------
|
||||
* IntrospectedTable 的一些拓展增强
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* @author: hewei
|
||||
* @time:2017/6/13 13:48
|
||||
* ---------------------------------------------------------------------------
|
||||
@@ -68,7 +71,7 @@ public class IntrospectedTableTools {
|
||||
|
||||
// 注意!! 如果配置了ExampleTargetPlugin插件,要修正Example 位置
|
||||
PluginConfiguration configuration = PluginTools.getPluginConfiguration(context, ExampleTargetPlugin.class);
|
||||
if (configuration != null && configuration.getProperty(ExampleTargetPlugin.TARGET_PACKAGE_KEY) != null){
|
||||
if (configuration != null && configuration.getProperty(ExampleTargetPlugin.TARGET_PACKAGE_KEY) != null) {
|
||||
String exampleType = introspectedTable.getExampleType();
|
||||
// 修改包名
|
||||
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = context.getJavaModelGeneratorConfiguration();
|
||||
@@ -78,4 +81,27 @@ public class IntrospectedTableTools {
|
||||
introspectedTable.setExampleType(newExampleType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全获取column 通过正则获取的name可能包含beginningDelimiter&&endingDelimiter
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @param columnName
|
||||
* @return
|
||||
*/
|
||||
public static IntrospectedColumn safeGetColumn(IntrospectedTable introspectedTable, String columnName) {
|
||||
// columnName
|
||||
columnName = columnName.trim();
|
||||
// 过滤
|
||||
String beginningDelimiter = introspectedTable.getContext().getBeginningDelimiter();
|
||||
if (StringUtility.stringHasValue(beginningDelimiter)) {
|
||||
columnName = columnName.replaceFirst("^" + beginningDelimiter, "");
|
||||
}
|
||||
String endingDelimiter = introspectedTable.getContext().getEndingDelimiter();
|
||||
if (StringUtility.stringHasValue(endingDelimiter)) {
|
||||
columnName = columnName.replaceFirst(endingDelimiter + "$", "");
|
||||
}
|
||||
|
||||
return introspectedTable.getColumn(columnName);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -120,7 +120,7 @@ public class XmlElementGeneratorTools {
|
||||
public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable, String prefix) {
|
||||
GeneratedKey gk = introspectedTable.getGeneratedKey();
|
||||
if (gk != null) {
|
||||
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
|
||||
IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, gk.getColumn());
|
||||
// if the column is null, then it's a configuration error. The
|
||||
// warning has already been reported
|
||||
if (introspectedColumn != null) {
|
||||
|
||||
@@ -21,9 +21,13 @@ import org.apache.ibatis.session.SqlSession;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.mybatis.generator.exception.InvalidConfigurationException;
|
||||
import org.mybatis.generator.exception.XMLParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -188,4 +192,36 @@ public class IncrementsPluginTest {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试 autoDelimitKeywords
|
||||
*/
|
||||
@Test
|
||||
public void testWithAutoDelimitKeywords() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-autoDelimitKeywords.xml");
|
||||
tool.generate(new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
// 1. 测试updateByExample、updateByExampleSelective
|
||||
ObjectUtil TbKeyWord = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbKeyWordMapper")));
|
||||
|
||||
ObjectUtil TbKeyWordExample = new ObjectUtil(loader, packagz + ".TbKeyWordExample");
|
||||
ObjectUtil criteria = new ObjectUtil(TbKeyWordExample.invoke("createCriteria"));
|
||||
criteria.invoke("andIdEqualTo", 1l);
|
||||
|
||||
ObjectUtil TbKeyWordBuilder = new ObjectUtil(loader, packagz + ".TbKeyWord$Builder");
|
||||
ObjectUtil TbKeyWordBuilderInc = new ObjectUtil(loader, packagz + ".TbKeyWord$Builder$Inc#INC");
|
||||
TbKeyWordBuilder.invoke("update", 100l, TbKeyWordBuilderInc.getObject());
|
||||
|
||||
// 执行
|
||||
// inc_f1 增加100
|
||||
Object result = TbKeyWord.invoke("updateByExampleSelective", TbKeyWordBuilder.invoke("build"), TbKeyWordExample.getObject());
|
||||
Assert.assertEquals(result, 1);
|
||||
// 验证更新
|
||||
ResultSet rs = DBHelper.execute(sqlSession.getConnection(), "select * from tb_key_word where id = 1");
|
||||
rs.first();
|
||||
Assert.assertEquals(rs.getLong("update"), 101);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,3 +101,20 @@ CREATE TABLE `tb_single_blob` (
|
||||
INSERT INTO `tb_single_blob` VALUES ('1', 'fd1', '0', '1', '2', '3');
|
||||
INSERT INTO `tb_single_blob` VALUES ('2', null, null, '3', '2', '1');
|
||||
INSERT INTO `tb_single_blob` VALUES ('3', null, null, '1', '1', '1');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for tb_key_word
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `tb_key_word`;
|
||||
CREATE TABLE `tb_key_word` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
|
||||
`field2` int(11) DEFAULT NULL,
|
||||
`inc_f1` bigint(20) NOT NULL DEFAULT '0',
|
||||
`update` bigint(20) NOT NULL DEFAULT '0',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of tb_key_word
|
||||
-- ----------------------------
|
||||
INSERT INTO `tb_key_word` VALUES ('1', '0', '0', '1');
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ 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.
|
||||
-->
|
||||
|
||||
<!DOCTYPE generatorConfiguration
|
||||
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
|
||||
<generatorConfiguration>
|
||||
<properties resource="db.properties"/>
|
||||
|
||||
<!--导入属性配置 -->
|
||||
<context id="default" targetRuntime="MyBatis3">
|
||||
<property name="autoDelimitKeywords" value="true"/>
|
||||
<property name="beginningDelimiter" value="`"/>
|
||||
<property name="endingDelimiter" value="`"/>
|
||||
|
||||
<!-- 插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.IncrementsPlugin" />
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin" />
|
||||
|
||||
<!--jdbc的数据库连接 -->
|
||||
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
|
||||
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
|
||||
targetPackage 指定生成的model生成所在的包名
|
||||
targetProject 指定在该项目下所在的路径 -->
|
||||
<javaModelGenerator targetPackage="" targetProject="">
|
||||
<!-- 是否对model添加 构造函数 -->
|
||||
<property name="constructorBased" value="true"/>
|
||||
<!-- 给Model添加一个父类 -->
|
||||
<!--<property name="rootClass" value="com.itfsw.base"/>-->
|
||||
</javaModelGenerator>
|
||||
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
|
||||
<sqlMapGenerator targetPackage="" targetProject="" />
|
||||
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
|
||||
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
|
||||
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
|
||||
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
|
||||
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
|
||||
|
||||
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
<table tableName="tb_key_word">
|
||||
<property name="incrementsColumns" value="update"/>
|
||||
|
||||
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
|
||||
</table>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
Reference in New Issue
Block a user