Commit a87dcf9a authored by hewei's avatar hewei

UpsertPlugin 插件增加批量操作方法

parent f6b040a4
...@@ -147,7 +147,7 @@ public class BatchInsertPluginTest { ...@@ -147,7 +147,7 @@ public class BatchInsertPluginTest {
@Test @Test
public void testBatchInsert() throws Exception { public void testBatchInsert() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BatchInsertPlugin/mybatis-generator.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BatchInsertPlugin/mybatis-generator.xml");
tool.generate(new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/BatchInsertPlugin/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
...@@ -184,7 +184,7 @@ public class BatchInsertPluginTest { ...@@ -184,7 +184,7 @@ public class BatchInsertPluginTest {
@Test @Test
public void testBatchInsertSelective() throws Exception { public void testBatchInsertSelective() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BatchInsertPlugin/mybatis-generator.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BatchInsertPlugin/mybatis-generator.xml");
tool.generate(new AbstractShellCallback() { tool.generate(() -> DBHelper.resetDB("scripts/BatchInsertPlugin/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
......
...@@ -25,8 +25,11 @@ import org.mybatis.generator.exception.InvalidConfigurationException; ...@@ -25,8 +25,11 @@ import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.exception.XMLParserException;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Array;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/** /**
* --------------------------------------------------------------------------- * ---------------------------------------------------------------------------
...@@ -41,7 +44,7 @@ public class UpsertPluginTest { ...@@ -41,7 +44,7 @@ public class UpsertPluginTest {
* 初始化 * 初始化
*/ */
@BeforeClass @BeforeClass
public static void init() throws Exception{ public static void init() throws Exception {
DBHelper.createDB("scripts/UpsertPlugin/init.sql"); DBHelper.createDB("scripts/UpsertPlugin/init.sql");
} }
...@@ -421,4 +424,136 @@ public class UpsertPluginTest { ...@@ -421,4 +424,136 @@ public class UpsertPluginTest {
} }
}); });
} }
/**
* 测试批量batchUpsert
*/
@Test
public void testBatchUpsert() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator-with-allowBatchUpsert.xml");
tool.generate(() -> DBHelper.resetDB("scripts/UpsertPlugin/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")));
List<Object> params = new ArrayList<>();
params.add(new ObjectUtil(loader, packagz + ".Tb").set("id", 1L).set("field1", "ts1").getObject());
params.add(new ObjectUtil(loader, packagz + ".Tb").set("id", 6L).set("field1", "ts2").set("field2", 1).getObject());
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "batchUpsert", params);
Assert.assertEquals(sql, "insert into tb (id, field1, field2) values (1, 'ts1', null ) , (6, 'ts2', 1 ) on duplicate key update id = values(id), field1 = values(field1), field2 = values(field2)");
// 2. 执行sql
Object count = tbMapper.invoke("batchUpsert", params);
Assert.assertEquals(count, 3);
// 验证
ResultSet rs = DBHelper.execute(sqlSession, "select * from tb where id = 1");
rs.first();
Assert.assertEquals(rs.getString("field1"), "ts1");
rs = DBHelper.execute(sqlSession, "select * from tb where id = 6");
rs.first();
Assert.assertEquals(rs.getString("field1"), "ts2");
}
});
}
/**
* 测试批量batchUpsertWithBLOBs
*/
@Test
public void testBatchUpsertWithBLOBs() throws Exception {
// 1. batchUpsert
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator-with-allowBatchUpsert.xml");
tool.generate(() -> DBHelper.resetDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbBlobsMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbBlobsMapper")));
List<Object> params = new ArrayList<>();
params.add(new ObjectUtil(loader, packagz + ".TbBlobs").set("id", 1L).set("field1", "ts1").getObject());
params.add(new ObjectUtil(loader, packagz + ".TbBlobs").set("id", 6L).set("field1", "ts2").getObject());
String sql = SqlHelper.getFormatMapperSql(tbBlobsMapper.getObject(), "batchUpsert", params);
Assert.assertEquals(sql, "insert into tb_blobs (id, field1) values (1, 'ts1') , (6, 'ts2') on duplicate key update id = values(id), field1 = values(field1)");
// 2. 执行sql
Object count = tbBlobsMapper.invoke("batchUpsert", params);
Assert.assertEquals(count, 3);
// 验证
ResultSet rs = DBHelper.execute(sqlSession, "select * from tb_blobs where id = 1");
rs.first();
Assert.assertEquals(rs.getString("field1"), "ts1");
rs = DBHelper.execute(sqlSession, "select * from tb_blobs where id = 6");
rs.first();
Assert.assertEquals(rs.getString("field1"), "ts2");
}
});
// 2. batchUpsertWithBLOBs
tool.generate(() -> DBHelper.resetDB("scripts/UpsertPlugin/init.sql"), new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbBlobsMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbBlobsMapper")));
List<Object> params = new ArrayList<>();
params.add(new ObjectUtil(loader, packagz + ".TbBlobsWithBLOBs").set("id", 1L).set("field1", "ts1").set("field3", "ff1").getObject());
params.add(new ObjectUtil(loader, packagz + ".TbBlobsWithBLOBs").set("id", 6L).set("field1", "ts2").set("field3", "ff2").getObject());
String sql = SqlHelper.getFormatMapperSql(tbBlobsMapper.getObject(), "batchUpsertWithBLOBs", params);
Assert.assertEquals(sql, "insert into tb_blobs (id, field1, field2, field3) values (1, 'ts1', 'null', 'ff1') , (6, 'ts2', 'null', 'ff2') on duplicate key update id = values(id), field1 = values(field1), field2 = values(field2), field3 = values(field3)");
// 2. 执行sql
Object count = tbBlobsMapper.invoke("batchUpsertWithBLOBs", params);
Assert.assertEquals(count, 3);
// 验证
ResultSet rs = DBHelper.execute(sqlSession, "select * from tb_blobs where id = 1");
rs.first();
Assert.assertEquals(rs.getString("field1"), "ts1");
Assert.assertEquals(rs.getString("field3"), "ff1");
rs = DBHelper.execute(sqlSession, "select * from tb_blobs where id = 6");
rs.first();
Assert.assertEquals(rs.getString("field1"), "ts2");
Assert.assertEquals(rs.getString("field3"), "ff2");
}
});
}
/**
* 测试批量batchUpsertSelective
*/
@Test
public void testBatchUpsertSelective() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/UpsertPlugin/mybatis-generator-with-allowBatchUpsert.xml");
tool.generate(() -> DBHelper.resetDB("scripts/UpsertPlugin/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")));
List<Object> params = new ArrayList<>();
params.add(new ObjectUtil(loader, packagz + ".Tb").set("id", 1L).set("field1", "ts1").getObject());
params.add(new ObjectUtil(loader, packagz + ".Tb").set("id", 6L).set("field1", "ts2").set("field2", 1).getObject());
ObjectUtil columnId = new ObjectUtil(loader, packagz + ".Tb$Column#id");
ObjectUtil columnField1 = new ObjectUtil(loader, packagz + ".Tb$Column#field1");
Object columns = Array.newInstance(columnId.getCls(), 2);
Array.set(columns, 0, columnId.getObject());
Array.set(columns, 1, columnField1.getObject());
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "batchUpsertSelective", params, columns);
Assert.assertEquals(sql, "insert into tb ( id , field1 ) values ( 1 , 'ts1' ) , ( 6 , 'ts2' ) on duplicate key update id = values(id) , field1 = values(field1)");
// 2. 执行sql
Object count = tbMapper.invoke("batchUpsertSelective", params, columns);
Assert.assertEquals(count, 3);
// 验证
ResultSet rs = DBHelper.execute(sqlSession, "select * from tb where id = 1");
rs.first();
Assert.assertEquals(rs.getString("field1"), "ts1");
rs = DBHelper.execute(sqlSession, "select * from tb where id = 6");
rs.first();
Assert.assertEquals(rs.getString("field1"), "ts2");
Assert.assertNull(rs.getString("field2"));
}
});
}
} }
...@@ -48,6 +48,9 @@ CREATE TABLE `tb_blobs` ( ...@@ -48,6 +48,9 @@ CREATE TABLE `tb_blobs` (
-- ---------------------------- -- ----------------------------
-- Records of tb_blobs -- Records of tb_blobs
-- ---------------------------- -- ----------------------------
INSERT INTO `tb_blobs` VALUES ('1', 'fd1', null, 'KK1');
INSERT INTO `tb_blobs` VALUES ('2', null, '2', null);
INSERT INTO `tb_blobs` VALUES ('3', 'fd3', '3', 'KK3');
-- ---------------------------- -- ----------------------------
-- Table structure for tb_keys -- Table structure for tb_keys
......
<?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.UpsertPlugin">
<property name="allowBatchUpsert" value="true"/>
</plugin>
<plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>
<!--jdbc的数据库连接 -->
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}"/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator targetPackage="" targetProject=""/>
<!--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"/>
<table tableName="tb_keys"/>
<table tableName="tb_single_blob"/>
<table tableName="tb_blobs"/>
<table tableName="tb_with_inc_id">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="tb_blobs_with_inc_id">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
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