From 07c78ddd2925238f7690a5bd13ebc56d4a60d696 Mon Sep 17 00:00:00 2001 From: hewei Date: Fri, 21 Apr 2017 11:47:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0SelectiveEnhancedPlugin?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/SelectiveEnhancedPlugin.java | 55 ++++++++++++++++--- .../generator/plugins/UpsertPlugin.java | 2 +- 2 files changed, 47 insertions(+), 10 deletions(-) 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 37bc78f..f137881 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java @@ -87,14 +87,18 @@ public class SelectiveEnhancedPlugin extends PluginAdapter { CommentTools.addMethodComment(mIsSelective, introspectedTable); mIsSelective.setVisibility(JavaVisibility.PUBLIC); mIsSelective.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance()); - mIsSelective.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "column")); - mIsSelective.addBodyLine("if (column == null) {"); mIsSelective.addBodyLine("return this.selectiveColumns.size() > 0;"); - mIsSelective.addBodyLine("} else {"); - mIsSelective.addBodyLine("return this.selectiveColumns.get(column) != null;"); - mIsSelective.addBodyLine("}"); topLevelClass.addMethod(mIsSelective); + // Method isSelective + Method mIsSelective1 = new Method("isSelective"); + CommentTools.addMethodComment(mIsSelective1, introspectedTable); + mIsSelective1.setVisibility(JavaVisibility.PUBLIC); + mIsSelective1.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance()); + mIsSelective1.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "column")); + mIsSelective1.addBodyLine("return this.selectiveColumns.get(column) != null;"); + topLevelClass.addMethod(mIsSelective1); + // Method selective Method mSelective = new Method("selective"); CommentTools.addMethodComment(mSelective, introspectedTable); @@ -137,11 +141,44 @@ public class SelectiveEnhancedPlugin extends PluginAdapter { // ====================================== 1. insertSelective ====================================== if ("insertSelective".equals(id)) { - List trimEles = this.findEle(xmlElement, "trim"); - for (XmlElement ele : trimEles) { + List eles = this.findEle(xmlElement, "trim"); + for (XmlElement ele : eles) { this.replaceEle(ele, "_parameter."); } } + // ====================================== 2. updateByExampleSelective ====================================== + if ("updateByExampleSelective".equals(id)) { + List eles = this.findEle(xmlElement, "set"); + for (XmlElement ele : eles) { + this.replaceEle(ele, "_parameter."); + } + } + // ====================================== 3. updateByPrimaryKeySelective ====================================== + if ("updateByPrimaryKeySelective".equals(id)) { + List eles = this.findEle(xmlElement, "set"); + for (XmlElement ele : eles) { + this.replaceEle(ele, "_parameter."); + } + } + // ====================================== 4. upsertSelective ====================================== + if ("upsertSelective".equals(id)) { + List eles = this.findEle(xmlElement, "trim"); + for (XmlElement ele : eles) { + this.replaceEle(ele, "_parameter."); + } + } + // ====================================== 5. upsertByExampleSelective ====================================== + if ("upsertByExampleSelective".equals(id)) { + List eles = this.findEle(xmlElement, "trim"); + for (XmlElement ele : eles) { + this.replaceEle(ele, "_parameter."); + } + List eles1 = this.findEle(xmlElement, "set"); + for (XmlElement ele : eles1) { + this.replaceEle(ele, "_parameter."); + } + } + } } return true; @@ -187,7 +224,7 @@ public class SelectiveEnhancedPlugin extends PluginAdapter { String text = textElement.getContent().trim(); String field = ""; if (text.matches(".*\\s*=\\s*#\\{.*\\},?")) { - Pattern pattern = Pattern.compile("(.*)\\s*=\\s*#\\{.*},?"); + Pattern pattern = Pattern.compile("(.*?)\\s*=\\s*#\\{.*},?"); Matcher matcher = pattern.matcher(text); if (matcher.find()){ field = matcher.group(1); @@ -203,7 +240,7 @@ public class SelectiveEnhancedPlugin extends PluginAdapter { } XmlElement ifEle = new XmlElement("if"); - ifEle.addAttribute(new Attribute("test", prefix + "isSelective(" + field + ")")); + ifEle.addAttribute(new Attribute("test", prefix + "isSelective(\'" + field + "\')")); ifEle.addElement(textElement); whenEle.addElement(ifEle); } 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 460e81b..5c6a7eb 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java @@ -255,7 +255,7 @@ public class UpsertPlugin extends PluginAdapter { // update eleUpsertByExampleSelective.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())); eleUpsertByExampleSelective.addElement(new TextElement("set")); - eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.generateSets(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "record.")); + eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.generateSetsSelective(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "record.")); // update where eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));