Compare commits

..
7 Commits
6 changed files with 202 additions and 193 deletions
+32 -2
View File
@@ -24,10 +24,40 @@ Maven引用:
<dependency>
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
</dependency>
```
---------------------------------------
MyBatis Generator 参考配置(插件依赖应该配置在mybatis-generator-maven-plugin插件依赖中[[issues#6]](https://github.com/itfsw/mybatis-generator-plugin/issues/6))
```xml
<!-- mybatis-generator 自动代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!-- 配置文件 -->
<configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
<!-- 允许移动和修改 -->
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<!-- jdbc 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.driver.version}</version>
</dependency>
<dependency>
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>${mybatis.generator.plugin.version}</version>
</dependency>
</dependencies>
</plugin>
```
---------------------------------------
### 1. 查询单条数据插件
对应表Mapper接口增加了方法
插件:
@@ -633,7 +663,7 @@ public class Test {
}
}
```
### 12. 自定义注释插件
### 13. 自定义注释插件
Mybatis Generator是原生支持自定义注释的(commentGenerator配置type属性),但使用比较麻烦需要自己实现CommentGenerator接口并打包配置赖等等。
该插件借助freemarker极佳的灵活性实现了自定义注释的快速配置。
>warning: 下方提供了一个参考模板,需要注意${mgb}的输出,因为Mybatis Generator就是通过该字符串判断是否为自身生成代码进行覆盖重写。
+2 -2
View File
@@ -3,7 +3,7 @@
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
@@ -187,7 +187,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.0.11</tag>
<tag>V1.0.12</tag>
</scm>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++distribution++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
@@ -232,37 +232,42 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
XmlElement whenEle = new XmlElement("when");
whenEle.addAttribute(new Attribute("test", prefix + "isSelective()"));
for (Element ele : element.getElements()) {
// if的text节点
XmlElement xmlElement = (XmlElement) ele;
TextElement textElement = (TextElement) xmlElement.getElements().get(0);
// 对于字符串主键,是没有if判断节点的
if (ele instanceof XmlElement){
// if的text节点
XmlElement xmlElement = (XmlElement) ele;
TextElement textElement = (TextElement) xmlElement.getElements().get(0);
// 找出field 名称
String text = textElement.getContent().trim();
String field = "";
if (text.matches(".*\\s*=\\s*#\\{.*\\},?")) {
Pattern pattern = Pattern.compile("(.*?)\\s*=\\s*#\\{.*},?");
Matcher matcher = pattern.matcher(text);
if (matcher.find()){
field = matcher.group(1);
}
} else if (text.matches("#\\{.*\\},?")) {
Pattern pattern = Pattern.compile("#\\{(.*?),.*\\},?");
Matcher matcher = pattern.matcher(text);
if (matcher.find()){
field = matcher.group(1);
// 找出field 名称
String text = textElement.getContent().trim();
String field = "";
if (text.matches(".*\\s*=\\s*#\\{.*\\},?")) {
Pattern pattern = Pattern.compile("(.*?)\\s*=\\s*#\\{.*},?");
Matcher matcher = pattern.matcher(text);
if (matcher.find()){
field = matcher.group(1);
}
} else if (text.matches("#\\{.*\\},?")) {
Pattern pattern = Pattern.compile("#\\{(.*?),.*\\},?");
Matcher matcher = pattern.matcher(text);
if (matcher.find()){
field = matcher.group(1);
}
} else {
field = text.replaceAll(",", "");
}
XmlElement ifEle = new XmlElement("if");
// bug fixed: 修正使用autoDelimitKeywords过滤关键词造成的field前后加了特殊字符的问题
field = field.replaceAll("`", "").replaceAll("\"", "").replaceAll("'", "");
ifEle.addAttribute(new Attribute("test", prefix + "isSelective(\'" + field + "\')"));
ifEle.addElement(textElement);
whenEle.addElement(ifEle);
} else {
field = text.replaceAll(",", "");
whenEle.addElement(ele);
}
XmlElement ifEle = new XmlElement("if");
// bug fixed: 修正使用autoDelimitKeywords过滤关键词造成的field前后加了特殊字符的问题
field = field.replaceAll("`", "").replaceAll("\"", "").replaceAll("'", "");
ifEle.addAttribute(new Attribute("test", prefix + "isSelective(\'" + field + "\')"));
ifEle.addElement(textElement);
whenEle.addElement(ifEle);
}
// otherwise
@@ -17,7 +17,11 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import com.itfsw.mybatis.generator.plugins.utils.IntrospectedTableTools;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.config.TableConfiguration;
import java.util.List;
/**
* ---------------------------------------------------------------------------
@@ -32,10 +36,26 @@ public class TablePrefixPlugin extends BasePlugin {
public static final String PRE_PREFIX = "prefix"; // 前缀 property
private String prefix; // 前缀
/**
* {@inheritDoc}
*/
@Override
public boolean validate(List<String> warnings) {
// 如果table配置了domainObjectName或者mapperName就不要再启动该插件了
for (TableConfiguration tableConfiguration : context.getTableConfigurations()) {
if (tableConfiguration.getDomainObjectName() != null || tableConfiguration.getMapperName() != null) {
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件请不要配合table的domainObjectName或者mapperName一起使用!");
return false;
}
}
return super.validate(warnings);
}
/**
* 初始化阶段
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param introspectedTable
* @return
*/
@@ -44,61 +64,19 @@ public class TablePrefixPlugin extends BasePlugin {
// 1. 首先获取全局配置
this.prefix = getProperties().getProperty(PRE_PREFIX);
// 2. 获取每个table 具体的
if (introspectedTable.getTableConfigurationProperty(PRE_PREFIX) != null){
if (introspectedTable.getTableConfigurationProperty(PRE_PREFIX) != null) {
this.prefix = introspectedTable.getTableConfigurationProperty(PRE_PREFIX);
}
// 3. 判断是否配置了前缀
if (this.prefix != null){
// 3.1. 为Model增加前缀
if (introspectedTable.getBaseRecordType() != null){
introspectedTable.setBaseRecordType(this.renameJavaType(introspectedTable.getBaseRecordType()));
}
// 3.2. 为ModelKey添加前缀
if (introspectedTable.getPrimaryKeyType() != null){
introspectedTable.setPrimaryKeyType(this.renameJavaType(introspectedTable.getPrimaryKeyType()));
}
// 3.3. WithBLOBs Model 添加前缀
if (introspectedTable.getRecordWithBLOBsType() != null){
introspectedTable.setRecordWithBLOBsType(this.renameJavaType(introspectedTable.getRecordWithBLOBsType()));
}
// 3.4. mapper 添加前缀
if (introspectedTable.getMyBatis3JavaMapperType() != null){
introspectedTable.setMyBatis3JavaMapperType(this.renameJavaType(introspectedTable.getMyBatis3JavaMapperType()));
}
// 3.5. example 添加前缀
if (introspectedTable.getExampleType() != null){
introspectedTable.setExampleType(this.renameJavaType(introspectedTable.getExampleType()));
}
// 3.6. Dao 添加前缀
if (introspectedTable.getDAOInterfaceType() != null){
introspectedTable.setDAOInterfaceType(this.renameJavaType(introspectedTable.getDAOInterfaceType()));
}
// 3.7. DAOImpl 添加前缀
if (introspectedTable.getDAOImplementationType() != null){
introspectedTable.setDAOImplementationType(this.renameJavaType(introspectedTable.getDAOImplementationType()));
}
// 3.8. 修正xml文件前缀
if (introspectedTable.getMyBatis3XmlMapperFileName() != null){
introspectedTable.setMyBatis3XmlMapperFileName(this.prefix + introspectedTable.getMyBatis3XmlMapperFileName());
// !!! TableRenamePlugin 插件的 tableOverride 优先级最高
if (this.prefix != null && introspectedTable.getTableConfigurationProperty(TableRenamePlugin.PRE_TABLE_OVERRIDE) == null) {
String domainObjectName = introspectedTable.getFullyQualifiedTable().getDomainObjectName();
domainObjectName = prefix + domainObjectName;
try {
IntrospectedTableTools.setDomainObjectName(introspectedTable, getContext(), domainObjectName);
} catch (Exception e) {
logger.error("itfsw:插件" + this.getClass().getTypeName() + "使用prefix替换时异常!", e);
}
}
}
/**
* 为类型添加前缀
*
* @param type
* @return
*/
private String renameJavaType(String type){
int lastDot = type.lastIndexOf(".") + 1;
return type.substring(0, lastDot) + this.prefix + type.substring(lastDot);
}
}
@@ -17,7 +17,10 @@
package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
import com.itfsw.mybatis.generator.plugins.utils.IntrospectedTableTools;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.config.TableConfiguration;
import org.mybatis.generator.internal.util.JavaBeansUtil;
import java.util.List;
import java.util.regex.Matcher;
@@ -49,6 +52,14 @@ public class TableRenamePlugin extends BasePlugin {
return false;
}
// 如果table配置了domainObjectName或者mapperName就不要再启动该插件了
for (TableConfiguration tableConfiguration: context.getTableConfigurations()) {
if (tableConfiguration.getDomainObjectName() != null || tableConfiguration.getMapperName() != null){
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件请不要配合table的domainObjectName或者mapperName一起使用!");
return false;
}
}
return super.validate(warnings);
}
@@ -63,122 +74,26 @@ public class TableRenamePlugin extends BasePlugin {
// 1. 获取表单独配置
if (introspectedTable.getTableConfigurationProperty(PRE_TABLE_OVERRIDE) != null) {
String override = introspectedTable.getTableConfigurationProperty(PRE_TABLE_OVERRIDE);
// 3.1. Model修正名称
if (introspectedTable.getBaseRecordType() != null){
introspectedTable.setBaseRecordType(this.renameJavaType(introspectedTable.getBaseRecordType(), override, ""));
}
// 3.2. ModelKey修正名称
if (introspectedTable.getPrimaryKeyType() != null){
introspectedTable.setPrimaryKeyType(this.renameJavaType(introspectedTable.getPrimaryKeyType(), override, "Key"));
}
// 3.3. WithBLOBs Model 修正名称
if (introspectedTable.getRecordWithBLOBsType() != null){
introspectedTable.setRecordWithBLOBsType(this.renameJavaType(introspectedTable.getRecordWithBLOBsType(), override, "WithBLOBs"));
}
// 3.4. mapper 修正名称
if (introspectedTable.getMyBatis3JavaMapperType() != null){
introspectedTable.setMyBatis3JavaMapperType(this.renameJavaType(introspectedTable.getMyBatis3JavaMapperType(), override, "Mapper"));
}
// 3.5. example 修正名称
if (introspectedTable.getExampleType() != null){
introspectedTable.setExampleType(this.renameJavaType(introspectedTable.getExampleType(), override, "Example"));
}
// 3.6. Dao 添加前缀
if (introspectedTable.getDAOInterfaceType() != null){
introspectedTable.setDAOInterfaceType(this.renameJavaType(introspectedTable.getDAOInterfaceType(), override, "Dao"));
}
// 3.7. DAOImpl 添加前缀
if (introspectedTable.getDAOImplementationType() != null){
introspectedTable.setDAOImplementationType(this.renameJavaType(introspectedTable.getDAOImplementationType(), override, "DAOImpl"));
}
// 3.8. 修正xml文件名称
if (introspectedTable.getMyBatis3XmlMapperFileName() != null){
introspectedTable.setMyBatis3XmlMapperFileName(override + "Mapper.xml");
try {
IntrospectedTableTools.setDomainObjectName(introspectedTable, getContext(), override);
} catch (Exception e) {
logger.error("itfsw:插件" + this.getClass().getTypeName() + "使用tableOverride替换时异常!", e);
}
} else if (getProperties().getProperty(PRE_SEARCH_STRING) != null){
String searchString = getProperties().getProperty(PRE_SEARCH_STRING);
String replaceString = getProperties().getProperty(PRE_REPLACE_STRING);
// 3.1. Model修正名称
if (introspectedTable.getBaseRecordType() != null){
introspectedTable.setBaseRecordType(this.renameJavaType(introspectedTable.getBaseRecordType(), searchString, replaceString, ""));
}
// 3.2. ModelKey修正名称
if (introspectedTable.getPrimaryKeyType() != null){
introspectedTable.setPrimaryKeyType(this.renameJavaType(introspectedTable.getPrimaryKeyType(), searchString, replaceString, "Key"));
}
// 3.3. WithBLOBs Model 修正名称
if (introspectedTable.getRecordWithBLOBsType() != null){
introspectedTable.setRecordWithBLOBsType(this.renameJavaType(introspectedTable.getRecordWithBLOBsType(), searchString, replaceString, "WithBLOBs"));
}
// 3.4. mapper 修正名称
if (introspectedTable.getMyBatis3JavaMapperType() != null){
introspectedTable.setMyBatis3JavaMapperType(this.renameJavaType(introspectedTable.getMyBatis3JavaMapperType(), searchString, replaceString, "Mapper"));
}
// 3.5. example 修正名称
if (introspectedTable.getExampleType() != null){
introspectedTable.setExampleType(this.renameJavaType(introspectedTable.getExampleType(), searchString, replaceString, "Example"));
}
// 3.6. Dao 添加前缀
if (introspectedTable.getDAOInterfaceType() != null){
introspectedTable.setDAOInterfaceType(this.renameJavaType(introspectedTable.getDAOInterfaceType(), searchString, replaceString, "Dao"));
}
// 3.7. DAOImpl 添加前缀
if (introspectedTable.getDAOImplementationType() != null){
introspectedTable.setDAOImplementationType(this.renameJavaType(introspectedTable.getDAOImplementationType(), searchString, replaceString, "DAOImpl"));
}
// 3.8. 修正xml文件名称
if (introspectedTable.getMyBatis3XmlMapperFileName() != null){
Pattern pattern = Pattern.compile(searchString);
Matcher matcher = pattern.matcher(introspectedTable.getMyBatis3XmlMapperFileName());
String fileName = matcher.replaceAll(replaceString);
introspectedTable.setMyBatis3XmlMapperFileName(fileName);
String domainObjectName = introspectedTable.getFullyQualifiedTable().getDomainObjectName();
Pattern pattern = Pattern.compile(searchString);
Matcher matcher = pattern.matcher(domainObjectName);
domainObjectName = matcher.replaceAll(replaceString);
// 命名规范化
domainObjectName = JavaBeansUtil.getCamelCaseString(domainObjectName, true);
try {
IntrospectedTableTools.setDomainObjectName(introspectedTable, getContext(), domainObjectName);
} catch (Exception e) {
logger.error("itfsw:插件" + this.getClass().getTypeName() + "使用searchString、replaceString替换时异常!", e);
}
}
}
/**
* 重命名类型
*
* @param type
* @param override
* @return
*/
private String renameJavaType(String type, String override, String suffix) {
int lastDot = type.lastIndexOf(".");
return type.substring(0, lastDot) + "." + override + suffix;
}
/**
* 重命名类型
*
* @param type
* @param searchString
* @param replaceString
* @return
*/
private String renameJavaType(String type, String searchString, String replaceString, String suffix) {
int lastDot = type.lastIndexOf(".");
String shortName = type.substring(lastDot + 1, type.length() - 1);
Pattern pattern = Pattern.compile(searchString);
Matcher matcher = pattern.matcher(shortName);
return type.substring(0, lastDot) + "." + matcher.replaceAll(replaceString) + suffix;
}
}
@@ -0,0 +1,81 @@
/*
* 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.utils;
import com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin;
import org.mybatis.generator.api.FullyQualifiedTable;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.JavaModelGeneratorConfiguration;
import org.mybatis.generator.config.PluginConfiguration;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* ---------------------------------------------------------------------------
* IntrospectedTable 的一些拓展增强
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/6/13 13:48
* ---------------------------------------------------------------------------
*/
public class IntrospectedTableTools {
/**
* 设置DomainObjectName和MapperName
*
* @param introspectedTable
* @param context
* @param domainObjectName
*/
public static void setDomainObjectName(IntrospectedTable introspectedTable, Context context, String domainObjectName) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
// 配置信息(没啥用)
introspectedTable.getTableConfiguration().setDomainObjectName(domainObjectName);
// FullyQualifiedTable修正
Field domainObjectNameField = FullyQualifiedTable.class.getDeclaredField("domainObjectName");
domainObjectNameField.setAccessible(true);
domainObjectNameField.set(introspectedTable.getFullyQualifiedTable(), domainObjectName);
// 重新修正introspectedTable属性信息
Method calculateJavaClientAttributes = IntrospectedTable.class.getDeclaredMethod("calculateJavaClientAttributes");
calculateJavaClientAttributes.setAccessible(true);
calculateJavaClientAttributes.invoke(introspectedTable);
Method calculateModelAttributes = IntrospectedTable.class.getDeclaredMethod("calculateModelAttributes");
calculateModelAttributes.setAccessible(true);
calculateModelAttributes.invoke(introspectedTable);
Method calculateXmlAttributes = IntrospectedTable.class.getDeclaredMethod("calculateXmlAttributes");
calculateXmlAttributes.setAccessible(true);
calculateXmlAttributes.invoke(introspectedTable);
// 注意!! 如果配置了ExampleTargetPlugin插件,要修正Example 位置
PluginConfiguration configuration = PluginTools.getPluginConfiguration(ExampleTargetPlugin.class, context);
if (configuration != null && configuration.getProperty(ExampleTargetPlugin.TARGET_PACKAGE_KEY) != null){
String exampleType = introspectedTable.getExampleType();
// 修改包名
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = context.getJavaModelGeneratorConfiguration();
String targetPackage = javaModelGeneratorConfiguration.getTargetPackage();
String newExampleType = exampleType.replace(targetPackage, configuration.getProperty(ExampleTargetPlugin.TARGET_PACKAGE_KEY));
introspectedTable.setExampleType(newExampleType);
}
}
}