From 0f16031f4a7e2e4685521082ffcf19e405ae862c Mon Sep 17 00:00:00 2001 From: hewei Date: Wed, 25 Apr 2018 19:02:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=B7=A5=E5=85=B7=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E6=AF=8F=E6=AC=A1=E6=89=A7=E8=A1=8C=E5=89=8D?= =?UTF-8?q?=E9=87=8D=E7=BD=AE=E6=95=B0=E6=8D=AE=EF=BC=8C=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E8=84=8F=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/ExampleEnhancedPluginTest.java | 8 +- .../plugins/IncrementsPluginTest.java | 110 +++++++++++++----- .../generator/plugins/LimitPluginTest.java | 2 +- .../plugins/LogicalDeletePluginTest.java | 12 +- .../plugins/ModelBuilderPluginTest.java | 6 +- .../plugins/ModelColumnPluginTest.java | 4 +- .../OldSelectiveEnhancedPluginTest.java | 12 +- .../plugins/SelectOneByExamplePluginTest.java | 6 +- .../plugins/SelectiveEnhancedPluginTest.java | 10 +- .../generator/plugins/UpsertPluginTest.java | 14 +-- .../plugins/tools/IBeforeCallback.java | 32 +++++ .../plugins/tools/MyBatisGeneratorTool.java | 18 ++- .../scripts/IncrementsPlugin/init.sql | 2 +- ...h-upsert-and-selective-enhanced-plugin.xml | 65 +++++++++++ 14 files changed, 231 insertions(+), 70 deletions(-) create mode 100644 src/test/java/com/itfsw/mybatis/generator/plugins/tools/IBeforeCallback.java create mode 100644 src/test/resources/scripts/IncrementsPlugin/mybatis-generator-with-upsert-and-selective-enhanced-plugin.xml diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPluginTest.java index b3674fc..daada76 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPluginTest.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPluginTest.java @@ -21,8 +21,6 @@ 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.lang.reflect.Array; @@ -52,7 +50,7 @@ public class ExampleEnhancedPluginTest { * 测试生成的example方法 */ @Test - public void testExample() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testExample() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ExampleEnhancedPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -73,7 +71,7 @@ public class ExampleEnhancedPluginTest { * 测试生成的orderBy方法 */ @Test - public void testOrderBy() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testOrderBy() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ExampleEnhancedPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -96,7 +94,7 @@ public class ExampleEnhancedPluginTest { * 测试andIf方法 */ @Test - public void testAndIf() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testAndIf() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ExampleEnhancedPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java index 451ff02..0487537 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java @@ -19,16 +19,11 @@ 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.Before; import org.junit.Test; -import org.mybatis.generator.exception.InvalidConfigurationException; -import org.mybatis.generator.exception.XMLParserException; -import java.io.IOException; import java.lang.reflect.Array; import java.lang.reflect.Method; import java.sql.ResultSet; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -41,15 +36,6 @@ import java.util.List; * --------------------------------------------------------------------------- */ public class IncrementsPluginTest { - - /** - * 初始化数据库 - */ - @Before - public void init() throws Exception { - DBHelper.createDB("scripts/IncrementsPlugin/init.sql"); - } - /** * 测试没有配置ModelBuilderPlugin */ @@ -67,7 +53,7 @@ public class IncrementsPluginTest { @Test public void testModelBuilderMethod() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator.xml"); - tool.generate(new AbstractShellCallback() { + tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() { @Override public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { // 1. 测试生成的方法 @@ -116,7 +102,7 @@ public class IncrementsPluginTest { @Test public void testSqlAndExecute() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator.xml"); - tool.generate(new AbstractShellCallback() { + tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() { @Override public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { @@ -216,9 +202,9 @@ public class IncrementsPluginTest { * 测试整合 SelectiveEnhancedPlugin 插件 */ @Test - public void testWithSelectiveEnhancedPlugin() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testWithSelectiveEnhancedPlugin() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-selective-enhanced-plugin.xml"); - tool.generate(new AbstractShellCallback() { + tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() { @Override public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { // 1. 测试updateByExampleSelective @@ -309,9 +295,9 @@ public class IncrementsPluginTest { * 测试整合 UpsertPlugin */ @Test - public void testWithUpsertPlugin() throws InterruptedException, SQLException, InvalidConfigurationException, IOException, XMLParserException { + public void testWithUpsertPlugin() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-upsert-plugin.xml"); - tool.generate(new AbstractShellCallback() { + tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.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"))); @@ -364,11 +350,12 @@ public class IncrementsPluginTest { // sql sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertSelective", tbBuilder.invoke("build")); Assert.assertEquals(sql, "insert into tb ( id, field1, inc_f1, inc_f2 ) values ( 30, 'ts2', 10, 1 ) on duplicate key update id = 30, field1 = 'ts2', inc_f1 = inc_f1 + 10 , inc_f2 = 1"); - result = tbMapper.invoke("upsert", tbBuilder.invoke("build")); + result = tbMapper.invoke("upsertSelective", tbBuilder.invoke("build")); Assert.assertEquals(result, 1); // 再次执行触发update tbBuilder.invoke("incF1", 10L, tbBuilderInc.getObject()); - tbMapper.invoke("upsertSelective", tbBuilder.invoke("build")); + result = tbMapper.invoke("upsertSelective", tbBuilder.invoke("build")); + Assert.assertEquals(result, 2); rs = DBHelper.execute(sqlSession, "select * from tb where id = 30"); rs.first(); Assert.assertEquals(rs.getInt("inc_f1"), 20); @@ -383,11 +370,12 @@ public class IncrementsPluginTest { // sql sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertByExampleSelective", tbBuilder.invoke("build"), tbExample.getObject()); Assert.assertEquals(sql, "update tb set id = 40, field1 = 'ts3', inc_f1 = inc_f1 + 10 , inc_f2 = 1 WHERE ( field1 = 'ts3' ) ; insert into tb ( id, field1, inc_f1, inc_f2 ) select 40, 'ts3', 10, 1 from dual where not exists ( select 1 from tb WHERE ( field1 = 'ts3' ) )"); - result = tbMapper.invoke("upsert", tbBuilder.invoke("build")); - Assert.assertEquals(result, 1); + result = tbMapper.invoke("upsertByExampleSelective", tbBuilder.invoke("build"), tbExample.getObject()); + Assert.assertEquals(result, 0); // 再次执行触发update tbBuilder.invoke("incF1", 10L, tbBuilderInc.getObject()); - tbMapper.invoke("upsertByExampleSelective", tbBuilder.invoke("build"), tbExample.getObject()); + result = tbMapper.invoke("upsertByExampleSelective", tbBuilder.invoke("build"), tbExample.getObject()); + Assert.assertEquals(result, 1); rs = DBHelper.execute(sqlSession, "select * from tb where id = 40"); rs.first(); Assert.assertEquals(rs.getInt("inc_f1"), 20); @@ -399,9 +387,9 @@ public class IncrementsPluginTest { * 测试 autoDelimitKeywords */ @Test - public void testWithAutoDelimitKeywords() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testWithAutoDelimitKeywords() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-autoDelimitKeywords.xml"); - tool.generate(new AbstractShellCallback() { + tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() { @Override public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { // 1. 测试updateByExample、updateByExampleSelective @@ -426,4 +414,72 @@ public class IncrementsPluginTest { } }); } + + /** + * 测试同时整合 UpsertPlugin 和 SelectiveEnhancedPlugin + */ + @Test + public void testWithUpsertAndSelectiveEnhancedPlugin() throws Exception { + MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-upsert-and-selective-enhanced-plugin.xml"); + tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() { + @Override + public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { + + + // --------------------------- upsertSelective (非空判断) --------------------------------- + ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); + + ObjectUtil tbBuilder = new ObjectUtil(loader, packagz + ".Tb$Builder"); + ObjectUtil tbBuilderInc = new ObjectUtil(loader, packagz + ".Tb$Builder$Inc#INC"); + tbBuilder.invoke("id", 10L); + tbBuilder.invoke("field1", "ts1"); + tbBuilder.invoke("incF1", 10L, tbBuilderInc.getObject()); + + // sql + String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertSelective", tbBuilder.invoke("build")); + Assert.assertEquals(sql, "insert into tb ( id, field1, inc_f1 ) values ( 10, 'ts1', 10 ) on duplicate key update id = 10, field1 = 'ts1', inc_f1 = inc_f1 + 10"); + Object result = tbMapper.invoke("upsertSelective", tbBuilder.invoke("build"), Array.newInstance(new ObjectUtil(loader, packagz + ".Tb$Column#field1").getCls(), 0)); + Assert.assertEquals(result, 1); + // 再次执行触发update + tbBuilder.invoke("incF1", 10L, tbBuilderInc.getObject()); + tbMapper.invoke("upsertSelective", tbBuilder.invoke("build"), Array.newInstance(new ObjectUtil(loader, packagz + ".Tb$Column#field1").getCls(), 0)); + ResultSet rs = DBHelper.execute(sqlSession, "select * from tb where id = 10"); + rs.first(); + Assert.assertEquals(rs.getInt("inc_f1"), 20); + + + // --------------------------- upsertSelective (指定字段) --------------------------------- + tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); + + tbBuilder = new ObjectUtil(loader, packagz + ".Tb$Builder"); + tbBuilderInc = new ObjectUtil(loader, packagz + ".Tb$Builder$Inc#INC"); + tbBuilder.invoke("id", 20L); + tbBuilder.invoke("field1", "ts1"); + tbBuilder.invoke("incF1", 20L, tbBuilderInc.getObject()); + + ObjectUtil TbColumnId = new ObjectUtil(loader, packagz + ".Tb$Column#id"); + ObjectUtil TbColumnField1 = new ObjectUtil(loader, packagz + ".Tb$Column#field1"); + ObjectUtil TbColumnIncF1 = new ObjectUtil(loader, packagz + ".Tb$Column#incF1"); + ObjectUtil TbColumnIncF2 = new ObjectUtil(loader, packagz + ".Tb$Column#incF2"); + Object columns = Array.newInstance(TbColumnField1.getCls(), 4); + Array.set(columns, 0, TbColumnId.getObject()); + Array.set(columns, 1, TbColumnField1.getObject()); + Array.set(columns, 2, TbColumnIncF1.getObject()); + Array.set(columns, 3, TbColumnIncF2.getObject()); + + // sql + sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "upsertSelective", tbBuilder.invoke("build"), columns); + Assert.assertEquals(sql, "insert into tb ( id , field1 , inc_f1 , inc_f2 ) values ( 20 , 'ts1' , 20 , null ) on duplicate key update id = 20 , field1 = 'ts1' , inc_f1 = inc_f1 + 20 , inc_f2 = null"); + result = tbMapper.invoke("upsertSelective", tbBuilder.invoke("build"), columns); + Assert.assertEquals(result, 1); + // 再次执行触发update + tbBuilder.invoke("incF1", 20L, tbBuilderInc.getObject()); + result = tbMapper.invoke("upsertSelective", tbBuilder.invoke("build"), columns); + Assert.assertEquals(result, 2); + rs = DBHelper.execute(sqlSession, "select * from tb where id = 20"); + rs.first(); + Assert.assertEquals(rs.getInt("inc_f1"), 40); + } + }); + } } diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/LimitPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/LimitPluginTest.java index c57bcc9..eec87e9 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/LimitPluginTest.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/LimitPluginTest.java @@ -61,7 +61,7 @@ public class LimitPluginTest { * 测试生成的Sql语句和具体执行 */ @Test - public void testSqlAndExecute() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testSqlAndExecute() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LimitPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePluginTest.java index d10143d..75e3a86 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePluginTest.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePluginTest.java @@ -80,7 +80,7 @@ public class LogicalDeletePluginTest { * 测试 logicalDeleteByExample */ @Test - public void testLogicalDeleteByExample() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testLogicalDeleteByExample() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LogicalDeletePlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -108,7 +108,7 @@ public class LogicalDeletePluginTest { * 测试 logicalDeleteByPrimaryKey */ @Test - public void testLogicalDeleteByPrimaryKey() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testLogicalDeleteByPrimaryKey() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LogicalDeletePlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -132,7 +132,7 @@ public class LogicalDeletePluginTest { * 测试关联生成的方法和常量 */ @Test - public void testOtherMethods() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testOtherMethods() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LogicalDeletePlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -159,7 +159,7 @@ public class LogicalDeletePluginTest { * 测试自定义常量 */ @Test - public void testCustomConst() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testCustomConst() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LogicalDeletePlugin/mybatis-generator-with-customConstName.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -186,7 +186,7 @@ public class LogicalDeletePluginTest { * 测试Model andLogicalDeleted 方法 */ @Test - public void testModelAndLogicalDeletedMethod() throws InterruptedException, SQLException, InvalidConfigurationException, IOException, XMLParserException { + public void testModelAndLogicalDeletedMethod() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LogicalDeletePlugin/mybatis-generator-with-customConstName.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -206,7 +206,7 @@ public class LogicalDeletePluginTest { * 测试 selectByPrimaryKeyWithLogicalDelete */ @Test - public void testSelectByPrimaryKeyWithLogicalDelete() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testSelectByPrimaryKeyWithLogicalDelete() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LogicalDeletePlugin/mybatis-generator-with-customConstName.xml"); tool.generate(new AbstractShellCallback() { @Override diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPluginTest.java index 3ab5ed2..445a770 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPluginTest.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPluginTest.java @@ -24,8 +24,6 @@ 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.SQLException; @@ -51,7 +49,7 @@ public class ModelBuilderPluginTest { * 测试生成的model */ @Test - public void test() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void test() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ModelBuilderPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -113,7 +111,7 @@ public class ModelBuilderPluginTest { * 测试静态builder方法 */ @Test - public void testBuilderMethod() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testBuilderMethod() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ModelBuilderPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/ModelColumnPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/ModelColumnPluginTest.java index ccd9925..b3e3ef7 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/ModelColumnPluginTest.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/ModelColumnPluginTest.java @@ -24,8 +24,6 @@ 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.SQLException; @@ -51,7 +49,7 @@ public class ModelColumnPluginTest { * 测试生成的model */ @Test - public void test() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void test() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ModelColumnPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/OldSelectiveEnhancedPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/OldSelectiveEnhancedPluginTest.java index 3764b66..964149c 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/OldSelectiveEnhancedPluginTest.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/OldSelectiveEnhancedPluginTest.java @@ -65,7 +65,7 @@ public class OldSelectiveEnhancedPluginTest { * 测试Model */ @Test - public void testModel() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testModel() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/OldSelectiveEnhancedPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -91,7 +91,7 @@ public class OldSelectiveEnhancedPluginTest { * 测试insertSelective增强 */ @Test - public void testInsertSelective() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testInsertSelective() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/OldSelectiveEnhancedPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -125,7 +125,7 @@ public class OldSelectiveEnhancedPluginTest { * 测试 updateByExampleSelective */ @Test - public void testUpdateByExampleSelective() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testUpdateByExampleSelective() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/OldSelectiveEnhancedPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -160,7 +160,7 @@ public class OldSelectiveEnhancedPluginTest { * 测试 updateByPrimaryKeySelective */ @Test - public void testUpdateByPrimaryKeySelective() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testUpdateByPrimaryKeySelective() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/OldSelectiveEnhancedPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -192,7 +192,7 @@ public class OldSelectiveEnhancedPluginTest { * 测试 upsertSelective */ @Test - public void testUpsertSelective() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testUpsertSelective() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/OldSelectiveEnhancedPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -226,7 +226,7 @@ public class OldSelectiveEnhancedPluginTest { * 测试 upsertByExampleSelective */ @Test - public void testUpsertByExampleSelective() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testUpsertByExampleSelective() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/OldSelectiveEnhancedPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/SelectOneByExamplePluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/SelectOneByExamplePluginTest.java index 565a1d3..3c69a96 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/SelectOneByExamplePluginTest.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/SelectOneByExamplePluginTest.java @@ -21,8 +21,6 @@ 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.SQLException; @@ -48,7 +46,7 @@ public class SelectOneByExamplePluginTest { * 测试 selectOneByExample */ @Test - public void testSelectOneByExample() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testSelectOneByExample() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/SelectOneByExamplePlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -75,7 +73,7 @@ public class SelectOneByExamplePluginTest { * 测试 selectOneByExampleWithBLOBs */ @Test - public void testSelectOneByExampleWithBLOBs() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testSelectOneByExampleWithBLOBs() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/SelectOneByExamplePlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPluginTest.java index 920b863..2f153c7 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPluginTest.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPluginTest.java @@ -65,7 +65,7 @@ public class SelectiveEnhancedPluginTest { * 测试insertSelective增强 */ @Test - public void testInsertSelective() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testInsertSelective() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/SelectiveEnhancedPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -98,7 +98,7 @@ public class SelectiveEnhancedPluginTest { * 测试 updateByExampleSelective */ @Test - public void testUpdateByExampleSelective() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testUpdateByExampleSelective() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/SelectiveEnhancedPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -132,7 +132,7 @@ public class SelectiveEnhancedPluginTest { * 测试 updateByPrimaryKeySelective */ @Test - public void testUpdateByPrimaryKeySelective() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testUpdateByPrimaryKeySelective() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/SelectiveEnhancedPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -163,7 +163,7 @@ public class SelectiveEnhancedPluginTest { * 测试 upsertSelective */ @Test - public void testUpsertSelective() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testUpsertSelective() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/SelectiveEnhancedPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -196,7 +196,7 @@ public class SelectiveEnhancedPluginTest { * 测试 upsertByExampleSelective */ @Test - public void testUpsertByExampleSelective() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testUpsertByExampleSelective() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/SelectiveEnhancedPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java index daec3af..14274a9 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java @@ -65,7 +65,7 @@ public class UpsertPluginTest { * 测试 upsert */ @Test - public void testUpsert() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testUpsert() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -97,7 +97,7 @@ public class UpsertPluginTest { * 测试 upsertWithBLOBs */ @Test - public void testUpsertWithBLOBs() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testUpsertWithBLOBs() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -151,7 +151,7 @@ public class UpsertPluginTest { * 测试 upsertSelective */ @Test - public void testUpsertSelective() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testUpsertSelective() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -190,7 +190,7 @@ public class UpsertPluginTest { * 测试 upsertByExample */ @Test - public void testUpsertByExample() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testUpsertByExample() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -226,7 +226,7 @@ public class UpsertPluginTest { * 测试 upsertByExampleWithBLOBs */ @Test - public void testUpsertByExampleWithBLOBs() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testUpsertByExampleWithBLOBs() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -287,7 +287,7 @@ public class UpsertPluginTest { * 测试 upsertByExampleSelective */ @Test - public void testUpsertByExampleSelective() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testUpsertByExampleSelective() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override @@ -332,7 +332,7 @@ public class UpsertPluginTest { * 测试 存在自增主键的情况 */ @Test - public void testWithIdentityAndGeneratedAlwaysColumns() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + public void testWithIdentityAndGeneratedAlwaysColumns() throws Exception { MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml"); tool.generate(new AbstractShellCallback() { @Override diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/tools/IBeforeCallback.java b/src/test/java/com/itfsw/mybatis/generator/plugins/tools/IBeforeCallback.java new file mode 100644 index 0000000..d98e8fc --- /dev/null +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/tools/IBeforeCallback.java @@ -0,0 +1,32 @@ +/* + * 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. + */ + +package com.itfsw.mybatis.generator.plugins.tools; + +/** + * --------------------------------------------------------------------------- + * 回调之前执行 + * --------------------------------------------------------------------------- + * @author: hewei + * @time:2018/4/25 18:42 + * --------------------------------------------------------------------------- + */ +public interface IBeforeCallback { + /** + * 执行 + */ + void run() throws Exception; +} diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/tools/MyBatisGeneratorTool.java b/src/test/java/com/itfsw/mybatis/generator/plugins/tools/MyBatisGeneratorTool.java index 47e83a1..25f42fe 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/tools/MyBatisGeneratorTool.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/tools/MyBatisGeneratorTool.java @@ -81,19 +81,35 @@ public class MyBatisGeneratorTool { /** * 执行MyBatisGenerator + * @param before * @param callback * @return * @throws SQLException * @throws IOException * @throws InterruptedException */ - public MyBatisGenerator generate(AbstractShellCallback callback) throws InvalidConfigurationException, InterruptedException, SQLException, IOException { + public MyBatisGenerator generate(IBeforeCallback before, AbstractShellCallback callback) throws Exception { + before.run(); callback.setTool(this); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null, null, null, true); return myBatisGenerator; } + /** + * 执行MyBatisGenerator + * @param callback + * @return + * @throws SQLException + * @throws IOException + * @throws InterruptedException + */ + public MyBatisGenerator generate(AbstractShellCallback callback) throws Exception { + return this.generate(() -> { + + }, callback); + } + /** * 执行MyBatisGenerator(不生成文件) * @return diff --git a/src/test/resources/scripts/IncrementsPlugin/init.sql b/src/test/resources/scripts/IncrementsPlugin/init.sql index fd9cc27..6b08d4c 100644 --- a/src/test/resources/scripts/IncrementsPlugin/init.sql +++ b/src/test/resources/scripts/IncrementsPlugin/init.sql @@ -23,7 +23,7 @@ CREATE TABLE `tb` ( `id` bigint(20) NOT NULL COMMENT '注释1', `field1` varchar(255) DEFAULT NULL COMMENT '注释2', `inc_f1` bigint(20) NOT NULL DEFAULT '0', - `inc_f2` bigint(20) NOT NULL DEFAULT '0', + `inc_f2` bigint(20) DEFAULT '0', `inc_f3` bigint(20) DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; diff --git a/src/test/resources/scripts/IncrementsPlugin/mybatis-generator-with-upsert-and-selective-enhanced-plugin.xml b/src/test/resources/scripts/IncrementsPlugin/mybatis-generator-with-upsert-and-selective-enhanced-plugin.xml new file mode 100644 index 0000000..0437910 --- /dev/null +++ b/src/test/resources/scripts/IncrementsPlugin/mybatis-generator-with-upsert-and-selective-enhanced-plugin.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
+ + +
+
+
\ No newline at end of file