Skip to content

Commit 5126a75

Browse files
author
hewei
committed
bug:SelectiveEnhancedPlugin对于自增主键返回存在异常
1 parent f74bc8a commit 5126a75

File tree

5 files changed

+121
-5
lines changed

5 files changed

+121
-5
lines changed

src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,9 @@ public boolean sqlMapInsertSelectiveElementGenerated(XmlElement element, Introsp
169169
// warning has already been reported
170170
if (introspectedColumn != null) {
171171
if (gk.isJdbcStandard()) {
172-
answer.addAttribute(new Attribute("useGeneratedKeys", "true"));
173-
answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty()));
174-
answer.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName()));
172+
XmlElementGeneratorTools.useGeneratedKeys(answer, introspectedTable, "record.");
175173
} else {
176-
answer.addElement(XmlElementGeneratorTools.getSelectKey(introspectedColumn, gk));
174+
answer.addElement(XmlElementGeneratorTools.getSelectKey(introspectedColumn, gk, "record."));
177175
}
178176
}
179177
}

src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ public class XmlElementGeneratorTools {
5252
* @return the selectKey element
5353
*/
5454
public static Element getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey) {
55+
return getSelectKey(introspectedColumn, generatedKey, null);
56+
}
57+
58+
public static Element getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey, String prefix) {
5559
String identityColumnType = introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName();
5660

5761
XmlElement answer = new XmlElement("selectKey");
5862
answer.addAttribute(new Attribute("resultType", identityColumnType));
59-
answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty()));
63+
answer.addAttribute(new Attribute("keyProperty", (prefix == null ? "" : prefix) + introspectedColumn.getJavaProperty()));
6064
answer.addAttribute(new Attribute("order", generatedKey.getMyBatis3Order()));
6165

6266
answer.addElement(new TextElement(generatedKey.getRuntimeSqlStatement()));

src/test/java/com/itfsw/mybatis/generator/plugins/BugFixedTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,35 @@ public void reloadProject(SqlSession sqlSession, ClassLoader loader, String pack
7676
}
7777
});
7878
}
79+
80+
/**
81+
* insertSelective 因为集成SelectiveEnhancedPlugin,传入参数变成map,自增ID返回要修正
82+
* @throws Exception
83+
*/
84+
@Test
85+
public void bug0002() throws Exception {
86+
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BugFixedTest/bug-0002.xml");
87+
tool.generate(() -> DBHelper.createDB("scripts/BugFixedTest/bug-0002.sql"), new AbstractShellCallback() {
88+
@Override
89+
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
90+
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
91+
92+
ObjectUtil tb = new ObjectUtil(loader, packagz + ".Tb");
93+
tb.set("field1", "ts1");
94+
95+
// selective
96+
ObjectUtil TbColumnField1 = new ObjectUtil(loader, packagz + ".Tb$Column#field1");
97+
Object columns = Array.newInstance(TbColumnField1.getCls(), 1);
98+
Array.set(columns, 0, TbColumnField1.getObject());
99+
100+
// sql
101+
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "insertSelective", tb.getObject(), columns);
102+
Assert.assertEquals(sql, "insert into tb ( field1 ) values ( 'ts1' )");
103+
Object result = tbMapper.invoke("insertSelective", tb.getObject(), columns);
104+
Assert.assertEquals(result, 1);
105+
// 自增ID
106+
Assert.assertEquals(tb.get("id"), 1L);
107+
}
108+
});
109+
}
79110
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Navicat MySQL Data Transfer
3+
4+
Source Server : localhost
5+
Source Server Version : 50617
6+
Source Host : localhost:3306
7+
Source Database : mybatis-generator-plugin
8+
9+
Target Server Type : MYSQL
10+
Target Server Version : 50617
11+
File Encoding : 65001
12+
13+
Date: 2017-06-27 11:17:08
14+
*/
15+
16+
SET FOREIGN_KEY_CHECKS=0;
17+
18+
-- ----------------------------
19+
-- Table structure for tb
20+
-- ----------------------------
21+
DROP TABLE IF EXISTS `tb`;
22+
CREATE TABLE `tb` (
23+
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
24+
`field1` varchar(255) DEFAULT NULL,
25+
PRIMARY KEY (`id`)
26+
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2018.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<!DOCTYPE generatorConfiguration
19+
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
20+
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
21+
<generatorConfiguration>
22+
<properties resource="db.properties"/>
23+
<!--导入属性配置 -->
24+
<context id="default" targetRuntime="MyBatis3">
25+
<property name="autoDelimitKeywords" value="true"/>
26+
<property name="beginningDelimiter" value="`"/>
27+
<property name="endingDelimiter" value="`"/>
28+
29+
<!-- 插件 -->
30+
<plugin type="com.itfsw.mybatis.generator.plugins.SelectiveEnhancedPlugin"/>
31+
<plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>
32+
33+
<!--jdbc的数据库连接 -->
34+
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
35+
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
36+
targetPackage 指定生成的model生成所在的包名
37+
targetProject 指定在该项目下所在的路径 -->
38+
<javaModelGenerator targetPackage="" targetProject="">
39+
<!-- 是否对model添加 构造函数 -->
40+
<property name="constructorBased" value="true"/>
41+
<!-- 给Model添加一个父类 -->
42+
<!--<property name="rootClass" value="com.itfsw.base"/>-->
43+
</javaModelGenerator>
44+
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
45+
<sqlMapGenerator targetPackage="" targetProject="" />
46+
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
47+
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
48+
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
49+
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
50+
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
51+
52+
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
53+
<table tableName="tb">
54+
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
55+
</table>
56+
</context>
57+
</generatorConfiguration>

0 commit comments

Comments
 (0)