Commit 6c458a09 authored by hewei's avatar hewei

略微优化下测试用例执行效率

parent d7eb5937
...@@ -63,7 +63,7 @@ public class IncrementsPluginTest { ...@@ -63,7 +63,7 @@ public class IncrementsPluginTest {
@Test @Test
public void testModelBuilderMethod() throws Exception { public void testModelBuilderMethod() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator.xml");
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
// 1. 测试生成的方法 // 1. 测试生成的方法
...@@ -112,7 +112,7 @@ public class IncrementsPluginTest { ...@@ -112,7 +112,7 @@ public class IncrementsPluginTest {
@Test @Test
public void testSqlAndExecute() throws Exception { public void testSqlAndExecute() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator.xml");
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
...@@ -214,7 +214,7 @@ public class IncrementsPluginTest { ...@@ -214,7 +214,7 @@ public class IncrementsPluginTest {
@Test @Test
public void testWithSelectiveEnhancedPlugin() throws Exception { public void testWithSelectiveEnhancedPlugin() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-selective-enhanced-plugin.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-selective-enhanced-plugin.xml");
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
// 1. 测试updateByExampleSelective // 1. 测试updateByExampleSelective
...@@ -307,7 +307,7 @@ public class IncrementsPluginTest { ...@@ -307,7 +307,7 @@ public class IncrementsPluginTest {
@Test @Test
public void testWithUpsertPlugin() throws Exception { public void testWithUpsertPlugin() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-upsert-plugin.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-upsert-plugin.xml");
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
...@@ -399,7 +399,7 @@ public class IncrementsPluginTest { ...@@ -399,7 +399,7 @@ public class IncrementsPluginTest {
@Test @Test
public void testWithAutoDelimitKeywords() throws Exception { public void testWithAutoDelimitKeywords() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-autoDelimitKeywords.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-autoDelimitKeywords.xml");
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
// 1. 测试updateByExample、updateByExampleSelective // 1. 测试updateByExample、updateByExampleSelective
...@@ -433,7 +433,7 @@ public class IncrementsPluginTest { ...@@ -433,7 +433,7 @@ public class IncrementsPluginTest {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-upsert-and-selective-enhanced-plugin.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/IncrementsPlugin/mybatis-generator-with-upsert-and-selective-enhanced-plugin.xml");
// upsertSelective 基于原生非空判断 // upsertSelective 基于原生非空判断
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
...@@ -459,7 +459,7 @@ public class IncrementsPluginTest { ...@@ -459,7 +459,7 @@ public class IncrementsPluginTest {
}); });
// upsertByExampleSelective 基于原生非空判断 // upsertByExampleSelective 基于原生非空判断
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
// 1. 测试updateByExampleSelective // 1. 测试updateByExampleSelective
...@@ -499,7 +499,7 @@ public class IncrementsPluginTest { ...@@ -499,7 +499,7 @@ public class IncrementsPluginTest {
}); });
// upsertSelective 基于指定字段 // upsertSelective 基于指定字段
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
...@@ -536,7 +536,7 @@ public class IncrementsPluginTest { ...@@ -536,7 +536,7 @@ public class IncrementsPluginTest {
}); });
// upsertByExampleSelective 基于指定字段 // upsertByExampleSelective 基于指定字段
tool.generate(() -> DBHelper.createDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/IncrementsPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
// 1. 测试updateByExampleSelective // 1. 测试updateByExampleSelective
......
...@@ -50,7 +50,7 @@ public class SelectSelectivePluginTest { ...@@ -50,7 +50,7 @@ public class SelectSelectivePluginTest {
public void testSelectByExampleSelective() throws Exception { public void testSelectByExampleSelective() throws Exception {
// 一些基础测试 // 一些基础测试
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/SelectSelectivePlugin/mybatis-generator.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/SelectSelectivePlugin/mybatis-generator.xml");
tool.generate(() -> DBHelper.createDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
// 1. 测试sql // 1. 测试sql
...@@ -111,7 +111,7 @@ public class SelectSelectivePluginTest { ...@@ -111,7 +111,7 @@ public class SelectSelectivePluginTest {
} }
}); });
// 测试Selective不传 // 测试Selective不传
tool.generate(() -> DBHelper.createDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
...@@ -134,7 +134,7 @@ public class SelectSelectivePluginTest { ...@@ -134,7 +134,7 @@ public class SelectSelectivePluginTest {
} }
}); });
// 测试WithBLOBs的情况 // 测试WithBLOBs的情况
tool.generate(() -> DBHelper.createDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbBlobsMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbBlobsMapper"))); ObjectUtil tbBlobsMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbBlobsMapper")));
...@@ -167,7 +167,7 @@ public class SelectSelectivePluginTest { ...@@ -167,7 +167,7 @@ public class SelectSelectivePluginTest {
} }
}); });
// 测试Key的情况 // 测试Key的情况
tool.generate(() -> DBHelper.createDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbKeysMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbKeysMapper"))); ObjectUtil tbKeysMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbKeysMapper")));
...@@ -209,7 +209,7 @@ public class SelectSelectivePluginTest { ...@@ -209,7 +209,7 @@ public class SelectSelectivePluginTest {
public void testSelectByPrimaryKeySelective() throws Exception { public void testSelectByPrimaryKeySelective() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/SelectSelectivePlugin/mybatis-generator.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/SelectSelectivePlugin/mybatis-generator.xml");
// 一些基础测试 // 一些基础测试
tool.generate(() -> DBHelper.createDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
// 1. 测试sql // 1. 测试sql
...@@ -243,7 +243,7 @@ public class SelectSelectivePluginTest { ...@@ -243,7 +243,7 @@ public class SelectSelectivePluginTest {
} }
}); });
// 测试Selective不传 // 测试Selective不传
tool.generate(() -> DBHelper.createDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
...@@ -261,7 +261,7 @@ public class SelectSelectivePluginTest { ...@@ -261,7 +261,7 @@ public class SelectSelectivePluginTest {
} }
}); });
// 测试WithBLOBs的情况 // 测试WithBLOBs的情况
tool.generate(() -> DBHelper.createDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbBlobsMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbBlobsMapper"))); ObjectUtil tbBlobsMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbBlobsMapper")));
...@@ -289,7 +289,7 @@ public class SelectSelectivePluginTest { ...@@ -289,7 +289,7 @@ public class SelectSelectivePluginTest {
} }
}); });
// 测试Key的情况 // 测试Key的情况
tool.generate(() -> DBHelper.createDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbKeysMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbKeysMapper"))); ObjectUtil tbKeysMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbKeysMapper")));
...@@ -368,7 +368,7 @@ public class SelectSelectivePluginTest { ...@@ -368,7 +368,7 @@ public class SelectSelectivePluginTest {
}); });
// 测试Selective不传 // 测试Selective不传
tool.generate(() -> DBHelper.createDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
...@@ -390,7 +390,7 @@ public class SelectSelectivePluginTest { ...@@ -390,7 +390,7 @@ public class SelectSelectivePluginTest {
} }
}); });
// 测试WithBLOBs的情况 // 测试WithBLOBs的情况
tool.generate(() -> DBHelper.createDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbBlobsMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbBlobsMapper"))); ObjectUtil tbBlobsMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbBlobsMapper")));
...@@ -422,7 +422,7 @@ public class SelectSelectivePluginTest { ...@@ -422,7 +422,7 @@ public class SelectSelectivePluginTest {
} }
}); });
// 测试Key的情况 // 测试Key的情况
tool.generate(() -> DBHelper.createDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/SelectSelectivePlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbKeysMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbKeysMapper"))); ObjectUtil tbKeysMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbKeysMapper")));
......
...@@ -19,6 +19,7 @@ package com.itfsw.mybatis.generator.plugins; ...@@ -19,6 +19,7 @@ package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.tools.*; import com.itfsw.mybatis.generator.plugins.tools.*;
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSession;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.exception.XMLParserException;
...@@ -36,6 +37,13 @@ import java.sql.SQLException; ...@@ -36,6 +37,13 @@ import java.sql.SQLException;
* --------------------------------------------------------------------------- * ---------------------------------------------------------------------------
*/ */
public class UpsertPluginTest { public class UpsertPluginTest {
/**
* 初始化
*/
@BeforeClass
public static void init() throws Exception{
DBHelper.createDB("scripts/UpsertPlugin/init.sql");
}
/** /**
* 测试配置异常 * 测试配置异常
...@@ -59,7 +67,7 @@ public class UpsertPluginTest { ...@@ -59,7 +67,7 @@ public class UpsertPluginTest {
@Test @Test
public void testUpsert() throws Exception { public void testUpsert() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml");
tool.generate(() -> DBHelper.createDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
...@@ -91,7 +99,7 @@ public class UpsertPluginTest { ...@@ -91,7 +99,7 @@ public class UpsertPluginTest {
@Test @Test
public void testUpsertWithBLOBs() throws Exception { public void testUpsertWithBLOBs() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml");
tool.generate(() -> DBHelper.createDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
// 1. 多个 blob // 1. 多个 blob
...@@ -145,7 +153,7 @@ public class UpsertPluginTest { ...@@ -145,7 +153,7 @@ public class UpsertPluginTest {
@Test @Test
public void testUpsertSelective() throws Exception { public void testUpsertSelective() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml");
tool.generate(() -> DBHelper.createDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
// 1. 普通 // 1. 普通
...@@ -184,7 +192,7 @@ public class UpsertPluginTest { ...@@ -184,7 +192,7 @@ public class UpsertPluginTest {
@Test @Test
public void testUpsertByExample() throws Exception { public void testUpsertByExample() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml");
tool.generate(() -> DBHelper.createDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
...@@ -220,7 +228,7 @@ public class UpsertPluginTest { ...@@ -220,7 +228,7 @@ public class UpsertPluginTest {
@Test @Test
public void testUpsertByExampleWithBLOBs() throws Exception { public void testUpsertByExampleWithBLOBs() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml");
tool.generate(() -> DBHelper.createDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
// 1. 多个 blob // 1. 多个 blob
...@@ -281,7 +289,7 @@ public class UpsertPluginTest { ...@@ -281,7 +289,7 @@ public class UpsertPluginTest {
@Test @Test
public void testUpsertByExampleSelective() throws Exception { public void testUpsertByExampleSelective() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml");
tool.generate(() -> DBHelper.createDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
// 1. 普通 // 1. 普通
...@@ -326,7 +334,7 @@ public class UpsertPluginTest { ...@@ -326,7 +334,7 @@ public class UpsertPluginTest {
@Test @Test
public void testWithIdentityAndGeneratedAlwaysColumns() throws Exception { public void testWithIdentityAndGeneratedAlwaysColumns() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator.xml");
tool.generate(() -> DBHelper.createDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
// ------------------------------------------ upsert --------------------------------------------------- // ------------------------------------------ upsert ---------------------------------------------------
......
...@@ -37,15 +37,24 @@ import java.util.Properties; ...@@ -37,15 +37,24 @@ import java.util.Properties;
public class DBHelper { public class DBHelper {
private static final String DB_CONFIG = "db.properties"; private static final String DB_CONFIG = "db.properties";
public static Properties properties; // 数据库信息 public static Properties properties; // 数据库信息
private static Connection connection; // 数据库连接
private static String dbLock;
static { static {
try { try {
// 获取数据库配置信息 // 获取数据库配置信息
properties = new Properties(); properties = new Properties();
InputStream inputStream = Resources.getResourceAsStream(DB_CONFIG); try (InputStream inputStream = Resources.getResourceAsStream(DB_CONFIG)) {
properties.load(inputStream); properties.load(inputStream);
inputStream.close(); }
} catch (IOException e) { // 数据库连接
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
...@@ -53,19 +62,43 @@ public class DBHelper { ...@@ -53,19 +62,43 @@ public class DBHelper {
/** /**
* 创建数据库 * 创建数据库
* @param resource * @param resource
* @throws ClassNotFoundException
* @throws SQLException * @throws SQLException
* @throws IOException * @throws IOException
*/ */
public static void createDB(String resource) throws ClassNotFoundException, SQLException, IOException { public static void createDB(String resource) throws SQLException, IOException {
String driver = properties.getProperty("driver"); try (
String url = properties.getProperty("url"); Statement statement = connection.createStatement();
String username = properties.getProperty("username"); // 获取建表和初始化sql
String password = properties.getProperty("password"); InputStream inputStream = Resources.getResourceAsStream(resource);
// 获取connection InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
Class.forName(driver); BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
) {
// 读取sql语句执行
StringBuffer sb = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
if (!line.startsWith("--")) {
sb.append(line).append("\n");
}
}
statement.execute(sb.toString());
dbLock = resource;
}
}
/**
* 重置数据库
* @param resource
* @throws SQLException
* @throws IOException
*/
public static void resetDB(String resource) throws Exception {
if (dbLock == null || !dbLock.equals(resource)) {
throw new Exception("重置数据库只能重置已锁定的!");
}
try ( try (
Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
// 获取建表和初始化sql // 获取建表和初始化sql
InputStream inputStream = Resources.getResourceAsStream(resource); InputStream inputStream = Resources.getResourceAsStream(resource);
...@@ -76,10 +109,20 @@ public class DBHelper { ...@@ -76,10 +109,20 @@ public class DBHelper {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
String line; String line;
while ((line = bufferedReader.readLine()) != null) { while ((line = bufferedReader.readLine()) != null) {
sb.append(line).append("\n"); if (!line.startsWith("--")) {
if (line.matches(".*;$")) { sb.append(line).append("\n");
statement.execute(sb.toString());
sb.setLength(0); if (line.matches(".*;$\\s*")) {
String sql = sb.toString().trim();
if (sql.startsWith("DROP")) {
statement.execute(sql.replace("DROP TABLE IF EXISTS", "TRUNCATE TABLE"));
} else if (!sql.startsWith("CREATE")) {
statement.execute(sql);
}
sb.setLength(0);
}
} }
} }
} }
......
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