Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56addb28f8 | ||
|
|
0c276b1553 | ||
|
|
46d0b154d9 | ||
|
|
e133a04108 | ||
|
|
a47d4510c5 | ||
|
|
a1bdd5f1cd | ||
|
|
4f14290789 | ||
|
|
2b88b81d49 | ||
|
|
2571f1c507 | ||
|
|
30a3539fc4 | ||
|
|
1f5e57dc51 | ||
|
|
9b500d994a | ||
|
|
8ff3ee9988 | ||
|
|
2f9fb379fb | ||
|
|
1d99a99edd | ||
|
|
6e9a283ad2 | ||
|
|
476b27832d |
@@ -14,6 +14,7 @@
|
||||
* [数据Model属性对应Column获取插件(ModelColumnPlugin)](#8-数据model属性对应column获取插件)
|
||||
* [存在即更新插件(UpsertPlugin)](#9-存在即更新插件)
|
||||
* [Selective选择插入更新增强插件(SelectiveEnhancedPlugin)](#10-selective选择插入更新增强插件)
|
||||
* [Table增加前缀插件(TableSuffixPlugin)](#11-table增加前缀插件)
|
||||
|
||||
---------------------------------------
|
||||
Maven引用:
|
||||
@@ -21,7 +22,7 @@ Maven引用:
|
||||
<dependency>
|
||||
<groupId>com.itfsw</groupId>
|
||||
<artifactId>mybatis-generator-plugin</artifactId>
|
||||
<version>1.0.7</version>
|
||||
<version>1.0.9</version>
|
||||
</dependency>
|
||||
```
|
||||
---------------------------------------
|
||||
@@ -36,13 +37,23 @@ Maven引用:
|
||||
```java
|
||||
public interface TbMapper {
|
||||
/**
|
||||
* 这是Mybatis Generator拓展插件生成的方法(请勿删除).
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table tb
|
||||
*
|
||||
* @mbg.generated
|
||||
* @author hewei
|
||||
* @project https://github.com/itfsw/mybatis-generator-plugin
|
||||
*/
|
||||
Tb selectOneByExample(TbExample example);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table tb
|
||||
*
|
||||
* @mbg.generated
|
||||
* @project https://github.com/itfsw/mybatis-generator-plugin
|
||||
*/
|
||||
// Model WithBLOBs 时才有
|
||||
TbWithBLOBs selectOneByExampleWithBLOBs(TbExample example);
|
||||
}
|
||||
```
|
||||
### 2. MySQL分页插件
|
||||
@@ -56,28 +67,29 @@ public interface TbMapper {
|
||||
```java
|
||||
public class TbExample {
|
||||
/**
|
||||
* 这是Mybatis Generator拓展插件生成的属性(请勿删除).
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database table tb
|
||||
*
|
||||
* @mbg.generated
|
||||
* @author hewei
|
||||
* @project https://github.com/itfsw/mybatis-generator-plugin
|
||||
*/
|
||||
protected Integer offset;
|
||||
|
||||
/**
|
||||
* 这是Mybatis Generator拓展插件生成的属性(请勿删除).
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database table tb
|
||||
*
|
||||
* @mbg.generated
|
||||
* @author hewei
|
||||
* @project https://github.com/itfsw/mybatis-generator-plugin
|
||||
*/
|
||||
protected Integer rows;
|
||||
|
||||
/**
|
||||
* 这是Mybatis Generator拓展插件生成的方法(请勿删除).
|
||||
* This method corresponds to the database table rc_user_token
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table tb
|
||||
*
|
||||
* @mbg.generated
|
||||
* @author hewei
|
||||
* @project https://github.com/itfsw/mybatis-generator-plugin
|
||||
*/
|
||||
public TbExample limit(Integer rows) {
|
||||
this.rows = rows;
|
||||
@@ -85,11 +97,11 @@ public class TbExample {
|
||||
}
|
||||
|
||||
/**
|
||||
* 这是Mybatis Generator拓展插件生成的方法(请勿删除).
|
||||
* This method corresponds to the database table rc_user_token
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table tb
|
||||
*
|
||||
* @mbg.generated
|
||||
* @author hewei
|
||||
* @project https://github.com/itfsw/mybatis-generator-plugin
|
||||
*/
|
||||
public TbExample limit(Integer offset, Integer rows) {
|
||||
this.offset = offset;
|
||||
@@ -98,11 +110,11 @@ public class TbExample {
|
||||
}
|
||||
|
||||
/**
|
||||
* 这是Mybatis Generator拓展插件生成的方法(请勿删除).
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table tb
|
||||
*
|
||||
* @mbg.generated
|
||||
* @author hewei
|
||||
* @project https://github.com/itfsw/mybatis-generator-plugin
|
||||
*/
|
||||
public TbExample page(Integer page, Integer pageSize) {
|
||||
this.offset = page * pageSize;
|
||||
@@ -478,6 +490,14 @@ public class Test {
|
||||
.example()
|
||||
);
|
||||
// upsertByExampleSelective 用法类似
|
||||
|
||||
// 当Model WithBLOBs 存在时上述方法增加对应的 WithBLOBs 方法,举例如下:
|
||||
TbWithBLOBs tb3 = new Tb.Builder()
|
||||
.field1(1)
|
||||
.field2("xx0")
|
||||
.delFlag(Tb.DEL_FLAG_ON)
|
||||
.build();
|
||||
int k6 = this.tbMapper.upsertWithBLOBs(tb);
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -530,4 +550,35 @@ public class Test {
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
### 11. Table增加前缀插件
|
||||
项目中有时会遇到配置多数据源对应多业务的情况,这种情况下可能会出现不同数据源出现重复表名,造成异常冲突。
|
||||
该插件允许为表增加前缀,改变最终生成的Model、Mapper、Example类名以及xml名。
|
||||
|
||||
插件:
|
||||
```xml
|
||||
<!-- Table增加前缀插件 -->
|
||||
<xml>
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.TableSuffixPlugin">
|
||||
<!-- 这里配置的是全局表前缀,当然在table中配置的值会覆盖该全局配置 -->
|
||||
<property name="suffix" value="Cm"/>
|
||||
</plugin>
|
||||
|
||||
<table tableName="tb">
|
||||
<!-- 这里可以单独表配置前缀,覆盖全局配置 -->
|
||||
<property name="suffix" value="Db1"/>
|
||||
</table>
|
||||
</xml>
|
||||
```
|
||||
使用:
|
||||
```java
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
// Tb 表对应的Model、Mapper、Example类都增加了Db1的前缀
|
||||
// Model类名: Tb -> Db1Tb
|
||||
// Mapper类名: TbMapper -> Db1TbMapper
|
||||
// Example类名: TbExample -> Db1TbExample
|
||||
// xml文件名: TbMapper.xml -> Db1TbMapper.xml
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
<groupId>com.itfsw</groupId>
|
||||
<artifactId>mybatis-generator-plugin</artifactId>
|
||||
<version>1.0.7</version>
|
||||
<version>1.0.9</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>${project.groupId}:${project.artifactId}</name>
|
||||
@@ -175,7 +175,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.7</tag>
|
||||
<tag>V1.0.9</tag>
|
||||
</scm>
|
||||
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++distribution++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
|
||||
@@ -1,302 +0,0 @@
|
||||
/*
|
||||
* 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.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
import org.mybatis.generator.api.dom.xml.Attribute;
|
||||
import org.mybatis.generator.api.dom.xml.Document;
|
||||
import org.mybatis.generator.api.dom.xml.TextElement;
|
||||
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||
import org.mybatis.generator.codegen.mybatis3.ListUtilities;
|
||||
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
|
||||
import org.mybatis.generator.config.Context;
|
||||
import org.mybatis.generator.config.PluginConfiguration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* 批量插入插件
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/1/13 9:33
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
@Deprecated
|
||||
public class BatchInsertOldPlugin extends BasePlugin {
|
||||
public static final String METHOD_BATCH_INSERT = "batchInsert"; // 方法名
|
||||
private boolean hasModelBuilderPlugin; // 是否配置了ModelBuilderPlugin插件
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean validate(List<String> warnings) {
|
||||
// 插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键
|
||||
if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false
|
||||
&& "com.microsoft.jdbc.sqlserver.SQLServer".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false
|
||||
&& "com.microsoft.sqlserver.jdbc.SQLServerDriver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false){
|
||||
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键!");
|
||||
return false;
|
||||
}
|
||||
|
||||
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件已过期,不再维护请使用com.itfsw.mybatis.generator.plugins.BatchInsertPlugin区分batchInsert和batchInsertSelective!");
|
||||
|
||||
return super.validate(warnings);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public void initialized(IntrospectedTable introspectedTable) {
|
||||
// 插件使用前提是使用了ModelColumnPlugin插件
|
||||
Context ctx = getContext();
|
||||
// 利用反射获取pluginConfigurations属性
|
||||
try {
|
||||
java.lang.reflect.Field field = Context.class.getDeclaredField("pluginConfigurations");
|
||||
field.setAccessible(true);
|
||||
List<PluginConfiguration> list = (List<PluginConfiguration>) field.get(ctx);
|
||||
// 检查是否配置了ModelColumnPlugin插件
|
||||
for (PluginConfiguration config: list) {
|
||||
if (ModelColumnPlugin.class.getName().equals(config.getConfigurationType())){
|
||||
this.hasModelBuilderPlugin = true;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Java Client Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param interfaze
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
if (hasModelBuilderPlugin){
|
||||
// 方法生成
|
||||
Method method = new Method(METHOD_BATCH_INSERT);
|
||||
// 返回值类型
|
||||
method.setReturnType(FullyQualifiedJavaType.getIntInstance());
|
||||
// 添加参数
|
||||
FullyQualifiedJavaType type = FullyQualifiedJavaType.getNewListInstance();
|
||||
type.addTypeArgument(introspectedTable.getRules().calculateAllFieldsClass());
|
||||
method.addParameter(new Parameter(type, "list", "@Param(\"list\")"));
|
||||
FullyQualifiedJavaType type1 = new FullyQualifiedJavaType(introspectedTable.getRules().calculateAllFieldsClass().getShortName()+"."+ModelColumnPlugin.ENUM_NAME);
|
||||
method.addParameter(new Parameter(type1, "insertColumns", "@Param(\"insertColumns\")", true));
|
||||
// 添加方法说明
|
||||
commentGenerator.addGeneralMethodComment(method, introspectedTable);
|
||||
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(method);
|
||||
logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsert方法。");
|
||||
} else {
|
||||
// 方法生成
|
||||
Method method = new Method(METHOD_BATCH_INSERT);
|
||||
// 方法可见性 interface会忽略
|
||||
// method.setVisibility(JavaVisibility.PUBLIC);
|
||||
// 返回值类型
|
||||
method.setReturnType(FullyQualifiedJavaType.getIntInstance());
|
||||
// 添加参数
|
||||
FullyQualifiedJavaType type = FullyQualifiedJavaType.getNewListInstance();
|
||||
type.addTypeArgument(introspectedTable.getRules().calculateAllFieldsClass());
|
||||
method.addParameter(new Parameter(type, "list", "@Param(\"list\")"));
|
||||
// 添加方法说明
|
||||
commentGenerator.addGeneralMethodComment(method, introspectedTable);
|
||||
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(method);
|
||||
logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsert方法。");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL Map Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param document
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
|
||||
if (hasModelBuilderPlugin){
|
||||
XmlElement element = new XmlElement("insert");
|
||||
element.addAttribute(new Attribute("id", METHOD_BATCH_INSERT));
|
||||
// 参数类型
|
||||
element.addAttribute(new Attribute("parameterType", "map"));
|
||||
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
|
||||
commentGenerator.addComment(element);
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
XmlElementGeneratorTools.useGeneratedKeys(element, introspectedTable);
|
||||
|
||||
// choose 节点
|
||||
XmlElement choose = new XmlElement("choose");
|
||||
// when 节点
|
||||
XmlElement when = new XmlElement("when");
|
||||
when.addAttribute(new Attribute("test", "insertColumns.length != 0"));
|
||||
when.addElement(new TextElement("insert into "+introspectedTable.getFullyQualifiedTableNameAtRuntime()+" ("));
|
||||
|
||||
XmlElement foreachInsertColumns = new XmlElement("foreach");
|
||||
foreachInsertColumns.addAttribute(new Attribute("collection", "insertColumns"));
|
||||
foreachInsertColumns.addAttribute(new Attribute("item", "column"));
|
||||
foreachInsertColumns.addAttribute(new Attribute("separator", ","));
|
||||
foreachInsertColumns.addElement(new TextElement("${column.value}"));
|
||||
|
||||
when.addElement(foreachInsertColumns);
|
||||
|
||||
when.addElement(new TextElement(")"));
|
||||
|
||||
// values
|
||||
when.addElement(new TextElement("values"));
|
||||
|
||||
// foreach values
|
||||
XmlElement foreachValues = new XmlElement("foreach");
|
||||
foreachValues.addAttribute(new Attribute("collection", "list"));
|
||||
foreachValues.addAttribute(new Attribute("item", "item"));
|
||||
foreachValues.addAttribute(new Attribute("separator", ","));
|
||||
|
||||
foreachValues.addElement(new TextElement("("));
|
||||
|
||||
// foreach 所有插入的列,比较是否存在
|
||||
XmlElement foreachInsertColumnsCheck = new XmlElement("foreach");
|
||||
foreachInsertColumnsCheck.addAttribute(new Attribute("collection", "insertColumns"));
|
||||
foreachInsertColumnsCheck.addAttribute(new Attribute("item", "column"));
|
||||
foreachInsertColumnsCheck.addAttribute(new Attribute("separator", ","));
|
||||
|
||||
// 所有表字段
|
||||
List<IntrospectedColumn> columns = ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
|
||||
for (int i = 0; i < columns.size(); i++) {
|
||||
IntrospectedColumn introspectedColumn = columns.get(i);
|
||||
XmlElement check = new XmlElement("if");
|
||||
check.addAttribute(new Attribute("test", "'"+introspectedColumn.getActualColumnName()+"' == column.value"));
|
||||
check.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item.")));
|
||||
|
||||
foreachInsertColumnsCheck.addElement(check);
|
||||
}
|
||||
foreachValues.addElement(foreachInsertColumnsCheck);
|
||||
|
||||
foreachValues.addElement(new TextElement(")"));
|
||||
|
||||
when.addElement(foreachValues);
|
||||
choose.addElement(when);
|
||||
|
||||
// 普通不带insertColumns参数实现
|
||||
XmlElement otherwise = new XmlElement("otherwise");
|
||||
this.addNormalBatchInsertXml(otherwise, introspectedTable);
|
||||
choose.addElement(otherwise);
|
||||
|
||||
element.addElement(choose);
|
||||
if (context.getPlugins().sqlMapInsertElementGenerated(element, introspectedTable)) {
|
||||
document.getRootElement().addElement(element);
|
||||
logger.debug("itfsw(批量插入插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加batchInsert实现方法。");
|
||||
}
|
||||
} else {
|
||||
XmlElement element = new XmlElement("insert"); //$NON-NLS-1$
|
||||
|
||||
element.addAttribute(new Attribute("id", METHOD_BATCH_INSERT)); //$NON-NLS-1$
|
||||
|
||||
// 参数类型
|
||||
element.addAttribute(new Attribute("parameterType", FullyQualifiedJavaType.getNewListInstance().getFullyQualifiedName()));
|
||||
|
||||
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
|
||||
commentGenerator.addComment(element);
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
XmlElementGeneratorTools.useGeneratedKeys(element, introspectedTable);
|
||||
|
||||
// 普通插入语句
|
||||
this.addNormalBatchInsertXml(element, introspectedTable);
|
||||
|
||||
document.getRootElement().addElement(element);
|
||||
logger.debug("itfsw(批量插入插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加batchInsert实现方法。");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通不带insertColumns的插入语句Xml实现
|
||||
*
|
||||
* @param element
|
||||
* @param introspectedTable
|
||||
*/
|
||||
private void addNormalBatchInsertXml(XmlElement element, IntrospectedTable introspectedTable) {
|
||||
|
||||
StringBuilder insertClause = new StringBuilder();
|
||||
StringBuilder valuesClause = new StringBuilder();
|
||||
|
||||
insertClause.append("insert into "); //$NON-NLS-1$
|
||||
insertClause.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
|
||||
insertClause.append(" ("); //$NON-NLS-1$
|
||||
|
||||
valuesClause.append(" ("); //$NON-NLS-1$
|
||||
|
||||
List<String> valuesClauses = new ArrayList<String>();
|
||||
List<IntrospectedColumn> columns = ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
|
||||
for (int i = 0; i < columns.size(); i++) {
|
||||
IntrospectedColumn introspectedColumn = columns.get(i);
|
||||
|
||||
insertClause.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
|
||||
|
||||
// 生成foreach下插入values
|
||||
valuesClause.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item."));
|
||||
if (i + 1 < columns.size()) {
|
||||
insertClause.append(", "); //$NON-NLS-1$
|
||||
valuesClause.append(", "); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
insertClause.append(')');
|
||||
element.addElement(new TextElement(insertClause.toString()));
|
||||
|
||||
valuesClause.append(')');
|
||||
valuesClauses.add(valuesClause.toString());
|
||||
|
||||
|
||||
// 添加foreach节点
|
||||
XmlElement foreachElement = new XmlElement("foreach");
|
||||
foreachElement.addAttribute(new Attribute("collection", "list"));
|
||||
foreachElement.addAttribute(new Attribute("item", "item"));
|
||||
foreachElement.addAttribute(new Attribute("separator", ","));
|
||||
|
||||
for (String clause : valuesClauses) {
|
||||
foreachElement.addElement(new TextElement(clause));
|
||||
}
|
||||
|
||||
// values 构建
|
||||
element.addElement(new TextElement("values"));
|
||||
element.addElement(foreachElement);
|
||||
}
|
||||
}
|
||||
@@ -65,9 +65,6 @@ public class BatchInsertPlugin extends BasePlugin {
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO
|
||||
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件如之前使用1.0.5-的插件可继续使用com.itfsw.mybatis.generator.plugins.BatchInsertOldPlugin或者修改batchInsert(@Param(\"list\") List<Tb> list, @Param(\"insertColumns\") Tb.Column ... selective)调用为batchInsertSelective!");
|
||||
|
||||
return super.validate(warnings);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* 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 java.util.List;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* 增加Criteria Builder方法
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2016/12/28 14:56
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
@Deprecated
|
||||
public class CriteriaBuilderPlugin extends ExampleEnhancedPlugin{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean validate(List<String> warnings) {
|
||||
logger.warn("itfsw:插件"+this.getClass().getTypeName()+"插件已经过时,请使用com.itfsw.mybatis.generator.plugins.ExampleEnhancedPlugin插件替换!");
|
||||
return super.validate(warnings);
|
||||
}
|
||||
}
|
||||
@@ -133,12 +133,13 @@ public class LogicalDeletePlugin extends BasePlugin {
|
||||
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
if (this.logicalDeleteColumn != null){
|
||||
// 1. 逻辑删除ByExample
|
||||
Method mLogicalDeleteByExample = new Method(METHOD_LOGICAL_DELETE_BY_EXAMPLE);
|
||||
// 返回值类型
|
||||
mLogicalDeleteByExample.setReturnType(FullyQualifiedJavaType.getIntInstance());
|
||||
// 添加参数
|
||||
FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
|
||||
mLogicalDeleteByExample.addParameter(new Parameter(type, "example"));
|
||||
Method mLogicalDeleteByExample = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_LOGICAL_DELETE_BY_EXAMPLE,
|
||||
JavaVisibility.DEFAULT,
|
||||
FullyQualifiedJavaType.getIntInstance(),
|
||||
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")
|
||||
);
|
||||
|
||||
// 添加方法说明
|
||||
commentGenerator.addGeneralMethodComment(mLogicalDeleteByExample, introspectedTable);
|
||||
// interface 增加方法
|
||||
|
||||
@@ -45,8 +45,36 @@ public class ModelBuilderPlugin extends BasePlugin {
|
||||
*/
|
||||
@Override
|
||||
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
List<Field> fields = topLevelClass.getFields();
|
||||
// 判断是否有生成Model的WithBLOBs类
|
||||
List<IntrospectedColumn> columns = introspectedTable.getRules().generateRecordWithBLOBsClass() ? introspectedTable.getNonBLOBColumns() : introspectedTable.getAllColumns();
|
||||
InnerClass innerClass = this.generateModelBuilder(topLevelClass, introspectedTable, columns);
|
||||
topLevelClass.addInnerClass(innerClass);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Model Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
InnerClass innerClass = this.generateModelBuilder(topLevelClass, introspectedTable, introspectedTable.getAllColumns());
|
||||
topLevelClass.addInnerClass(innerClass);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成ModelBuilder
|
||||
*
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @param columns
|
||||
* @return
|
||||
*/
|
||||
private InnerClass generateModelBuilder(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, List<IntrospectedColumn> columns){
|
||||
// 生成内部Builder类
|
||||
InnerClass innerClass = new InnerClass(BUILDER_CLASS_NAME);
|
||||
innerClass.setVisibility(JavaVisibility.PUBLIC);
|
||||
@@ -68,8 +96,7 @@ public class ModelBuilderPlugin extends BasePlugin {
|
||||
innerClass.addMethod(constructor);
|
||||
logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加的构造方法。");
|
||||
|
||||
|
||||
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
|
||||
for (IntrospectedColumn introspectedColumn : columns) {
|
||||
Field field = JavaBeansUtil.getJavaBeansField(introspectedColumn, context, introspectedTable);
|
||||
Method setterMethod = JavaBeansUtil.getJavaBeansSetter(introspectedColumn, context, introspectedTable);
|
||||
|
||||
@@ -97,9 +124,9 @@ public class ModelBuilderPlugin extends BasePlugin {
|
||||
build.addBodyLine("return this.obj;");
|
||||
commentGenerator.addGeneralMethodComment(build, introspectedTable);
|
||||
innerClass.addMethod(build);
|
||||
|
||||
logger.debug("itfsw(数据Model链式构建插件):" + topLevelClass.getType().getShortName() + ".Builder增加build方法。");
|
||||
|
||||
topLevelClass.addInnerClass(innerClass);
|
||||
return true;
|
||||
return innerClass;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,8 +22,6 @@ import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
import org.mybatis.generator.internal.util.JavaBeansUtil;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* 数据Model属性对应Column获取插件
|
||||
@@ -44,8 +42,31 @@ public class ModelColumnPlugin extends BasePlugin {
|
||||
*/
|
||||
@Override
|
||||
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
List<Field> fields = topLevelClass.getFields();
|
||||
topLevelClass.addInnerEnum(this.generateColumnEnum(topLevelClass, introspectedTable));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Model Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
topLevelClass.addInnerEnum(this.generateColumnEnum(topLevelClass, introspectedTable));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成Column字段枚举
|
||||
*
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
private InnerEnum generateColumnEnum(TopLevelClass topLevelClass, IntrospectedTable introspectedTable){
|
||||
// 生成内部枚举
|
||||
InnerEnum innerEnum = new InnerEnum(new FullyQualifiedJavaType(ENUM_NAME));
|
||||
innerEnum.setVisibility(JavaVisibility.PUBLIC);
|
||||
@@ -112,7 +133,6 @@ public class ModelColumnPlugin extends BasePlugin {
|
||||
innerEnum.addMethod(asc);
|
||||
logger.debug("itfsw(数据Model属性对应Column获取插件):" + topLevelClass.getType().getShortName() + ".Column增加asc()和desc()方法。");
|
||||
|
||||
topLevelClass.addInnerEnum(innerEnum);
|
||||
return true;
|
||||
return innerEnum;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package com.itfsw.mybatis.generator.plugins;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
import org.mybatis.generator.api.dom.xml.Attribute;
|
||||
@@ -26,6 +27,9 @@ import org.mybatis.generator.api.dom.xml.Document;
|
||||
import org.mybatis.generator.api.dom.xml.TextElement;
|
||||
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
|
||||
|
||||
/**
|
||||
@@ -38,11 +42,79 @@ import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
|
||||
*/
|
||||
public class SelectOneByExamplePlugin extends BasePlugin {
|
||||
public static final String METHOD_SELECT_ONE_BY_EXAMPLE = "selectOneByExample"; // 方法名
|
||||
public static final String METHOD_SELECT_ONE_BY_EXAMPLE_WITH_BLOBS = "selectOneByExampleWithBLOBs"; // 方法名
|
||||
|
||||
@Override
|
||||
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
|
||||
// 有种情况下ModelBaseRecordClass不会生成不包含BLOBs的构造方法,造成selectOneByExample返回结果找不到对应的构造方法异常
|
||||
if (introspectedTable.hasBLOBColumns() && !introspectedTable.getRules().generateRecordWithBLOBsClass()) {
|
||||
// 添加没有BLOBs的构造方法
|
||||
Method method = new Method();
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
method.setConstructor(true);
|
||||
method.setName(topLevelClass.getType().getShortName());
|
||||
commentGenerator.addGeneralMethodComment(method, introspectedTable);
|
||||
|
||||
// 使用没有blobs的字段
|
||||
List<IntrospectedColumn> constructorColumns = introspectedTable.getNonBLOBColumns();
|
||||
|
||||
for (IntrospectedColumn introspectedColumn : constructorColumns) {
|
||||
method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), introspectedColumn.getJavaProperty()));
|
||||
topLevelClass.addImportedType(introspectedColumn.getFullyQualifiedJavaType());
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (introspectedTable.getRules().generatePrimaryKeyClass()) {
|
||||
boolean comma = false;
|
||||
sb.append("super(");
|
||||
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
|
||||
if (comma) {
|
||||
sb.append(", ");
|
||||
} else {
|
||||
comma = true;
|
||||
}
|
||||
sb.append(introspectedColumn.getJavaProperty());
|
||||
}
|
||||
sb.append(");");
|
||||
method.addBodyLine(sb.toString());
|
||||
}
|
||||
|
||||
List<IntrospectedColumn> introspectedColumns;
|
||||
if (!introspectedTable.getRules().generatePrimaryKeyClass() && introspectedTable.hasPrimaryKeyColumns()) {
|
||||
introspectedColumns = introspectedTable.getNonBLOBColumns();
|
||||
} else {
|
||||
introspectedColumns = introspectedTable.getBaseColumns();
|
||||
}
|
||||
|
||||
for (IntrospectedColumn introspectedColumn : introspectedColumns) {
|
||||
sb.setLength(0);
|
||||
sb.append("this.");
|
||||
sb.append(introspectedColumn.getJavaProperty());
|
||||
sb.append(" = ");
|
||||
sb.append(introspectedColumn.getJavaProperty());
|
||||
sb.append(';');
|
||||
method.addBodyLine(sb.toString());
|
||||
}
|
||||
|
||||
// 找到原有构造方法位置,保持构造方法放在一起美观
|
||||
ArrayList<Method> methods = (ArrayList<Method>) topLevelClass.getMethods();
|
||||
int index = 0;
|
||||
for (Method m : methods) {
|
||||
index++;
|
||||
if (m.getName().equals(topLevelClass.getType().getShortName())) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
methods.add(index, method);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Java Client Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param interfaze
|
||||
* @param topLevelClass
|
||||
* @param introspectedTable
|
||||
@@ -50,34 +122,48 @@ public class SelectOneByExamplePlugin extends BasePlugin {
|
||||
*/
|
||||
@Override
|
||||
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
// 方法生成
|
||||
// 方法生成 selectOneByExample
|
||||
Method method = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_SELECT_ONE_BY_EXAMPLE,
|
||||
JavaVisibility.DEFAULT,
|
||||
introspectedTable.getRules().calculateAllFieldsClass(),
|
||||
JavaElementGeneratorTools.getModelTypeWithoutBLOBs(introspectedTable),
|
||||
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(method, introspectedTable);
|
||||
commentGenerator.addGeneralMethodComment(method, introspectedTable);
|
||||
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(method);
|
||||
logger.debug("itfsw(查询单条数据插件):"+interfaze.getType().getShortName()+"增加SelectOneByExample方法。");
|
||||
logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExample方法。");
|
||||
|
||||
// 方法生成 selectOneByExampleWithBLOBs !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准
|
||||
if (introspectedTable.hasBLOBColumns()) {
|
||||
// 方法生成 selectOneByExample
|
||||
Method method1 = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_SELECT_ONE_BY_EXAMPLE_WITH_BLOBS,
|
||||
JavaVisibility.DEFAULT,
|
||||
JavaElementGeneratorTools.getModelTypeWithBLOBs(introspectedTable),
|
||||
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(method1, introspectedTable);
|
||||
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(method1);
|
||||
logger.debug("itfsw(查询单条数据插件):" + interfaze.getType().getShortName() + "增加selectOneByExampleWithBLOBs方法。");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL Map Methods 生成
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param document
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
|
||||
//数据库表名
|
||||
String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();
|
||||
|
||||
// ------------------------------------ selectOneByExample ----------------------------------
|
||||
// 生成查询语句
|
||||
XmlElement selectOneElement = new XmlElement("select");
|
||||
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
|
||||
@@ -85,75 +171,86 @@ commentGenerator.addGeneralMethodComment(method, introspectedTable);
|
||||
|
||||
// 添加ID
|
||||
selectOneElement.addAttribute(new Attribute("id", METHOD_SELECT_ONE_BY_EXAMPLE));
|
||||
// 添加返回类型
|
||||
selectOneElement.addAttribute(new Attribute("resultMap", introspectedTable.getBaseResultMapId()));
|
||||
// 添加参数类型
|
||||
selectOneElement.addAttribute(new Attribute("parameterType", introspectedTable.getExampleType()));
|
||||
selectOneElement.addElement(new TextElement("select"));
|
||||
|
||||
// ----------------------------------------- 表中是否有blob类型字段 ---------------------------------------
|
||||
if (introspectedTable.hasBLOBColumns()){
|
||||
// 添加返回类型
|
||||
selectOneElement.addAttribute(new Attribute("resultMap", introspectedTable.getResultMapWithBLOBsId()));
|
||||
// 添加参数类型
|
||||
selectOneElement.addAttribute(new Attribute("parameterType", introspectedTable.getExampleType()));
|
||||
// 添加查询SQL
|
||||
selectOneElement.addElement(new TextElement("select")); //$NON-NLS-1$
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
|
||||
sb.append('\'');
|
||||
sb.append(introspectedTable.getSelectByExampleQueryId());
|
||||
sb.append("' as QUERYID,"); //$NON-NLS-1$
|
||||
selectOneElement.addElement(new TextElement(sb.toString()));
|
||||
}
|
||||
|
||||
selectOneElement.addElement(XmlElementGeneratorTools.getBaseColumnListElement(introspectedTable));
|
||||
selectOneElement.addElement(new TextElement(",")); //$NON-NLS-1$
|
||||
selectOneElement.addElement(XmlElementGeneratorTools.getBlobColumnListElement(introspectedTable));
|
||||
|
||||
sb.setLength(0);
|
||||
sb.append("from "); //$NON-NLS-1$
|
||||
sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
|
||||
sb.append('\'');
|
||||
sb.append(introspectedTable.getSelectByExampleQueryId());
|
||||
sb.append("' as QUERYID,");
|
||||
selectOneElement.addElement(new TextElement(sb.toString()));
|
||||
selectOneElement.addElement(XmlElementGeneratorTools.getExampleIncludeElement(introspectedTable));
|
||||
|
||||
XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
|
||||
ifElement.addAttribute(new Attribute("test", "orderByClause != null")); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
ifElement.addElement(new TextElement("order by ${orderByClause}")); //$NON-NLS-1$
|
||||
selectOneElement.addElement(ifElement);
|
||||
|
||||
// 只查询一条
|
||||
selectOneElement.addElement(new TextElement("limit 1"));
|
||||
} else {
|
||||
// 添加返回类型
|
||||
selectOneElement.addAttribute(new Attribute("resultMap", introspectedTable.getBaseResultMapId()));
|
||||
// 添加参数类型
|
||||
selectOneElement.addAttribute(new Attribute("parameterType", introspectedTable.getExampleType()));
|
||||
selectOneElement.addElement(new TextElement("select")); //$NON-NLS-1$
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
|
||||
sb.append('\'');
|
||||
sb.append(introspectedTable.getSelectByExampleQueryId());
|
||||
sb.append("' as QUERYID,"); //$NON-NLS-1$
|
||||
selectOneElement.addElement(new TextElement(sb.toString()));
|
||||
}
|
||||
selectOneElement.addElement(XmlElementGeneratorTools.getBaseColumnListElement(introspectedTable));
|
||||
|
||||
sb.setLength(0);
|
||||
sb.append("from "); //$NON-NLS-1$
|
||||
sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
|
||||
selectOneElement.addElement(new TextElement(sb.toString()));
|
||||
selectOneElement.addElement(XmlElementGeneratorTools.getExampleIncludeElement(introspectedTable));
|
||||
|
||||
XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
|
||||
ifElement.addAttribute(new Attribute("test", "orderByClause != null")); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
ifElement.addElement(new TextElement("order by ${orderByClause}")); //$NON-NLS-1$
|
||||
selectOneElement.addElement(ifElement);
|
||||
|
||||
// 只查询一条
|
||||
selectOneElement.addElement(new TextElement("limit 1"));
|
||||
}
|
||||
selectOneElement.addElement(XmlElementGeneratorTools.getBaseColumnListElement(introspectedTable));
|
||||
|
||||
sb.setLength(0);
|
||||
sb.append("from ");
|
||||
sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
|
||||
selectOneElement.addElement(new TextElement(sb.toString()));
|
||||
selectOneElement.addElement(XmlElementGeneratorTools.getExampleIncludeElement(introspectedTable));
|
||||
|
||||
XmlElement ifElement = new XmlElement("if");
|
||||
ifElement.addAttribute(new Attribute("test", "orderByClause != null")); //$NON-NLS-2$
|
||||
ifElement.addElement(new TextElement("order by ${orderByClause}"));
|
||||
selectOneElement.addElement(ifElement);
|
||||
|
||||
// 只查询一条
|
||||
selectOneElement.addElement(new TextElement("limit 1"));
|
||||
// 添加到根节点
|
||||
document.getRootElement().addElement(selectOneElement);
|
||||
logger.debug("itfsw(查询单条数据插件):"+introspectedTable.getMyBatis3XmlMapperFileName()+"增加SelectOneByExample方法("+(introspectedTable.hasBLOBColumns() ? "有" : "无")+"Blob类型))。");
|
||||
logger.debug("itfsw(查询单条数据插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加selectOneByExample方法。");
|
||||
|
||||
// ------------------------------------ selectOneByExampleWithBLOBs ----------------------------------
|
||||
// !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准
|
||||
if (introspectedTable.hasBLOBColumns()) {
|
||||
// 生成查询语句
|
||||
XmlElement selectOneWithBLOBsElement = new XmlElement("select");
|
||||
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
|
||||
commentGenerator.addComment(selectOneWithBLOBsElement);
|
||||
|
||||
// 添加ID
|
||||
selectOneWithBLOBsElement.addAttribute(new Attribute("id", METHOD_SELECT_ONE_BY_EXAMPLE_WITH_BLOBS));
|
||||
// 添加返回类型
|
||||
selectOneWithBLOBsElement.addAttribute(new Attribute("resultMap", introspectedTable.getResultMapWithBLOBsId()));
|
||||
// 添加参数类型
|
||||
selectOneWithBLOBsElement.addAttribute(new Attribute("parameterType", introspectedTable.getExampleType()));
|
||||
// 添加查询SQL
|
||||
selectOneWithBLOBsElement.addElement(new TextElement("select"));
|
||||
|
||||
sb.setLength(0);
|
||||
if (stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
|
||||
sb.append('\'');
|
||||
sb.append(introspectedTable.getSelectByExampleQueryId());
|
||||
sb.append("' as QUERYID,");
|
||||
selectOneWithBLOBsElement.addElement(new TextElement(sb.toString()));
|
||||
}
|
||||
|
||||
selectOneWithBLOBsElement.addElement(XmlElementGeneratorTools.getBaseColumnListElement(introspectedTable));
|
||||
selectOneWithBLOBsElement.addElement(new TextElement(","));
|
||||
selectOneWithBLOBsElement.addElement(XmlElementGeneratorTools.getBlobColumnListElement(introspectedTable));
|
||||
|
||||
sb.setLength(0);
|
||||
sb.append("from ");
|
||||
sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
|
||||
selectOneWithBLOBsElement.addElement(new TextElement(sb.toString()));
|
||||
selectOneWithBLOBsElement.addElement(XmlElementGeneratorTools.getExampleIncludeElement(introspectedTable));
|
||||
|
||||
XmlElement ifElement1 = new XmlElement("if");
|
||||
ifElement1.addAttribute(new Attribute("test", "orderByClause != null")); //$NON-NLS-2$
|
||||
ifElement1.addElement(new TextElement("order by ${orderByClause}"));
|
||||
selectOneWithBLOBsElement.addElement(ifElement1);
|
||||
|
||||
// 只查询一条
|
||||
selectOneWithBLOBsElement.addElement(new TextElement("limit 1"));
|
||||
|
||||
// 添加到根节点
|
||||
document.getRootElement().addElement(selectOneWithBLOBsElement);
|
||||
logger.debug("itfsw(查询单条数据插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加selectOneByExampleWithBLOBs方法。");
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -169,14 +169,32 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
|
||||
List<XmlElement> eles = this.findEle(xmlElement, "trim");
|
||||
this.replaceEle(eles.get(0), "record.");
|
||||
// upsertByExampleSelective的第二个trim比较特殊,需另行处理
|
||||
this.replaceEleForUpsertByExampleSelective(eles.get(1), "record.", introspectedTable);
|
||||
this.replaceEleForUpsertByExampleSelective(eles.get(1), "record.", introspectedTable, !introspectedTable.getRules().generateRecordWithBLOBsClass());
|
||||
|
||||
List<XmlElement> eles1 = this.findEle(xmlElement, "set");
|
||||
for (XmlElement ele : eles1) {
|
||||
this.replaceEle(ele, "record.");
|
||||
}
|
||||
}
|
||||
// ====================================== 6. upsertSelectiveWithBLOBs ======================================
|
||||
if ("upsertSelectiveWithBLOBs".equals(id)) {
|
||||
List<XmlElement> eles = this.findEle(xmlElement, "trim");
|
||||
for (XmlElement ele : eles) {
|
||||
this.replaceEle(ele, "_parameter.");
|
||||
}
|
||||
}
|
||||
// ====================================== 7. upsertByExampleSelectiveWithBLOBs ======================================
|
||||
if ("upsertByExampleSelectiveWithBLOBs".equals(id)) {
|
||||
List<XmlElement> eles = this.findEle(xmlElement, "trim");
|
||||
this.replaceEle(eles.get(0), "record.");
|
||||
// upsertByExampleSelective的第二个trim比较特殊,需另行处理
|
||||
this.replaceEleForUpsertByExampleSelective(eles.get(1), "record.", introspectedTable, true);
|
||||
|
||||
List<XmlElement> eles1 = this.findEle(xmlElement, "set");
|
||||
for (XmlElement ele : eles1) {
|
||||
this.replaceEle(ele, "record.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -238,6 +256,10 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -262,16 +284,17 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
|
||||
* @param element
|
||||
* @param prefix
|
||||
* @param introspectedTable
|
||||
* @param allColumns
|
||||
*/
|
||||
private void replaceEleForUpsertByExampleSelective(XmlElement element, String prefix, IntrospectedTable introspectedTable) {
|
||||
private void replaceEleForUpsertByExampleSelective(XmlElement element, String prefix, IntrospectedTable introspectedTable, boolean allColumns) {
|
||||
// choose
|
||||
XmlElement chooseEle = new XmlElement("choose");
|
||||
// when
|
||||
XmlElement whenEle = new XmlElement("when");
|
||||
whenEle.addAttribute(new Attribute("test", prefix + "isSelective()"));
|
||||
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
|
||||
for (IntrospectedColumn introspectedColumn : (allColumns ? introspectedTable.getAllColumns() : introspectedTable.getNonBLOBColumns())) {
|
||||
XmlElement eleIf = new XmlElement("if");
|
||||
eleIf.addAttribute(new Attribute("test", prefix + "isSelective(\'" + MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + "\')"));
|
||||
eleIf.addAttribute(new Attribute("test", prefix + "isSelective(\'" + introspectedColumn.getActualColumnName() + "\')"));
|
||||
|
||||
eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
|
||||
whenEle.addElement(eleIf);
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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.utils.BasePlugin;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* table前缀,解决多数据源表重名问题插件(为Model、Mapper、Example、xml等增加前缀)
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/5/18 13:54
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class TableSuffixPlugin extends BasePlugin {
|
||||
|
||||
public static final String PRE_SUFFIX = "suffix"; // 前缀 property
|
||||
private String suffix; // 前缀
|
||||
|
||||
/**
|
||||
* 初始化阶段
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public void initialized(IntrospectedTable introspectedTable) {
|
||||
// 1. 首先获取全局配置
|
||||
this.suffix = getProperties().getProperty(PRE_SUFFIX);
|
||||
// 2. 获取每个table 具体的
|
||||
if (introspectedTable.getTableConfigurationProperty(PRE_SUFFIX) != null){
|
||||
this.suffix = introspectedTable.getTableConfigurationProperty(PRE_SUFFIX);
|
||||
}
|
||||
// 3. 判断是否配置了前缀
|
||||
if (this.suffix != 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. 修正xml文件前缀
|
||||
if (introspectedTable.getMyBatis3XmlMapperFileName() != null){
|
||||
introspectedTable.setMyBatis3XmlMapperFileName(this.suffix + introspectedTable.getMyBatis3XmlMapperFileName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 为类型添加前缀
|
||||
*
|
||||
* @param type
|
||||
* @return
|
||||
*/
|
||||
private String renameJavaType(String type){
|
||||
int lastDot = type.lastIndexOf(".") + 1;
|
||||
return type.substring(0, lastDot) + this.suffix + type.substring(lastDot);
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ package com.itfsw.mybatis.generator.plugins;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
import org.mybatis.generator.api.dom.xml.Attribute;
|
||||
@@ -45,6 +46,11 @@ public class UpsertPlugin extends BasePlugin {
|
||||
public static final String METHOD_UPSERT_BY_EXAMPLE = "upsertByExample"; // 方法名
|
||||
public static final String METHOD_UPSERT_BY_EXAMPLE_SELECTIVE = "upsertByExampleSelective"; // 方法名
|
||||
|
||||
public static final String METHOD_UPSERT_WITH_BLOBS = "upsertWithBLOBs"; // 方法名
|
||||
public static final String METHOD_UPSERT_SELECTIVE_WITH_BLOBS = "upsertSelectiveWithBLOBs"; // 方法名
|
||||
public static final String METHOD_UPSERT_BY_EXAMPLE_WITH_BLOBS = "upsertByExampleWithBLOBs"; // 方法名
|
||||
public static final String METHOD_UPSERT_BY_EXAMPLE_SELECTIVE_WITH_BLOBS = "upsertByExampleSelectiveWithBLOBs"; // 方法名
|
||||
|
||||
public static final String PRE_ALLOW_MULTI_QUERIES = "allowMultiQueries"; // property allowMultiQueries
|
||||
private boolean allowMultiQueries = false; // 是否允许多sql提交
|
||||
|
||||
@@ -88,7 +94,7 @@ public class UpsertPlugin extends BasePlugin {
|
||||
METHOD_UPSERT,
|
||||
JavaVisibility.DEFAULT,
|
||||
FullyQualifiedJavaType.getIntInstance(),
|
||||
new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record")
|
||||
new Parameter(JavaElementGeneratorTools.getModelTypeWithoutBLOBs(introspectedTable), "record")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mUpsert, introspectedTable);
|
||||
// interface 增加方法
|
||||
@@ -100,7 +106,7 @@ public class UpsertPlugin extends BasePlugin {
|
||||
METHOD_UPSERT_SELECTIVE,
|
||||
JavaVisibility.DEFAULT,
|
||||
FullyQualifiedJavaType.getIntInstance(),
|
||||
new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record")
|
||||
new Parameter(JavaElementGeneratorTools.getModelTypeWithoutBLOBs(introspectedTable), "record")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mUpsertSelective, introspectedTable);
|
||||
// interface 增加方法
|
||||
@@ -113,7 +119,7 @@ public class UpsertPlugin extends BasePlugin {
|
||||
METHOD_UPSERT_BY_EXAMPLE,
|
||||
JavaVisibility.DEFAULT,
|
||||
FullyQualifiedJavaType.getIntInstance(),
|
||||
new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record", "@Param(\"record\")"),
|
||||
new Parameter(JavaElementGeneratorTools.getModelTypeWithoutBLOBs(introspectedTable), "record", "@Param(\"record\")"),
|
||||
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mUpsertByExample, introspectedTable);
|
||||
@@ -126,7 +132,7 @@ public class UpsertPlugin extends BasePlugin {
|
||||
METHOD_UPSERT_BY_EXAMPLE_SELECTIVE,
|
||||
JavaVisibility.DEFAULT,
|
||||
FullyQualifiedJavaType.getIntInstance(),
|
||||
new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record", "@Param(\"record\")"),
|
||||
new Parameter(JavaElementGeneratorTools.getModelTypeWithoutBLOBs(introspectedTable), "record", "@Param(\"record\")"),
|
||||
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mUpsertByExampleSelective, introspectedTable);
|
||||
@@ -135,6 +141,61 @@ public class UpsertPlugin extends BasePlugin {
|
||||
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExampleSelective方法。");
|
||||
}
|
||||
|
||||
// !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准
|
||||
if (introspectedTable.hasBLOBColumns()){
|
||||
// ====================================== 1. upsertWithBLOBs ======================================
|
||||
Method mUpsertWithBLOBs = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_UPSERT_WITH_BLOBS,
|
||||
JavaVisibility.DEFAULT,
|
||||
FullyQualifiedJavaType.getIntInstance(),
|
||||
new Parameter(JavaElementGeneratorTools.getModelTypeWithBLOBs(introspectedTable), "record")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mUpsertWithBLOBs, introspectedTable);
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(mUpsertWithBLOBs);
|
||||
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsert方法。");
|
||||
|
||||
// ====================================== 2. upsertSelectiveWithBLOBs ======================================
|
||||
Method mUpsertSelectiveWithBLOBs = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_UPSERT_SELECTIVE_WITH_BLOBS,
|
||||
JavaVisibility.DEFAULT,
|
||||
FullyQualifiedJavaType.getIntInstance(),
|
||||
new Parameter(JavaElementGeneratorTools.getModelTypeWithBLOBs(introspectedTable), "record")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mUpsertSelectiveWithBLOBs, introspectedTable);
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(mUpsertSelectiveWithBLOBs);
|
||||
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertSelective方法。");
|
||||
|
||||
if (this.allowMultiQueries){
|
||||
// ====================================== 3. upsertByExampleWithBLOBs ======================================
|
||||
Method mUpsertByExampleWithBLOBs = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_UPSERT_BY_EXAMPLE_WITH_BLOBS,
|
||||
JavaVisibility.DEFAULT,
|
||||
FullyQualifiedJavaType.getIntInstance(),
|
||||
new Parameter(JavaElementGeneratorTools.getModelTypeWithBLOBs(introspectedTable), "record", "@Param(\"record\")"),
|
||||
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mUpsertByExampleWithBLOBs, introspectedTable);
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(mUpsertByExampleWithBLOBs);
|
||||
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExample方法。");
|
||||
|
||||
// ====================================== 4. upsertByExampleSelectiveWithBLOBs ======================================
|
||||
Method mUpsertByExampleSelectiveWithBLOBs = JavaElementGeneratorTools.generateMethod(
|
||||
METHOD_UPSERT_BY_EXAMPLE_SELECTIVE_WITH_BLOBS,
|
||||
JavaVisibility.DEFAULT,
|
||||
FullyQualifiedJavaType.getIntInstance(),
|
||||
new Parameter(JavaElementGeneratorTools.getModelTypeWithBLOBs(introspectedTable), "record", "@Param(\"record\")"),
|
||||
new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")")
|
||||
);
|
||||
commentGenerator.addGeneralMethodComment(mUpsertByExampleSelectiveWithBLOBs, introspectedTable);
|
||||
// interface 增加方法
|
||||
interfaze.addMethod(mUpsertByExampleSelectiveWithBLOBs);
|
||||
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExampleSelective方法。");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -149,6 +210,142 @@ public class UpsertPlugin extends BasePlugin {
|
||||
@Override
|
||||
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
|
||||
|
||||
this.generateXmlElementWithoutBLOBs(document, introspectedTable);
|
||||
|
||||
// !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准
|
||||
if (introspectedTable.hasBLOBColumns()){
|
||||
this.generateXmlElementWithBLOBs(document, introspectedTable);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当Model有生成WithBLOBs类时的情况
|
||||
*
|
||||
* @param document
|
||||
* @param introspectedTable
|
||||
*/
|
||||
private void generateXmlElementWithBLOBs(Document document, IntrospectedTable introspectedTable){
|
||||
// ====================================== 1. upsert ======================================
|
||||
XmlElement eleUpsertWithBLOBs = new XmlElement("insert");
|
||||
eleUpsertWithBLOBs.addAttribute(new Attribute("id", METHOD_UPSERT_WITH_BLOBS));
|
||||
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
|
||||
commentGenerator.addComment(eleUpsertWithBLOBs);
|
||||
|
||||
// 参数类型
|
||||
eleUpsertWithBLOBs.addAttribute(new Attribute("parameterType", JavaElementGeneratorTools.getModelTypeWithBLOBs(introspectedTable).getFullyQualifiedName()));
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertWithBLOBs, introspectedTable);
|
||||
|
||||
// insert
|
||||
eleUpsertWithBLOBs.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertWithBLOBs.addElement(XmlElementGeneratorTools.generateKeys(introspectedTable.getAllColumns()));
|
||||
eleUpsertWithBLOBs.addElement(new TextElement("values"));
|
||||
eleUpsertWithBLOBs.addElement(XmlElementGeneratorTools.generateValues(introspectedTable.getAllColumns()));
|
||||
eleUpsertWithBLOBs.addElement(new TextElement("on duplicate key update "));
|
||||
eleUpsertWithBLOBs.addElement(XmlElementGeneratorTools.generateSets(introspectedTable.getAllColumns()));
|
||||
|
||||
document.getRootElement().addElement(eleUpsertWithBLOBs);
|
||||
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsert实现方法。");
|
||||
|
||||
// ====================================== 2. upsertSelective ======================================
|
||||
XmlElement eleUpsertSelectiveWithBLOBs = new XmlElement("insert");
|
||||
eleUpsertSelectiveWithBLOBs.addAttribute(new Attribute("id", METHOD_UPSERT_SELECTIVE_WITH_BLOBS));
|
||||
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
|
||||
commentGenerator.addComment(eleUpsertSelectiveWithBLOBs);
|
||||
|
||||
// 参数类型
|
||||
eleUpsertSelectiveWithBLOBs.addAttribute(new Attribute("parameterType", JavaElementGeneratorTools.getModelTypeWithBLOBs(introspectedTable).getFullyQualifiedName()));
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertSelectiveWithBLOBs, introspectedTable);
|
||||
|
||||
// insert
|
||||
eleUpsertSelectiveWithBLOBs.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertSelectiveWithBLOBs.addElement(XmlElementGeneratorTools.generateKeysSelective(introspectedTable.getAllColumns()));
|
||||
eleUpsertSelectiveWithBLOBs.addElement(new TextElement("values"));
|
||||
eleUpsertSelectiveWithBLOBs.addElement(XmlElementGeneratorTools.generateValuesSelective(introspectedTable.getAllColumns()));
|
||||
eleUpsertSelectiveWithBLOBs.addElement(new TextElement("on duplicate key update "));
|
||||
eleUpsertSelectiveWithBLOBs.addElement(XmlElementGeneratorTools.generateSetsSelective(introspectedTable.getAllColumns(), null, false));
|
||||
|
||||
document.getRootElement().addElement(eleUpsertSelectiveWithBLOBs);
|
||||
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
|
||||
|
||||
if (this.allowMultiQueries){
|
||||
// ====================================== 2. upsertByExample ======================================
|
||||
XmlElement eleUpsertByExampleWithBLOBs = new XmlElement("insert");
|
||||
eleUpsertByExampleWithBLOBs.addAttribute(new Attribute("id", METHOD_UPSERT_BY_EXAMPLE_WITH_BLOBS));
|
||||
// 参数类型
|
||||
eleUpsertByExampleWithBLOBs.addAttribute(new Attribute("parameterType", "map"));
|
||||
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
|
||||
commentGenerator.addComment(eleUpsertByExampleWithBLOBs);
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertByExampleWithBLOBs, introspectedTable, "record.");
|
||||
|
||||
// insert
|
||||
eleUpsertByExampleWithBLOBs.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertByExampleWithBLOBs.addElement(XmlElementGeneratorTools.generateKeys(introspectedTable.getAllColumns()));
|
||||
this.generateExistsClause(introspectedTable, eleUpsertByExampleWithBLOBs, false, true);
|
||||
|
||||
// multiQueries
|
||||
eleUpsertByExampleWithBLOBs.addElement(new TextElement(";"));
|
||||
|
||||
// update
|
||||
eleUpsertByExampleWithBLOBs.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertByExampleWithBLOBs.addElement(new TextElement("set"));
|
||||
eleUpsertByExampleWithBLOBs.addElement(XmlElementGeneratorTools.generateSets(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "record."));
|
||||
// update where
|
||||
eleUpsertByExampleWithBLOBs.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
|
||||
|
||||
document.getRootElement().addElement(eleUpsertByExampleWithBLOBs);
|
||||
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
|
||||
|
||||
// ====================================== 4. upsertByExampleSelective ======================================
|
||||
XmlElement eleUpsertByExampleSelectiveWithBLOBs = new XmlElement("insert");
|
||||
eleUpsertByExampleSelectiveWithBLOBs.addAttribute(new Attribute("id", METHOD_UPSERT_BY_EXAMPLE_SELECTIVE_WITH_BLOBS));
|
||||
// 参数类型
|
||||
eleUpsertByExampleSelectiveWithBLOBs.addAttribute(new Attribute("parameterType", "map"));
|
||||
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
|
||||
commentGenerator.addComment(eleUpsertByExampleSelectiveWithBLOBs);
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertByExampleSelectiveWithBLOBs, introspectedTable, "record.");
|
||||
|
||||
// insert
|
||||
eleUpsertByExampleSelectiveWithBLOBs.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertByExampleSelectiveWithBLOBs.addElement(XmlElementGeneratorTools.generateKeysSelective(introspectedTable.getAllColumns(), "record."));
|
||||
this.generateExistsClause(introspectedTable, eleUpsertByExampleSelectiveWithBLOBs, true, true);
|
||||
|
||||
// multiQueries
|
||||
eleUpsertByExampleSelectiveWithBLOBs.addElement(new TextElement(";"));
|
||||
|
||||
// update
|
||||
eleUpsertByExampleSelectiveWithBLOBs.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertByExampleSelectiveWithBLOBs.addElement(new TextElement("set"));
|
||||
eleUpsertByExampleSelectiveWithBLOBs.addElement(XmlElementGeneratorTools.generateSetsSelective(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "record."));
|
||||
|
||||
// update where
|
||||
eleUpsertByExampleSelectiveWithBLOBs.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
|
||||
|
||||
document.getRootElement().addElement(eleUpsertByExampleSelectiveWithBLOBs);
|
||||
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当Model没有生成WithBLOBs类时的情况
|
||||
*
|
||||
* @param document
|
||||
* @param introspectedTable
|
||||
*/
|
||||
private void generateXmlElementWithoutBLOBs(Document document, IntrospectedTable introspectedTable){
|
||||
// WithoutBLOBs也会存在只有一个时,不生成WithBLOBs对象的情况
|
||||
boolean flag = !introspectedTable.getRules().generateRecordWithBLOBsClass();
|
||||
List<IntrospectedColumn> columns = flag ? introspectedTable.getAllColumns() : introspectedTable.getNonBLOBColumns();
|
||||
|
||||
// ====================================== 1. upsert ======================================
|
||||
XmlElement eleUpsert = new XmlElement("insert");
|
||||
eleUpsert.addAttribute(new Attribute("id", METHOD_UPSERT));
|
||||
@@ -156,18 +353,18 @@ public class UpsertPlugin extends BasePlugin {
|
||||
commentGenerator.addComment(eleUpsert);
|
||||
|
||||
// 参数类型
|
||||
eleUpsert.addAttribute(new Attribute("parameterType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
|
||||
eleUpsert.addAttribute(new Attribute("parameterType", JavaElementGeneratorTools.getModelTypeWithoutBLOBs(introspectedTable).getFullyQualifiedName()));
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
XmlElementGeneratorTools.useGeneratedKeys(eleUpsert, introspectedTable);
|
||||
|
||||
// insert
|
||||
eleUpsert.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsert.addElement(XmlElementGeneratorTools.generateKeys(introspectedTable.getAllColumns()));
|
||||
eleUpsert.addElement(XmlElementGeneratorTools.generateKeys(columns));
|
||||
eleUpsert.addElement(new TextElement("values"));
|
||||
eleUpsert.addElement(XmlElementGeneratorTools.generateValues(introspectedTable.getAllColumns()));
|
||||
eleUpsert.addElement(XmlElementGeneratorTools.generateValues(columns));
|
||||
eleUpsert.addElement(new TextElement("on duplicate key update "));
|
||||
eleUpsert.addElement(XmlElementGeneratorTools.generateSets(introspectedTable.getAllColumns()));
|
||||
eleUpsert.addElement(XmlElementGeneratorTools.generateSets(columns));
|
||||
|
||||
document.getRootElement().addElement(eleUpsert);
|
||||
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsert实现方法。");
|
||||
@@ -179,18 +376,18 @@ public class UpsertPlugin extends BasePlugin {
|
||||
commentGenerator.addComment(eleUpsertSelective);
|
||||
|
||||
// 参数类型
|
||||
eleUpsertSelective.addAttribute(new Attribute("parameterType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
|
||||
eleUpsertSelective.addAttribute(new Attribute("parameterType", JavaElementGeneratorTools.getModelTypeWithoutBLOBs(introspectedTable).getFullyQualifiedName()));
|
||||
|
||||
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
|
||||
XmlElementGeneratorTools.useGeneratedKeys(eleUpsertSelective, introspectedTable);
|
||||
|
||||
// insert
|
||||
eleUpsertSelective.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertSelective.addElement(XmlElementGeneratorTools.generateKeysSelective(introspectedTable.getAllColumns()));
|
||||
eleUpsertSelective.addElement(XmlElementGeneratorTools.generateKeysSelective(columns));
|
||||
eleUpsertSelective.addElement(new TextElement("values"));
|
||||
eleUpsertSelective.addElement(XmlElementGeneratorTools.generateValuesSelective(introspectedTable.getAllColumns()));
|
||||
eleUpsertSelective.addElement(XmlElementGeneratorTools.generateValuesSelective(columns));
|
||||
eleUpsertSelective.addElement(new TextElement("on duplicate key update "));
|
||||
eleUpsertSelective.addElement(XmlElementGeneratorTools.generateSetsSelective(introspectedTable.getAllColumns(), null, false));
|
||||
eleUpsertSelective.addElement(XmlElementGeneratorTools.generateSetsSelective(columns, null, false));
|
||||
|
||||
document.getRootElement().addElement(eleUpsertSelective);
|
||||
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
|
||||
@@ -209,8 +406,8 @@ public class UpsertPlugin extends BasePlugin {
|
||||
|
||||
// insert
|
||||
eleUpsertByExample.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertByExample.addElement(XmlElementGeneratorTools.generateKeys(introspectedTable.getAllColumns()));
|
||||
this.generateExistsClause(introspectedTable, eleUpsertByExample, false);
|
||||
eleUpsertByExample.addElement(XmlElementGeneratorTools.generateKeys(columns));
|
||||
this.generateExistsClause(introspectedTable, eleUpsertByExample, false, flag);
|
||||
|
||||
// multiQueries
|
||||
eleUpsertByExample.addElement(new TextElement(";"));
|
||||
@@ -218,7 +415,7 @@ public class UpsertPlugin extends BasePlugin {
|
||||
// update
|
||||
eleUpsertByExample.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertByExample.addElement(new TextElement("set"));
|
||||
eleUpsertByExample.addElement(XmlElementGeneratorTools.generateSets(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "record."));
|
||||
eleUpsertByExample.addElement(XmlElementGeneratorTools.generateSets(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(columns), "record."));
|
||||
// update where
|
||||
eleUpsertByExample.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
|
||||
|
||||
@@ -238,8 +435,8 @@ public class UpsertPlugin extends BasePlugin {
|
||||
|
||||
// insert
|
||||
eleUpsertByExampleSelective.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.generateKeysSelective(introspectedTable.getAllColumns(), "record."));
|
||||
this.generateExistsClause(introspectedTable, eleUpsertByExampleSelective, true);
|
||||
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.generateKeysSelective(columns, "record."));
|
||||
this.generateExistsClause(introspectedTable, eleUpsertByExampleSelective, true, flag);
|
||||
|
||||
// multiQueries
|
||||
eleUpsertByExampleSelective.addElement(new TextElement(";"));
|
||||
@@ -247,7 +444,7 @@ public class UpsertPlugin extends BasePlugin {
|
||||
// update
|
||||
eleUpsertByExampleSelective.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
|
||||
eleUpsertByExampleSelective.addElement(new TextElement("set"));
|
||||
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.generateSetsSelective(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "record."));
|
||||
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.generateSetsSelective(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(columns), "record."));
|
||||
|
||||
// update where
|
||||
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
|
||||
@@ -255,8 +452,6 @@ public class UpsertPlugin extends BasePlugin {
|
||||
document.getRootElement().addElement(eleUpsertByExampleSelective);
|
||||
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,13 +460,16 @@ public class UpsertPlugin extends BasePlugin {
|
||||
* @param introspectedTable
|
||||
* @param element
|
||||
* @param selective
|
||||
* @param allColumns
|
||||
*/
|
||||
private void generateExistsClause(IntrospectedTable introspectedTable, XmlElement element, boolean selective){
|
||||
private void generateExistsClause(IntrospectedTable introspectedTable, XmlElement element, boolean selective, boolean allColumns){
|
||||
List<IntrospectedColumn> columns = allColumns ? introspectedTable.getAllColumns() : introspectedTable.getNonBLOBColumns();
|
||||
|
||||
element.addElement(new TextElement("select"));
|
||||
if (selective){
|
||||
element.addElement(XmlElementGeneratorTools.generateValuesSelective(introspectedTable.getAllColumns(), "record.", false));
|
||||
element.addElement(XmlElementGeneratorTools.generateValuesSelective(columns, "record.", false));
|
||||
} else {
|
||||
element.addElement(XmlElementGeneratorTools.generateValues(introspectedTable.getAllColumns(), "record.", false));
|
||||
element.addElement(XmlElementGeneratorTools.generateValues(columns, "record.", false));
|
||||
}
|
||||
element.addElement(new TextElement("from dual where not exists"));
|
||||
element.addElement(new TextElement("("));
|
||||
|
||||
+38
@@ -16,8 +16,11 @@
|
||||
|
||||
package com.itfsw.mybatis.generator.plugins.utils;
|
||||
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
|
||||
import static org.mybatis.generator.internal.util.messages.Messages.getString;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* Java ele 生成工具
|
||||
@@ -133,4 +136,39 @@ public class JavaElementGeneratorTools {
|
||||
);
|
||||
return generateMethodBody(method, "return this." + field.getName() + ";");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Model没有BLOBs类时的类型
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
public static FullyQualifiedJavaType getModelTypeWithoutBLOBs(IntrospectedTable introspectedTable){
|
||||
FullyQualifiedJavaType type;
|
||||
if (introspectedTable.getRules().generateBaseRecordClass()) {
|
||||
type = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
|
||||
} else if (introspectedTable.getRules().generatePrimaryKeyClass()) {
|
||||
type = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
|
||||
} else {
|
||||
throw new RuntimeException(getString("RuntimeError.12"));
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Model有BLOBs类时的类型
|
||||
*
|
||||
* @param introspectedTable
|
||||
* @return
|
||||
*/
|
||||
public static FullyQualifiedJavaType getModelTypeWithBLOBs(IntrospectedTable introspectedTable){
|
||||
FullyQualifiedJavaType type;
|
||||
if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
|
||||
type = new FullyQualifiedJavaType(introspectedTable.getRecordWithBLOBsType());
|
||||
} else {
|
||||
// the blob fields must be rolled up into the base class
|
||||
type = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
|
||||
}
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user