Skip to content

Commit 9a99c9b

Browse files
author
hewei
committed
新增ModelCloneablePlugin插件和测试用例
1 parent 276d0e2 commit 9a99c9b

File tree

4 files changed

+231
-0
lines changed

4 files changed

+231
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2018.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.itfsw.mybatis.generator.plugins;
18+
19+
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
20+
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
21+
import org.mybatis.generator.api.IntrospectedTable;
22+
import org.mybatis.generator.api.dom.java.*;
23+
24+
/**
25+
* ---------------------------------------------------------------------------
26+
* Cloneable
27+
* ---------------------------------------------------------------------------
28+
* @author: hewei
29+
* @time:2018/11/7 15:26
30+
* ---------------------------------------------------------------------------
31+
*/
32+
public class ModelCloneablePlugin extends BasePlugin {
33+
/**
34+
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
35+
* @param topLevelClass
36+
* @param introspectedTable
37+
* @return
38+
*/
39+
@Override
40+
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
41+
this.supportCloneable(topLevelClass, introspectedTable);
42+
return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
43+
}
44+
45+
/**
46+
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
47+
* @param topLevelClass
48+
* @param introspectedTable
49+
* @return
50+
*/
51+
@Override
52+
public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
53+
this.supportCloneable(topLevelClass, introspectedTable);
54+
return super.modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
55+
}
56+
57+
/**
58+
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
59+
* @param topLevelClass
60+
* @param introspectedTable
61+
* @return
62+
*/
63+
@Override
64+
public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
65+
this.supportCloneable(topLevelClass, introspectedTable);
66+
return super.modelRecordWithBLOBsClassGenerated(topLevelClass, introspectedTable);
67+
}
68+
69+
/**
70+
* 支持Cloneable
71+
* @param topLevelClass
72+
* @param introspectedTable
73+
*/
74+
private void supportCloneable(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
75+
// implement
76+
topLevelClass.addSuperInterface(new FullyQualifiedJavaType("java.lang.Cloneable"));
77+
// clone
78+
Method cloneMethod = JavaElementGeneratorTools.generateMethod(
79+
"clone",
80+
JavaVisibility.PUBLIC,
81+
topLevelClass.getType()
82+
);
83+
commentGenerator.addGeneralMethodComment(cloneMethod, introspectedTable);
84+
cloneMethod.addAnnotation("@Override");
85+
cloneMethod.addException(new FullyQualifiedJavaType("java.lang.CloneNotSupportedException"));
86+
cloneMethod.addBodyLine("return (" + topLevelClass.getType().getShortName() + ") super.clone();");
87+
topLevelClass.addMethod(cloneMethod);
88+
}
89+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2018.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.itfsw.mybatis.generator.plugins;
18+
19+
import com.itfsw.mybatis.generator.plugins.tools.AbstractShellCallback;
20+
import com.itfsw.mybatis.generator.plugins.tools.DBHelper;
21+
import com.itfsw.mybatis.generator.plugins.tools.MyBatisGeneratorTool;
22+
import com.itfsw.mybatis.generator.plugins.tools.ObjectUtil;
23+
import org.apache.ibatis.session.SqlSession;
24+
import org.junit.Assert;
25+
import org.junit.BeforeClass;
26+
import org.junit.Test;
27+
28+
/**
29+
* ---------------------------------------------------------------------------
30+
*
31+
* ---------------------------------------------------------------------------
32+
* @author: hewei
33+
* @time:2018/11/7 15:43
34+
* ---------------------------------------------------------------------------
35+
*/
36+
public class ModelCloneablePluginTest {
37+
/**
38+
* 初始化
39+
*/
40+
@BeforeClass
41+
public static void init() throws Exception {
42+
DBHelper.createDB("scripts/ModelCloneablePlugin/init.sql");
43+
}
44+
45+
/**
46+
* 测试生成的model
47+
*/
48+
@Test
49+
public void testModel() throws Exception {
50+
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ModelCloneablePlugin/mybatis-generator.xml");
51+
tool.generate(new AbstractShellCallback() {
52+
@Override
53+
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
54+
ObjectUtil tb = new ObjectUtil(loader, packagz + ".Tb");
55+
tb.set("id", 100L);
56+
tb.set("field1", "ts1");
57+
58+
ObjectUtil tbClone = new ObjectUtil(tb.invoke("clone"));
59+
Assert.assertEquals(tbClone.get("id"), 100L);
60+
Assert.assertEquals(tbClone.get("field1"), "ts1");
61+
62+
tbClone.set("field1", "ts2");
63+
Assert.assertEquals(tb.get("field1"), "ts1");
64+
Assert.assertEquals(tbClone.get("field1"), "ts2");
65+
}
66+
});
67+
}
68+
69+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Navicat MySQL Data Transfer
3+
4+
Source Server : localhost
5+
Source Server Version : 50617
6+
Source Host : localhost:3306
7+
Source Database : mybatis-generator-plugin
8+
9+
Target Server Type : MYSQL
10+
Target Server Version : 50617
11+
File Encoding : 65001
12+
13+
Date: 2017-07-03 17:34:11
14+
*/
15+
16+
SET FOREIGN_KEY_CHECKS=0;
17+
18+
-- ----------------------------
19+
-- Table structure for tb
20+
-- ----------------------------
21+
DROP TABLE IF EXISTS `tb`;
22+
CREATE TABLE `tb` (
23+
`id` bigint(20) NOT NULL COMMENT '注释1',
24+
`key1` varchar(255) NOT NULL,
25+
`field1` varchar(255) DEFAULT NULL COMMENT '注释2',
26+
`field2` int(11) DEFAULT NULL,
27+
PRIMARY KEY (`id`, `key1`)
28+
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2018.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<!DOCTYPE generatorConfiguration
19+
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
20+
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
21+
<generatorConfiguration>
22+
<properties resource="db.properties"/>
23+
<!--导入属性配置 -->
24+
<context id="default" targetRuntime="MyBatis3">
25+
<!-- 插件 -->
26+
<plugin type="com.itfsw.mybatis.generator.plugins.ModelCloneablePlugin"/>
27+
28+
<!--jdbc的数据库连接 -->
29+
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}"/>
30+
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
31+
targetPackage 指定生成的model生成所在的包名
32+
targetProject 指定在该项目下所在的路径 -->
33+
<javaModelGenerator targetPackage="" targetProject=""/>
34+
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
35+
<sqlMapGenerator targetPackage="" targetProject=""/>
36+
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
37+
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
38+
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
39+
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
40+
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
41+
42+
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
43+
<table tableName="tb"/>
44+
</context>
45+
</generatorConfiguration>

0 commit comments

Comments
 (0)