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$ + } + } + } +}