ModelColumnPlugin增加excludes方法
This commit is contained in:
@@ -85,6 +85,11 @@
|
||||
<version>2.4.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-beanutils</groupId>
|
||||
<artifactId>commons-beanutils</artifactId>
|
||||
<version>1.9.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++profiles++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.mybatis.generator.internal.util.JavaBeansUtil;
|
||||
*/
|
||||
public class ModelColumnPlugin extends BasePlugin {
|
||||
public static final String ENUM_NAME = "Column"; // 内部Enum名
|
||||
public static final String METHOD_EXCLUDES = "excludes"; // 方法名
|
||||
|
||||
/**
|
||||
* Model Methods 生成
|
||||
@@ -174,6 +175,28 @@ public class ModelColumnPlugin extends BasePlugin {
|
||||
FormatTools.addMethodWithBestPosition(innerEnum, asc);
|
||||
logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + ".Column增加asc()和desc()方法。");
|
||||
|
||||
// excludes
|
||||
topLevelClass.addImportedType("java.util.Arrays");
|
||||
topLevelClass.addImportedType(FullyQualifiedJavaType.getNewArrayListInstance());
|
||||
Method mExcludes = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_EXCLUDES,
|
||||
JavaVisibility.PUBLIC,
|
||||
new FullyQualifiedJavaType(ENUM_NAME + "[]"),
|
||||
new Parameter(innerEnum.getType(), "excludes", true)
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mExcludes, introspectedTable);
|
||||
mExcludes.setStatic(true);
|
||||
JavaElementGeneratorTools.generateMethodBody(
|
||||
mExcludes,
|
||||
"ArrayList<Column> columns = new ArrayList<>(Arrays.asList(Column.values()));",
|
||||
"if (excludes != null && excludes.length > 0) {",
|
||||
"columns.removeAll(new ArrayList<>(Arrays.asList(excludes)));",
|
||||
"}",
|
||||
"return columns.toArray(new Column[]{});"
|
||||
);
|
||||
FormatTools.addMethodWithBestPosition(innerEnum, mExcludes);
|
||||
logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + ".Column增加excludes方法。");
|
||||
|
||||
return innerEnum;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,16 +16,15 @@
|
||||
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.tools.AbstractShellCallback;
|
||||
import com.itfsw.mybatis.generator.plugins.tools.DBHelper;
|
||||
import com.itfsw.mybatis.generator.plugins.tools.MyBatisGeneratorTool;
|
||||
import com.itfsw.mybatis.generator.plugins.tools.ObjectUtil;
|
||||
import com.itfsw.mybatis.generator.plugins.tools.*;
|
||||
import org.apache.commons.beanutils.MethodUtils;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
@@ -41,7 +40,7 @@ public class ModelColumnPluginTest {
|
||||
* 初始化数据库
|
||||
*/
|
||||
@BeforeClass
|
||||
public static void init() throws SQLException, IOException, ClassNotFoundException {
|
||||
public static void init() throws SQLException, IOException {
|
||||
DBHelper.createDB("scripts/ModelColumnPlugin/init.sql");
|
||||
}
|
||||
|
||||
@@ -49,7 +48,7 @@ public class ModelColumnPluginTest {
|
||||
* 测试生成的model
|
||||
*/
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
public void testModel() throws Exception {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ModelColumnPlugin/mybatis-generator.xml");
|
||||
tool.generate(new AbstractShellCallback() {
|
||||
@Override
|
||||
@@ -78,6 +77,63 @@ public class ModelColumnPluginTest {
|
||||
Assert.assertEquals(TbKeysColumnKey1.invoke("value"), "key_1");
|
||||
ObjectUtil TbKeysColumnField1 = new ObjectUtil(loader, packagz + ".TbKeys$Column#field1");
|
||||
Assert.assertEquals(TbKeysColumnField1.invoke("value"), "field_1");
|
||||
|
||||
// 5. excludes 方法
|
||||
// 不排除
|
||||
Object columns = Array.newInstance(TbColumnField1.getCls(), 0);
|
||||
Object[] result = (Object[])(MethodUtils.invokeStaticMethod(Class.forName(packagz + ".Tb$Column"), "excludes", columns));
|
||||
Assert.assertEquals(result.length, 5);
|
||||
// 排除两个
|
||||
columns = Array.newInstance(TbColumnField1.getCls(), 2);
|
||||
Array.set(columns, 0, TbColumnField1.getObject());
|
||||
Array.set(columns, 1, TbColumnTsIncF2.getObject());
|
||||
result = (Object[])(MethodUtils.invokeStaticMethod(Class.forName(packagz + ".Tb$Column"), "excludes", columns));
|
||||
Assert.assertEquals(result.length, 3);
|
||||
for (Object obj : result){
|
||||
ObjectUtil column = new ObjectUtil(obj);
|
||||
if (column.invoke("value").equals("field_1") || column.invoke("value").equals("inc_f2")){
|
||||
Assert.assertTrue(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试excludes
|
||||
*/
|
||||
@Test
|
||||
public void testExcludes() throws Exception {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ModelColumnPlugin/mybatis-generator-with-SeleciveEnhancedPlugin.xml");
|
||||
tool.generate(new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
|
||||
|
||||
ObjectUtil tb = new ObjectUtil(loader, packagz + ".Tb");
|
||||
tb.set("id", 121L);
|
||||
tb.set("incF3", 10L);
|
||||
tb.set("tsIncF2", 5L);
|
||||
// selective
|
||||
ObjectUtil TbColumnId = new ObjectUtil(loader, packagz + ".Tb$Column#id");
|
||||
ObjectUtil TbColumnField1 = new ObjectUtil(loader, packagz + ".Tb$Column#field1");
|
||||
ObjectUtil TbColumnTsIncF2 = new ObjectUtil(loader, packagz + ".Tb$Column#tsIncF2");
|
||||
Object columns = Array.newInstance(TbColumnField1.getCls(), 3);
|
||||
Array.set(columns, 0, TbColumnId.getObject());
|
||||
Array.set(columns, 1, TbColumnField1.getObject());
|
||||
Array.set(columns, 2, TbColumnTsIncF2.getObject());
|
||||
|
||||
// sql(指定列)
|
||||
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "insertSelective", tb.getObject(), columns);
|
||||
Assert.assertEquals(sql, "insert into tb ( id , field_1 , inc_f2 ) values ( 121 , 'null' , 5 )");
|
||||
|
||||
// sql(排除列)
|
||||
columns = MethodUtils.invokeStaticMethod(Class.forName(packagz + ".Tb$Column"), "excludes", columns);
|
||||
sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "insertSelective", tb.getObject(), columns);
|
||||
Assert.assertEquals(sql, "insert into tb ( inc_f1 , inc_f3 ) values ( null , 10 )");
|
||||
|
||||
Object result = tbMapper.invoke("insertSelective", tb.getObject(), columns);
|
||||
Assert.assertEquals(result, 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -20,11 +20,11 @@ SET FOREIGN_KEY_CHECKS=0;
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `tb`;
|
||||
CREATE TABLE `tb` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
|
||||
`field_1` varchar(255) DEFAULT NULL COMMENT '注释2',
|
||||
`inc_f1` bigint(20) NOT NULL DEFAULT '0',
|
||||
`inc_f2` bigint(20) NOT NULL DEFAULT '0',
|
||||
`inc_f3` bigint(20) NOT NULL DEFAULT '0',
|
||||
`id` bigint(20) AUTO_INCREMENT COMMENT '注释1',
|
||||
`field_1` varchar(255) COMMENT '注释2',
|
||||
`inc_f1` bigint(20) 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;
|
||||
|
||||
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
<?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="allowMultiQueries" value="true"/>
|
||||
</plugin>
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.SelectiveEnhancedPlugin"/>
|
||||
<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">
|
||||
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
|
||||
|
||||
<columnOverride column="inc_f2" property="tsIncF2"/>
|
||||
</table>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
Reference in New Issue
Block a user