From 9d0068df0f97c8a1ceddc9b6a3e37d62d7be80e0 Mon Sep 17 00:00:00 2001 From: hewei Date: Wed, 22 Mar 2017 14:38:56 +0800 Subject: [PATCH] =?UTF-8?q?useGeneratedKeys=EF=BC=9A=E4=BD=BF=E7=94=A8JDBC?= =?UTF-8?q?=E7=9A=84getGenereatedKeys=E6=96=B9=E6=B3=95=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E4=B8=BB=E9=94=AE=E5=B9=B6=E8=B5=8B=E5=80=BC=E5=88=B0keyProper?= =?UTF-8?q?ty=E8=AE=BE=E7=BD=AE=E7=9A=84=E9=A2=86=E5=9F=9F=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E5=B1=9E=E6=80=A7=E4=B8=AD=E3=80=82=E6=8A=BD=E5=8F=96?= =?UTF-8?q?=E6=88=90=E4=B8=80=E4=B8=AA=E5=B7=A5=E5=85=B7=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/BatchInsertOldPlugin.java | 30 ++-------- .../generator/plugins/BatchInsertPlugin.java | 19 ++----- .../generator/plugins/utils/CommTools.java | 55 +++++++++++++++++++ 3 files changed, 66 insertions(+), 38 deletions(-) create mode 100644 src/main/java/com/itfsw/mybatis/generator/plugins/utils/CommTools.java diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/BatchInsertOldPlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/BatchInsertOldPlugin.java index 76a0233..1bc541f 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/BatchInsertOldPlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/BatchInsertOldPlugin.java @@ -16,6 +16,7 @@ package com.itfsw.mybatis.generator.plugins; +import com.itfsw.mybatis.generator.plugins.utils.CommTools; import com.itfsw.mybatis.generator.plugins.utils.CommentTools; import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedTable; @@ -28,7 +29,6 @@ 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.GeneratedKey; import org.mybatis.generator.config.PluginConfiguration; import org.mybatis.generator.internal.util.StringUtility; import org.slf4j.Logger; @@ -170,18 +170,8 @@ public class BatchInsertOldPlugin extends PluginAdapter { // 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated) CommentTools.addComment(element); - 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", introspectedColumn.getJavaProperty())); //$NON-NLS-1$ - element.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName())); //$NON-NLS-1$ - } - } + // 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer + CommTools.useGeneratedKeys(element, introspectedTable); // choose 节点 XmlElement choose = new XmlElement("choose"); @@ -255,18 +245,8 @@ public class BatchInsertOldPlugin extends PluginAdapter { // 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated) CommentTools.addComment(element); - 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", introspectedColumn.getJavaProperty())); //$NON-NLS-1$ - element.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName())); //$NON-NLS-1$ - } - } + // 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer + CommTools.useGeneratedKeys(element, introspectedTable); // 普通插入语句 this.addNormalBatchInsertXml(element, introspectedTable); diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/BatchInsertPlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/BatchInsertPlugin.java index 62c07f1..a6715b5 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/BatchInsertPlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/BatchInsertPlugin.java @@ -16,6 +16,7 @@ package com.itfsw.mybatis.generator.plugins; +import com.itfsw.mybatis.generator.plugins.utils.CommTools; import com.itfsw.mybatis.generator.plugins.utils.CommentTools; import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedTable; @@ -28,7 +29,6 @@ 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.GeneratedKey; import org.mybatis.generator.config.PluginConfiguration; import org.mybatis.generator.internal.util.StringUtility; import org.slf4j.Logger; @@ -160,6 +160,9 @@ public class BatchInsertPlugin extends PluginAdapter { // 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated) CommentTools.addComment(batchInsertEle); + // 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer + CommTools.useGeneratedKeys(batchInsertEle, introspectedTable); + StringBuilder insertClause = new StringBuilder(); StringBuilder valuesClause = new StringBuilder(); @@ -217,18 +220,8 @@ public class BatchInsertPlugin extends PluginAdapter { // 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated) CommentTools.addComment(element); - 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", introspectedColumn.getJavaProperty())); //$NON-NLS-1$ - element.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName())); //$NON-NLS-1$ - } - } + // 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer + CommTools.useGeneratedKeys(element, introspectedTable); element.addElement(new TextElement("insert into "+introspectedTable.getFullyQualifiedTableNameAtRuntime()+" (")); diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/CommTools.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/CommTools.java new file mode 100644 index 0000000..6b728a8 --- /dev/null +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/CommTools.java @@ -0,0 +1,55 @@ +/* + * 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.api.IntrospectedColumn; +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; + +/** + * --------------------------------------------------------------------------- + * 一些通用工具集 + * --------------------------------------------------------------------------- + * @author: hewei + * @time:2017/3/22 14:03 + * --------------------------------------------------------------------------- + */ +public class CommTools { + + /** + * 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer + * + * @param element + * @param introspectedTable + */ + public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable){ + 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", introspectedColumn.getJavaProperty())); //$NON-NLS-1$ + element.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName())); //$NON-NLS-1$ + } + } + } +}