diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java index a9d242a..4a0499f 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java @@ -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(","))); diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java index 4b6e87f..3f5a10a 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java @@ -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); } diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java index 2c570c9..88b3e94 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java @@ -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; diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java index 17dbe73..13a07dd 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java @@ -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); diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/IntrospectedTableTools.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/IntrospectedTableTools.java index 7958e2b..585805b 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/IntrospectedTableTools.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/IntrospectedTableTools.java @@ -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); + } } diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java index e16b764..e543e6a 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java @@ -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) { diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java index efe5bd7..29b3238 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java @@ -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); + } + }); + } } diff --git a/src/test/resources/scripts/IncrementsPlugin/init.sql b/src/test/resources/scripts/IncrementsPlugin/init.sql index d9350d4..205499a 100644 --- a/src/test/resources/scripts/IncrementsPlugin/init.sql +++ b/src/test/resources/scripts/IncrementsPlugin/init.sql @@ -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'); \ No newline at end of file diff --git a/src/test/resources/scripts/IncrementsPlugin/mybatis-generator-with-autoDelimitKeywords.xml b/src/test/resources/scripts/IncrementsPlugin/mybatis-generator-with-autoDelimitKeywords.xml new file mode 100644 index 0000000..8759b0a --- /dev/null +++ b/src/test/resources/scripts/IncrementsPlugin/mybatis-generator-with-autoDelimitKeywords.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
\ No newline at end of file