diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java
new file mode 100644
index 0000000..fb98021
--- /dev/null
+++ b/src/test/java/com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java
@@ -0,0 +1,95 @@
+/*
+ * 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;
+
+import com.itfsw.mybatis.generator.plugins.tools.*;
+import org.apache.ibatis.session.SqlSession;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.mybatis.generator.exception.InvalidConfigurationException;
+import org.mybatis.generator.exception.XMLParserException;
+
+import java.io.IOException;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * ---------------------------------------------------------------------------
+ *
+ * ---------------------------------------------------------------------------
+ * @author: hewei
+ * @time:2017/7/31 14:45
+ * ---------------------------------------------------------------------------
+ */
+public class UpsertPluginTest {
+ /**
+ * 初始化数据库
+ */
+ @BeforeClass
+ public static void init() throws SQLException, IOException, ClassNotFoundException {
+ DBHelper.createDB("scripts/UpsertPlugin/init.sql");
+ }
+
+ /**
+ * 测试配置异常
+ */
+ @Test
+ public void testWarnings() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
+ // 1. 没有使用mysql数据库
+ MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator-with-error-driver.xml");
+ tool.generate();
+ Assert.assertEquals(tool.getWarnings().get(1), "itfsw:插件com.itfsw.mybatis.generator.plugins.UpsertPlugin插件使用前提是数据库为MySQL!");
+
+ // 2. 普通提示
+ tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml");
+ tool.generate();
+ Assert.assertEquals(tool.getWarnings().get(0), "itfsw:插件com.itfsw.mybatis.generator.plugins.UpsertPlugin插件您开启了allowMultiQueries支持,注意在jdbc url 配置中增加“allowMultiQueries=true”支持(不怎么建议使用该功能,开启多sql提交会增加sql注入的风险,请确保你所有sql都使用MyBatis书写,请不要使用statement进行sql提交)!");
+ }
+
+ /**
+ * 测试 upsert
+ */
+ @Test
+ public void testUpsert() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
+ MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml");
+ tool.generate(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("id", 10l);
+ tb.set("field1", "ts1");
+ tb.set("field2", 5);
+
+ // sql
+ String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertSelective", tb.getObject());
+ Assert.assertEquals(sql, "insert into tb ( id, field1, field2 ) values ( 10, 'ts1', 5 ) on duplicate key update id = 10, field1 = 'ts1', field2 = 5");
+ Object result = tbMapper.invoke("upsertSelective", tb.getObject());
+ Assert.assertEquals(result, 1);
+
+ tb.set("field2", 20);
+ tbMapper.invoke("upsertSelective", tb.getObject());
+
+ ResultSet rs = DBHelper.execute(sqlSession, "select * from tb where id = 10");
+ rs.first();
+ Assert.assertEquals(rs.getInt("field2"), 20);
+ }
+ });
+ }
+}
diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java b/src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java
index 729d62d..ef423a0 100644
--- a/src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java
+++ b/src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java
@@ -17,6 +17,7 @@
package com.itfsw.mybatis.generator.plugins.tools;
import org.apache.ibatis.io.Resources;
+import org.apache.ibatis.session.SqlSession;
import java.io.BufferedReader;
import java.io.IOException;
@@ -87,6 +88,18 @@ public class DBHelper {
connection.close();
}
+ /**
+ * 执行sql
+ *
+ * @param sqlSession
+ * @param sql
+ * @return
+ * @throws SQLException
+ */
+ public static ResultSet execute(SqlSession sqlSession, String sql) throws SQLException {
+ return execute(sqlSession.getConnection(), sql);
+ }
+
/**
* 执行sql
*
diff --git a/src/test/resources/scripts/UpsertPlugin/init.sql b/src/test/resources/scripts/UpsertPlugin/init.sql
new file mode 100644
index 0000000..aef1e81
--- /dev/null
+++ b/src/test/resources/scripts/UpsertPlugin/init.sql
@@ -0,0 +1,84 @@
+/*
+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-07-03 17:34:11
+*/
+
+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 COMMENT '注释2',
+ `field2` int(11) DEFAULT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
+
+-- ----------------------------
+-- Records of tb
+-- ----------------------------
+INSERT INTO `tb` VALUES ('1', 'fd1', null);
+INSERT INTO `tb` VALUES ('2', null, '2');
+INSERT INTO `tb` VALUES ('3', 'fd3', '3');
+
+-- ----------------------------
+-- Table structure for tb_blobs
+-- ----------------------------
+DROP TABLE IF EXISTS `tb_blobs`;
+CREATE TABLE `tb_blobs` (
+ `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
+ `field1` varchar(255) DEFAULT NULL,
+ `field2` longtext COMMENT '注释2',
+ `field3` longtext,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+-- ----------------------------
+-- Records of tb_blobs
+-- ----------------------------
+
+-- ----------------------------
+-- Table structure for tb_keys
+-- ----------------------------
+DROP TABLE IF EXISTS `tb_keys`;
+CREATE TABLE `tb_keys` (
+ `key1` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
+ `key2` varchar(255) NOT NULL,
+ `field1` varchar(255) DEFAULT NULL COMMENT '注释2',
+ `field2` int(11) DEFAULT NULL,
+ PRIMARY KEY (`key1`,`key2`)
+) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
+
+-- ----------------------------
+-- Records of tb_keys
+-- ----------------------------
+INSERT INTO `tb_keys` VALUES ('1', '2', 'fd1', null);
+INSERT INTO `tb_keys` VALUES ('2', '3', null, '2');
+INSERT INTO `tb_keys` VALUES ('3', '4', 'fd2', '3');
+
+-- ----------------------------
+-- Table structure for tb_single_blob
+-- ----------------------------
+DROP TABLE IF EXISTS `tb_single_blob`;
+CREATE TABLE `tb_single_blob` (
+ `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
+ `field1` longtext COMMENT '注释2',
+ `field2` int(11) DEFAULT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+-- ----------------------------
+-- Records of tb_single_blob
+-- ----------------------------
diff --git a/src/test/resources/scripts/UpsertPlugin/mybatis-generator-with-error-driver.xml b/src/test/resources/scripts/UpsertPlugin/mybatis-generator-with-error-driver.xml
new file mode 100644
index 0000000..5a63179
--- /dev/null
+++ b/src/test/resources/scripts/UpsertPlugin/mybatis-generator-with-error-driver.xml
@@ -0,0 +1,47 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/test/resources/scripts/UpsertPlugin/mybatis-generator.xml b/src/test/resources/scripts/UpsertPlugin/mybatis-generator.xml
new file mode 100644
index 0000000..61c5e22
--- /dev/null
+++ b/src/test/resources/scripts/UpsertPlugin/mybatis-generator.xml
@@ -0,0 +1,56 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file