Commit 645a961f authored by hewei's avatar hewei

插件SelectSelectivePlugin测试用例

parent e908f149
......@@ -26,9 +26,11 @@ import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.IOException;
import java.lang.reflect.Array;
......@@ -63,6 +65,7 @@ public class SelectSelectivePluginTest {
*/
@Test
public void testSelectByExampleSelective() throws Exception {
DBHelper.cleanDao();
List<String> warnings = new ArrayList<>();
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(Resources.getResourceAsStream("scripts/SelectSelectivePlugin/mybatis-generator.xml"));
......@@ -78,7 +81,7 @@ public class SelectSelectivePluginTest {
ObjectUtil tbExample = new ObjectUtil(loader, "com.itfsw.mybatis.generator.plugins.dao.model.TbExample");
ObjectUtil criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
criteria.invoke("andIdLessThan", 100);
criteria.invoke("andIdLessThan", 100l);
tbExample.set("orderByClause", "field2 asc");
ObjectUtil columnField1 = new ObjectUtil(loader, "com.itfsw.mybatis.generator.plugins.dao.model.Tb$Column#field1");
......@@ -87,7 +90,7 @@ public class SelectSelectivePluginTest {
Array.set(columns1, 0, columnField1.getObject());
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExampleSelective", tbExample.getObject(), columns1);
Assert.assertEquals(sql, "select field1 from tb order by field2 asc");
Assert.assertEquals(sql, "select field1 from tb WHERE ( id < '100' ) order by field2 asc");
ObjectUtil columnField2 = new ObjectUtil(loader, "com.itfsw.mybatis.generator.plugins.dao.model.Tb$Column#field2");
Object columns2 = Array.newInstance(columnField1.getCls(), 2);
......@@ -95,7 +98,7 @@ public class SelectSelectivePluginTest {
Array.set(columns2, 1, columnField2.getObject());
sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExampleSelective", tbExample.getObject(), columns2);
Assert.assertEquals(sql, "select field1 , field2 from tb order by field2 asc");
Assert.assertEquals(sql, "select field1 , field2 from tb WHERE ( id < '100' ) order by field2 asc");
// 2. 执行sql
......@@ -129,8 +132,129 @@ public class SelectSelectivePluginTest {
myBatisGenerator.generate(null, null, null, true);
}
/**
* 测试生成的方法
* @throws Exception
*/
@Test
public void testSelectByPrimaryKeySelective() throws Exception {
DBHelper.cleanDao();
List<String> warnings = new ArrayList<>();
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(Resources.getResourceAsStream("scripts/SelectSelectivePlugin/mybatis-generator.xml"));
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, new AbstractShellCallback(true) {
@Override
public void reloadProject(ClassLoader loader) {
SqlSession sqlSession = null;
try {
// 1. 测试sql
sqlSession = helper.getSqlSession();
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass("com.itfsw.mybatis.generator.plugins.dao.TbMapper")));
ObjectUtil columnField1 = new ObjectUtil(loader, "com.itfsw.mybatis.generator.plugins.dao.model.TbKeys$Column#field1");
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object columns1 = Array.newInstance(columnField1.getCls(), 1);
Array.set(columns1, 0, columnField1.getObject());
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByPrimaryKeySelective", 1, columns1);
Assert.assertEquals(sql, "select field1 from tb where id = 1");
// 2. 测试xxxKey
ObjectUtil tbKeysMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass("com.itfsw.mybatis.generator.plugins.dao.TbKeysMapper")));
ObjectUtil tbKeysKey = new ObjectUtil(loader, "com.itfsw.mybatis.generator.plugins.dao.model.TbKeysKey");
tbKeysKey.set("key1", 1l);
tbKeysKey.set("key2", "2");
ObjectUtil columnField2 = new ObjectUtil(loader, "com.itfsw.mybatis.generator.plugins.dao.model.TbKeys$Column#field2");
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object columns2 = Array.newInstance(columnField2.getCls(), 1);
Array.set(columns2, 0, columnField2.getObject());
sql = SqlHelper.getFormatMapperSql(tbKeysMapper.getObject(), "selectByPrimaryKeySelective", tbKeysKey.getObject(), columns2);
Assert.assertEquals(sql, "select field2 from tb_keys where key1 = 1 and key2 = '2'");
// 3. 执行sql
Object tbKeys = tbKeysMapper.invokeVarArgs("selectByPrimaryKeySelective", tbKeysKey.getObject(), columns1);
Assert.assertEquals(new ObjectUtil(tbKeys).get("field1"), "fd1");
} catch (Exception e) {
e.printStackTrace();
Assert.assertTrue(false);
} finally {
sqlSession.close();
}
}
}, warnings);
myBatisGenerator.generate(null, null, null, true);
}
/**
* 测试生成的方法
* @throws Exception
*/
@Test
public void testSelectOneByExampleSelective() throws Exception {
// 没有配置SelectOneByExamplePlugin插件时不生成对应方法
DBHelper.cleanDao();
List<String> warnings = new ArrayList<>();
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(Resources.getResourceAsStream("scripts/SelectSelectivePlugin/mybatis-generator.xml"));
DefaultShellCallback shellCallback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
myBatisGenerator.generate(null, null, null, false);
List<GeneratedJavaFile> list = myBatisGenerator.getGeneratedJavaFiles();
for (GeneratedJavaFile file : list) {
if (file.getFileName().equals("TbMapper.java")) {
Assert.assertFalse(file.getFormattedContent().matches(".*selectByExampleSelective.*"));
}
}
// 配置了SelectOneByExamplePlugin
DBHelper.cleanDao();
config = cp.parseConfiguration(Resources.getResourceAsStream("scripts/SelectSelectivePlugin/mybatis-generator-with-SelectOneByExamplePlugin.xml"));
myBatisGenerator = new MyBatisGenerator(config, new AbstractShellCallback(true) {
@Override
public void reloadProject(ClassLoader loader) {
SqlSession sqlSession = null;
try {
// 1. 测试sql
sqlSession = helper.getSqlSession();
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass("com.itfsw.mybatis.generator.plugins.dao.TbMapper")));
ObjectUtil tbExample = new ObjectUtil(loader, "com.itfsw.mybatis.generator.plugins.dao.model.TbExample");
ObjectUtil criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
criteria.invoke("andIdEqualTo", 3l);
tbExample.set("orderByClause", "field2 asc");
ObjectUtil columnField1 = new ObjectUtil(loader, "com.itfsw.mybatis.generator.plugins.dao.model.Tb$Column#field1");
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object columns1 = Array.newInstance(columnField1.getCls(), 1);
Array.set(columns1, 0, columnField1.getObject());
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectOneByExampleSelective", tbExample.getObject(), columns1);
Assert.assertEquals(sql, "select field1 from tb WHERE ( id = '3' ) order by field2 asc limit 1");
// 2. 执行sql
Object result = tbMapper.invokeVarArgs("selectOneByExampleSelective", tbExample.getObject(), columns1);
ObjectUtil tb = new ObjectUtil(result);
Assert.assertEquals(tb.get("field1"), "fd3");
Assert.assertNull(tb.get("field2"));
} catch (Exception e) {
e.printStackTrace();
Assert.assertTrue(false);
} finally {
sqlSession.close();
}
}
}, warnings);
myBatisGenerator.generate(null, null, null, true);
}
@AfterClass
public static void clean() {
// DBHelper.reset();
DBHelper.reset();
}
}
......@@ -10,7 +10,7 @@ Target Server Type : MYSQL
Target Server Version : 50617
File Encoding : 65001
Date: 2017-06-30 11:15:22
Date: 2017-07-03 17:34:11
*/
SET FOREIGN_KEY_CHECKS=0;
......@@ -59,11 +59,14 @@ CREATE TABLE `tb_keys` (
`field1` varchar(255) DEFAULT NULL COMMENT '注释2',
`field2` int(11) DEFAULT NULL,
PRIMARY KEY (`key1`,`key2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tb_keys
-- ----------------------------
INSERT INTO `tb_keys` VALUES ('1', '2', 'fd1', null);
INSERT INTO `tb_keys` VALUES ('2', '3', null, '2');
INSERT INTO `tb_keys` VALUES ('3', '4', 'fd2', '3');
-- ----------------------------
-- Table structure for tb_single_blob
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2017.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!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">
<!-- 插件 -->
<!-- Select Selective插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.SelectSelectivePlugin"/>
<!-- 数据Model属性对应Column获取插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>
<!-- 查询单条数据插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"/>
<!--jdbc的数据库连接 -->
<jdbcConnection driverClass="${db.driver}" connectionURL="${db.url}" userId="${db.username}" password="${db.password}" />
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator targetPackage="com.itfsw.mybatis.generator.plugins.dao.model" targetProject="src/test/java">
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="com.itfsw.mybatis.generator.plugins.dao" targetProject="src/test/java" />
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator targetPackage="com.itfsw.mybatis.generator.plugins.dao" targetProject="src/test/java" type="XMLMAPPER"/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table tableName="tb">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="tb_keys" />
<table tableName="tb_single_blob">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="tb_blobs">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
......@@ -27,8 +27,6 @@
<plugin type="com.itfsw.mybatis.generator.plugins.SelectSelectivePlugin"/>
<!-- 数据Model属性对应Column获取插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>
<!-- 查询单条数据插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"/>
<!--jdbc的数据库连接 -->
<jdbcConnection driverClass="${db.driver}" connectionURL="${db.url}" userId="${db.username}" password="${db.password}" />
......
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