Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a500ab1948 | ||
|
|
bc8ae258e8 | ||
|
|
9a99c9b04d | ||
|
|
276d0e2004 | ||
|
|
0a5b7b2211 | ||
|
|
22ec84ba60 | ||
|
|
4077b1ccb5 | ||
|
|
5ae9ecb62c |
@@ -32,7 +32,7 @@ Maven引用:
|
||||
<dependency>
|
||||
<groupId>com.itfsw</groupId>
|
||||
<artifactId>mybatis-generator-plugin</artifactId>
|
||||
<version>1.2.10</version>
|
||||
<version>1.2.11</version>
|
||||
</dependency>
|
||||
```
|
||||
---------------------------------------
|
||||
@@ -42,7 +42,7 @@ MyBatis Generator 参考配置(插件依赖应该配置在mybatis-generator-ma
|
||||
<plugin>
|
||||
<groupId>org.mybatis.generator</groupId>
|
||||
<artifactId>mybatis-generator-maven-plugin</artifactId>
|
||||
<version>1.3.6</version>
|
||||
<version>1.3.7</version>
|
||||
<configuration>
|
||||
<!-- 配置文件 -->
|
||||
<configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
|
||||
@@ -1383,4 +1383,4 @@ public class Test {
|
||||
<property name="@NoArgsConstructor" value="false"/>
|
||||
</plugin>
|
||||
</xml>
|
||||
```
|
||||
```
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
<groupId>com.itfsw</groupId>
|
||||
<artifactId>mybatis-generator-plugin</artifactId>
|
||||
<version>1.2.11</version>
|
||||
<version>1.2.12</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>${project.groupId}:${project.artifactId}</name>
|
||||
@@ -233,7 +233,7 @@
|
||||
<connection>scm:git:https://github.com/itfsw/mybatis-generator-plugin.git</connection>
|
||||
<developerConnection>scm:git:https://github.com/itfsw/mybatis-generator-plugin.git</developerConnection>
|
||||
<url>https://github.com/itfsw/mybatis-generator-plugin</url>
|
||||
<tag>V1.2.11</tag>
|
||||
<tag>V1.2.12</tag>
|
||||
</scm>
|
||||
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++distribution++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
|
||||
@@ -46,6 +46,7 @@ import java.util.List;
|
||||
public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginHook, IIncrementsPluginHook, ILombokPluginHook {
|
||||
public static final String PRO_INCREMENTS_COLUMNS = "incrementsColumns"; // incrementsColumns property
|
||||
public static final String FIELD_INC_MAP = "incrementsColumnsInfoMap"; // 为了防止和用户数据库字段冲突,特殊命名
|
||||
public static final String METHOD_GET_INC_MAP = "incrementsColumnsInfoMap"; // 获取inc map
|
||||
public static final String METHOD_INC_CHECK = "hasIncsForColumn"; // inc 检查方法名称
|
||||
|
||||
private List<IntrospectedColumn> incColumns; // 表启用增量操作的字段
|
||||
@@ -316,7 +317,7 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
|
||||
builderCls.addField(fIncrements);
|
||||
|
||||
// Builder 构造函数增加 自增Map
|
||||
constructor.addBodyLine("this." + IncrementsPlugin.FIELD_INC_MAP + " = builder." + IncrementsPlugin.FIELD_INC_MAP + ";");
|
||||
constructor.addBodyLine("this." + IncrementsPlugin.FIELD_INC_MAP + ".putAll(builder." + IncrementsPlugin.FIELD_INC_MAP + ");");
|
||||
}
|
||||
|
||||
FullyQualifiedJavaType builderType2 = new FullyQualifiedJavaType(topLevelClass.getType().getShortName() + "." + topLevelClass.getType().getShortName() + "Builder");
|
||||
@@ -473,8 +474,8 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
|
||||
));
|
||||
TextElement spec = new TextElement(
|
||||
MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn)
|
||||
+ " ${" + (prefix != null ? prefix : "")
|
||||
+ IncrementsPlugin.FIELD_INC_MAP + "." + MyBatis3FormattingUtilities.escapeStringForMyBatis3(introspectedColumn.getActualColumnName()) + ".value} "
|
||||
+ " ${" + (prefix != null ? prefix : "_parameter.")
|
||||
+ IncrementsPlugin.METHOD_GET_INC_MAP + "()." + MyBatis3FormattingUtilities.escapeStringForMyBatis3(introspectedColumn.getActualColumnName()) + ".value} "
|
||||
+ MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
|
||||
when.addElement(spec);
|
||||
choose.addElement(when);
|
||||
@@ -519,7 +520,7 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
|
||||
sb.append("column.value");
|
||||
|
||||
when.addAttribute(new Attribute("test", sb.toString()));
|
||||
when.addElement(new TextElement("${column.escapedColumnName} = ${column.escapedColumnName} ${record." + FIELD_INC_MAP + "."
|
||||
when.addElement(new TextElement("${column.escapedColumnName} = ${column.escapedColumnName} ${record." + METHOD_GET_INC_MAP + "()."
|
||||
+ introspectedColumn.getActualColumnName()
|
||||
+ ".value} #{record.${column.javaProperty},jdbcType=${column.jdbcType}}"));
|
||||
choose.addElement(when);
|
||||
@@ -561,18 +562,24 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
|
||||
new FullyQualifiedJavaType("Map<String, " + this.getIncEnum(builderCls, introspectedTable).getFullyQualifiedName() + ">"),
|
||||
"new HashMap<String, " + this.getIncEnum(builderCls, introspectedTable).getFullyQualifiedName() + ">()"
|
||||
);
|
||||
fIncrements.setFinal(true);
|
||||
commentGenerator.addFieldComment(fIncrements, introspectedTable);
|
||||
topLevelClass.addField(fIncrements);
|
||||
topLevelClass.addImportedType("java.util.Map");
|
||||
topLevelClass.addImportedType("java.util.HashMap");
|
||||
// getter&setter
|
||||
if (!withLombok) {
|
||||
Method mGetter = JavaElementGeneratorTools.generateGetterMethod(fIncrements);
|
||||
commentGenerator.addGetterComment(mGetter, introspectedTable, null);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mGetter);
|
||||
Method mSetter = JavaElementGeneratorTools.generateSetterMethod(fIncrements);
|
||||
commentGenerator.addSetterComment(mSetter, introspectedTable, null);
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, mSetter);
|
||||
// inc map 获取方法
|
||||
if (withLombok) {
|
||||
topLevelClass.addImportedType(LombokPlugin.EnumLombokAnnotations.ACCESSORS_FLUENT_TRUE.getClazz());
|
||||
fIncrements.addAnnotation(LombokPlugin.EnumLombokAnnotations.ACCESSORS_FLUENT_TRUE.getAnnotation());
|
||||
} else {
|
||||
Method getIncMapMethod = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_GET_INC_MAP,
|
||||
JavaVisibility.PUBLIC,
|
||||
fIncrements.getType()
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(getIncMapMethod, introspectedTable);
|
||||
getIncMapMethod.addBodyLine("return this." + FIELD_INC_MAP + ";");
|
||||
FormatTools.addMethodWithBestPosition(topLevelClass, getIncMapMethod);
|
||||
}
|
||||
// 增加判断方法
|
||||
Method mHasIncsForColumn = JavaElementGeneratorTools.generateMethod(
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* Cloneable
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2018/11/7 15:26
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class ModelCloneablePlugin extends BasePlugin {
|
||||
/**
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
this.supportCloneable(topLevelClass, introspectedTable);
|
||||
return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
this.supportCloneable(topLevelClass, introspectedTable);
|
||||
return super.modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
this.supportCloneable(topLevelClass, introspectedTable);
|
||||
return super.modelRecordWithBLOBsClassGenerated(topLevelClass, introspectedTable);
|
||||
}
|
||||
|
||||
/**
|
||||
* 支持Cloneable
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
*/
|
||||
private void supportCloneable(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
// implement
|
||||
topLevelClass.addSuperInterface(new FullyQualifiedJavaType("java.lang.Cloneable"));
|
||||
// clone
|
||||
Method cloneMethod = JavaElementGeneratorTools.generateMethod(
|
||||
"clone",
|
||||
JavaVisibility.PUBLIC,
|
||||
topLevelClass.getType()
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(cloneMethod, introspectedTable);
|
||||
cloneMethod.addAnnotation("@Override");
|
||||
cloneMethod.addException(new FullyQualifiedJavaType("java.lang.CloneNotSupportedException"));
|
||||
cloneMethod.addBodyLine("return (" + topLevelClass.getType().getShortName() + ") super.clone();");
|
||||
topLevelClass.addMethod(cloneMethod);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
*
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2018/11/7 15:43
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class ModelCloneablePluginTest {
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
@BeforeClass
|
||||
public static void init() throws Exception {
|
||||
DBHelper.createDB("scripts/ModelCloneablePlugin/init.sql");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试生成的model
|
||||
*/
|
||||
@Test
|
||||
public void testModel() throws Exception {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ModelCloneablePlugin/mybatis-generator.xml");
|
||||
tool.generate(new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
ObjectUtil tb = new ObjectUtil(loader, packagz + ".Tb");
|
||||
tb.set("id", 100L);
|
||||
tb.set("field1", "ts1");
|
||||
|
||||
ObjectUtil tbClone = new ObjectUtil(tb.invoke("clone"));
|
||||
Assert.assertEquals(tbClone.get("id"), 100L);
|
||||
Assert.assertEquals(tbClone.get("field1"), "ts1");
|
||||
|
||||
tbClone.set("field1", "ts2");
|
||||
Assert.assertEquals(tb.get("field1"), "ts1");
|
||||
Assert.assertEquals(tbClone.get("field1"), "ts2");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
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-03 17:34:11
|
||||
*/
|
||||
|
||||
SET FOREIGN_KEY_CHECKS=0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for tb
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `tb`;
|
||||
CREATE TABLE `tb` (
|
||||
`id` bigint(20) NOT NULL COMMENT '注释1',
|
||||
`key1` varchar(255) NOT NULL,
|
||||
`field1` varchar(255) DEFAULT NULL COMMENT '注释2',
|
||||
`field2` int(11) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`, `key1`)
|
||||
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
|
||||
@@ -0,0 +1,45 @@
|
||||
<?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.ModelCloneablePlugin"/>
|
||||
|
||||
<!--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"/>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
Reference in New Issue
Block a user