Commit 5126a757 authored by hewei's avatar hewei

bug:SelectiveEnhancedPlugin对于自增主键返回存在异常

parent f74bc8a1
...@@ -169,11 +169,9 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin ...@@ -169,11 +169,9 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
// warning has already been reported // warning has already been reported
if (introspectedColumn != null) { if (introspectedColumn != null) {
if (gk.isJdbcStandard()) { if (gk.isJdbcStandard()) {
answer.addAttribute(new Attribute("useGeneratedKeys", "true")); XmlElementGeneratorTools.useGeneratedKeys(answer, introspectedTable, "record.");
answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty()));
answer.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName()));
} else { } else {
answer.addElement(XmlElementGeneratorTools.getSelectKey(introspectedColumn, gk)); answer.addElement(XmlElementGeneratorTools.getSelectKey(introspectedColumn, gk, "record."));
} }
} }
} }
......
...@@ -52,11 +52,15 @@ public class XmlElementGeneratorTools { ...@@ -52,11 +52,15 @@ public class XmlElementGeneratorTools {
* @return the selectKey element * @return the selectKey element
*/ */
public static Element getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey) { public static Element getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey) {
return getSelectKey(introspectedColumn, generatedKey, null);
}
public static Element getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey, String prefix) {
String identityColumnType = introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName(); String identityColumnType = introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName();
XmlElement answer = new XmlElement("selectKey"); XmlElement answer = new XmlElement("selectKey");
answer.addAttribute(new Attribute("resultType", identityColumnType)); answer.addAttribute(new Attribute("resultType", identityColumnType));
answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty())); answer.addAttribute(new Attribute("keyProperty", (prefix == null ? "" : prefix) + introspectedColumn.getJavaProperty()));
answer.addAttribute(new Attribute("order", generatedKey.getMyBatis3Order())); answer.addAttribute(new Attribute("order", generatedKey.getMyBatis3Order()));
answer.addElement(new TextElement(generatedKey.getRuntimeSqlStatement())); answer.addElement(new TextElement(generatedKey.getRuntimeSqlStatement()));
......
...@@ -76,4 +76,35 @@ public class BugFixedTest { ...@@ -76,4 +76,35 @@ public class BugFixedTest {
} }
}); });
} }
/**
* insertSelective 因为集成SelectiveEnhancedPlugin,传入参数变成map,自增ID返回要修正
* @throws Exception
*/
@Test
public void bug0002() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BugFixedTest/bug-0002.xml");
tool.generate(() -> DBHelper.createDB("scripts/BugFixedTest/bug-0002.sql"), new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
ObjectUtil tb = new ObjectUtil(loader, packagz + ".Tb");
tb.set("field1", "ts1");
// selective
ObjectUtil TbColumnField1 = new ObjectUtil(loader, packagz + ".Tb$Column#field1");
Object columns = Array.newInstance(TbColumnField1.getCls(), 1);
Array.set(columns, 0, TbColumnField1.getObject());
// sql
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "insertSelective", tb.getObject(), columns);
Assert.assertEquals(sql, "insert into tb ( field1 ) values ( 'ts1' )");
Object result = tbMapper.invoke("insertSelective", tb.getObject(), columns);
Assert.assertEquals(result, 1);
// 自增ID
Assert.assertEquals(tb.get("id"), 1L);
}
});
}
} }
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50617
Source Host : localhost:3306
Source Database : mybatis-generator-plugin
Target Server Type : MYSQL
Target Server Version : 50617
File Encoding : 65001
Date: 2017-06-27 11:17:08
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tb
-- ----------------------------
DROP TABLE IF EXISTS `tb`;
CREATE TABLE `tb` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
`field1` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2018.
~
~ 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.SelectiveEnhancedPlugin"/>
<plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>
<!--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">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment