测试工具优化,每次执行前重置数据,防止脏数据
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
+6
-6
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
<?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">
|
||||
<!-- 插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.IncrementsPlugin" />
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin" />
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.UpsertPlugin">
|
||||
<property name="allowMultiQueries" value="true"/>
|
||||
</plugin>
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.SelectiveEnhancedPlugin"/>
|
||||
|
||||
<!--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">
|
||||
<property name="incrementsColumns" value="inc_f1"/>
|
||||
</table>
|
||||
<table tableName="tb_keys">
|
||||
<property name="incrementsColumns" value="inc_f1,inc_f2,inc_f3"/>
|
||||
</table>
|
||||
<table tableName="tb_blobs">
|
||||
<property name="incrementsColumns" value="inc_f1,inc_f3"/>
|
||||
</table>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
Reference in New Issue
Block a user