From f0c8ada3d857aa516f1ff0c44847da2f427ded23 Mon Sep 17 00:00:00 2001 From: hewei Date: Fri, 28 Jul 2017 15:14:39 +0800 Subject: [PATCH] =?UTF-8?q?ModelBuilderPlugin=20=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/ModelBuilderPluginTest.java | 95 +++++++++++++++++++ .../scripts/ModelBuilderPlugin/init.sql | 72 ++++++++++++++ .../ModelBuilderPlugin/mybatis-generator.xml | 53 +++++++++++ 3 files changed, 220 insertions(+) create mode 100644 src/test/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPluginTest.java create mode 100644 src/test/resources/scripts/ModelBuilderPlugin/init.sql create mode 100644 src/test/resources/scripts/ModelBuilderPlugin/mybatis-generator.xml diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPluginTest.java new file mode 100644 index 0000000..9833a1b --- /dev/null +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPluginTest.java @@ -0,0 +1,95 @@ +/* + * 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.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 org.apache.ibatis.session.SqlSession; +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/28 14:36 + * --------------------------------------------------------------------------- + */ +public class ModelBuilderPluginTest { + /** + * 初始化数据库 + */ + @BeforeClass + public static void init() throws SQLException, IOException, ClassNotFoundException { + DBHelper.createDB("scripts/ModelBuilderPlugin/init.sql"); + } + + /** + * 测试生成的model + */ + @Test + public void test() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException { + MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ModelBuilderPlugin/mybatis-generator.xml"); + tool.generate(new AbstractShellCallback() { + @Override + public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { + // 1. 普通model + ObjectUtil tbBuilder = new ObjectUtil(loader, packagz + ".Tb$Builder"); + tbBuilder.invoke("field1", "ts1"); + tbBuilder.invoke("incF1", 100l); + ObjectUtil tb = new ObjectUtil(tbBuilder.invoke("build")); + Assert.assertEquals(tb.invoke("getField1"), "ts1"); + Assert.assertEquals(tb.invoke("getIncF1"), 100l); + + // 2. withBlobs + ObjectUtil tbBlobsBuilder = new ObjectUtil(loader, packagz + ".TbBlobs$Builder"); + tbBlobsBuilder.invoke("field1", "ts1"); + ObjectUtil tbBlobs = new ObjectUtil(tbBlobsBuilder.invoke("build")); + Assert.assertEquals(tbBlobs.invoke("getField1"), "ts1"); + + ObjectUtil tbBlobsWithBLOBsBuilder = new ObjectUtil(loader, packagz + ".TbBlobsWithBLOBs$Builder"); + tbBlobsWithBLOBsBuilder.invoke("field1", "ts1"); + tbBlobsWithBLOBsBuilder.invoke("field2", "ts2"); + ObjectUtil tbBlobsWithBLOBs = new ObjectUtil(tbBlobsWithBLOBsBuilder.invoke("build")); + Assert.assertEquals(tbBlobsWithBLOBs.invoke("getField1"), "ts1"); + Assert.assertEquals(tbBlobsWithBLOBs.invoke("getField2"), "ts2"); + + // 3. key + ObjectUtil tbKeysKeyBuilder = new ObjectUtil(loader, packagz + ".TbKeysKey$Builder"); + tbKeysKeyBuilder.invoke("key1", 60l); + ObjectUtil tbKeysKey = new ObjectUtil(tbKeysKeyBuilder.invoke("build")); + Assert.assertEquals(tbKeysKey.invoke("getKey1"), 60l); + + ObjectUtil tbKeysBuilder = new ObjectUtil(loader, packagz + ".TbKeys$Builder"); + tbKeysBuilder.invoke("key1", 50l); + tbKeysBuilder.invoke("field1", "ts2"); + ObjectUtil tbKeys = new ObjectUtil(tbKeysBuilder.invoke("build")); + Assert.assertEquals(tbKeys.invoke("getKey1"), 50l); + Assert.assertEquals(tbKeys.invoke("getField1"), "ts2"); + } + }); + } +} diff --git a/src/test/resources/scripts/ModelBuilderPlugin/init.sql b/src/test/resources/scripts/ModelBuilderPlugin/init.sql new file mode 100644 index 0000000..b736371 --- /dev/null +++ b/src/test/resources/scripts/ModelBuilderPlugin/init.sql @@ -0,0 +1,72 @@ +/* +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-07-05 17:21:41 +*/ + +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', + `inc_f1` bigint(20) NOT NULL DEFAULT '0', + `inc_f2` bigint(20) NOT NULL DEFAULT '0', + `inc_f3` bigint(20) NOT NULL DEFAULT '0', + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of tb +-- ---------------------------- + +-- ---------------------------- +-- Table structure for tb_blobs +-- ---------------------------- +DROP TABLE IF EXISTS `tb_blobs`; +CREATE TABLE `tb_blobs` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1', + `field1` varchar(255) DEFAULT NULL, + `field2` longtext COMMENT '注释2', + `field3` longtext, + `inc_f1` bigint(20) NOT NULL DEFAULT '0', + `inc_f2` bigint(20) NOT NULL DEFAULT '0', + `inc_f3` bigint(20) NOT NULL DEFAULT '0', + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of tb_blobs +-- ---------------------------- + +-- ---------------------------- +-- Table structure for tb_keys +-- ---------------------------- +DROP TABLE IF EXISTS `tb_keys`; +CREATE TABLE `tb_keys` ( + `key1` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1', + `key2` varchar(255) NOT NULL, + `field1` varchar(255) DEFAULT NULL COMMENT '注释2', + `field2` int(11) DEFAULT NULL, + `inc_f1` bigint(20) NOT NULL DEFAULT '0', + `inc_f2` bigint(20) NOT NULL DEFAULT '0', + `inc_f3` bigint(20) NOT NULL DEFAULT '0', + PRIMARY KEY (`key1`,`key2`) +) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of tb_keys +-- ---------------------------- + diff --git a/src/test/resources/scripts/ModelBuilderPlugin/mybatis-generator.xml b/src/test/resources/scripts/ModelBuilderPlugin/mybatis-generator.xml new file mode 100644 index 0000000..f8a83b3 --- /dev/null +++ b/src/test/resources/scripts/ModelBuilderPlugin/mybatis-generator.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + +
+
+
\ No newline at end of file