TableRenamePlugin测试用例
This commit is contained in:
@@ -23,6 +23,8 @@ import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.mybatis.generator.api.GeneratedJavaFile;
|
||||
import org.mybatis.generator.api.MyBatisGenerator;
|
||||
import org.mybatis.generator.exception.InvalidConfigurationException;
|
||||
import org.mybatis.generator.exception.XMLParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
@@ -48,6 +50,16 @@ public class ExampleTargetPluginTest {
|
||||
DBHelper.createDB("scripts/ExampleTargetPlugin/init.sql");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试异常配置
|
||||
*/
|
||||
@Test
|
||||
public void testWarnings() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ExampleTargetPlugin/mybatis-generator-without-target.xml");
|
||||
tool.generate();
|
||||
Assert.assertEquals(tool.getWarnings().get(0), "请配置com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin插件的目标包名(targetPackage)!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalPath() throws Exception {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ExampleTargetPlugin/mybatis-generator-without-plugin.xml");
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.tools.DBHelper;
|
||||
import com.itfsw.mybatis.generator.plugins.tools.MyBatisGeneratorTool;
|
||||
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;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/7/7 9:36
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class TableRenamePluginTest {
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
@BeforeClass
|
||||
public static void init() throws Exception{
|
||||
DBHelper.createDB("scripts/TableRenamePlugin/init.sql");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试提示信息
|
||||
*/
|
||||
@Test
|
||||
public void testWarnings() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
|
||||
// searchString、replaceString 单独配置
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/TableRenamePlugin/mybatis-generator-with-wrong-properties.xml");
|
||||
tool.generate();
|
||||
Assert.assertEquals(tool.getWarnings().get(0), "itfsw:插件com.itfsw.mybatis.generator.plugins.TableRenamePlugin插件的searchString、replaceString属性需配合使用,不能单独存在!");
|
||||
|
||||
// 和官方domainObjectName或者mapperName一起配置
|
||||
tool = MyBatisGeneratorTool.create("scripts/TableRenamePlugin/mybatis-generator-with-domainObject.xml");
|
||||
tool.generate();
|
||||
Assert.assertEquals(tool.getWarnings().get(0), "itfsw:插件com.itfsw.mybatis.generator.plugins.TableRenamePlugin插件请不要配合table的domainObjectName或者mapperName一起使用!");
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -53,11 +54,12 @@ public class DBHelper {
|
||||
|
||||
/**
|
||||
* 创建数据库
|
||||
*
|
||||
* @param resource
|
||||
* @throws Exception
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void createDB(String resource) throws Exception {
|
||||
public static void createDB(String resource) throws ClassNotFoundException, SQLException, IOException {
|
||||
String driver = properties.getProperty("driver");
|
||||
String url = properties.getProperty("url");
|
||||
String username = properties.getProperty("username");
|
||||
|
||||
@@ -27,6 +27,8 @@ import org.mybatis.generator.api.MyBatisGenerator;
|
||||
import org.mybatis.generator.config.Configuration;
|
||||
import org.mybatis.generator.config.Context;
|
||||
import org.mybatis.generator.config.xml.ConfigurationParser;
|
||||
import org.mybatis.generator.exception.InvalidConfigurationException;
|
||||
import org.mybatis.generator.exception.XMLParserException;
|
||||
import org.mybatis.generator.internal.DefaultShellCallback;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
@@ -62,11 +64,10 @@ public class MyBatisGeneratorTool {
|
||||
|
||||
/**
|
||||
* 创建
|
||||
*
|
||||
* @param resource
|
||||
* @return
|
||||
*/
|
||||
public static MyBatisGeneratorTool create(String resource) throws Exception {
|
||||
public static MyBatisGeneratorTool create(String resource) throws IOException, XMLParserException {
|
||||
MyBatisGeneratorTool tool = new MyBatisGeneratorTool();
|
||||
tool.warnings = new ArrayList<>();
|
||||
|
||||
@@ -80,14 +81,13 @@ public class MyBatisGeneratorTool {
|
||||
|
||||
/**
|
||||
* 执行MyBatisGenerator
|
||||
*
|
||||
* @param callback
|
||||
* @return
|
||||
* @throws SQLException
|
||||
* @throws IOException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public MyBatisGenerator generate(AbstractShellCallback callback) throws Exception {
|
||||
public MyBatisGenerator generate(AbstractShellCallback callback) throws InvalidConfigurationException, InterruptedException, SQLException, IOException {
|
||||
callback.setTool(this);
|
||||
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
|
||||
myBatisGenerator.generate(null, null, null, true);
|
||||
@@ -96,13 +96,12 @@ public class MyBatisGeneratorTool {
|
||||
|
||||
/**
|
||||
* 执行MyBatisGenerator(不生成文件)
|
||||
*
|
||||
* @return
|
||||
* @throws SQLException
|
||||
* @throws IOException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public MyBatisGenerator generate() throws Exception {
|
||||
public MyBatisGenerator generate() throws InvalidConfigurationException, InterruptedException, SQLException, IOException {
|
||||
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, new DefaultShellCallback(true), warnings);
|
||||
myBatisGenerator.generate(null, null, null, false);
|
||||
return myBatisGenerator;
|
||||
@@ -110,7 +109,6 @@ public class MyBatisGeneratorTool {
|
||||
|
||||
/**
|
||||
* 编译项目并返回 SqlSession
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public SqlSession compile() throws IOException, ClassNotFoundException {
|
||||
@@ -176,7 +174,6 @@ public class MyBatisGeneratorTool {
|
||||
|
||||
/**
|
||||
* 获取指定后缀的文件
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
@@ -198,10 +195,10 @@ public class MyBatisGeneratorTool {
|
||||
/**
|
||||
* 修正配置到指定target
|
||||
*/
|
||||
private void fixConfigToTarget(){
|
||||
private void fixConfigToTarget() {
|
||||
this.targetProject = this.getClass().getClassLoader().getResource("").getPath();
|
||||
this.targetPackage = DAO_PACKAGE + ".s" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
|
||||
for (Context context : config.getContexts()){
|
||||
for (Context context : config.getContexts()) {
|
||||
context.getJavaModelGeneratorConfiguration().setTargetProject(targetProject);
|
||||
context.getJavaModelGeneratorConfiguration().setTargetPackage(targetPackage);
|
||||
context.getSqlMapGeneratorConfiguration().setTargetProject(targetProject);
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
<?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">
|
||||
<!-- 插件 -->
|
||||
<!-- Example 目标包修改插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin"/>
|
||||
|
||||
<!--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">
|
||||
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
|
||||
</table>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
Navicat MySQL Data Transfer
|
||||
|
||||
Source Server : localhost
|
||||
Source Server Version : 50617
|
||||
Source Host : localhost:3306
|
||||
Source Database : mybatis-generator-plugin
|
||||
|
||||
Target Server Type : MYSQL
|
||||
Target Server Version : 50617
|
||||
File Encoding : 65001
|
||||
|
||||
Date: 2017-06-26 17:30:13
|
||||
*/
|
||||
|
||||
SET FOREIGN_KEY_CHECKS=0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for tb
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `tb`;
|
||||
CREATE TABLE `tb` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
|
||||
`field1` varchar(255) DEFAULT NULL COMMENT '注释2',
|
||||
PRIMARY KEY (`id`)
|
||||
);
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of tb
|
||||
-- ----------------------------
|
||||
@@ -0,0 +1,53 @@
|
||||
<?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">
|
||||
<!-- 插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.TableRenamePlugin" />
|
||||
|
||||
<!--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" domainObjectName="Test">
|
||||
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
|
||||
</table>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
<?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">
|
||||
<!-- 插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.TableRenamePlugin">
|
||||
<property name="searchString" value="^T"/>
|
||||
</plugin>
|
||||
|
||||
<!--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">
|
||||
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
|
||||
</table>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
Reference in New Issue
Block a user