Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88a4c10141 | ||
|
|
a616ccac67 | ||
|
|
8d54c7559e | ||
|
|
05558ab088 | ||
|
|
bf2045178f | ||
|
|
9ec7290cdd | ||
|
|
58c65dc8b5 | ||
|
|
5e5fe44a60 | ||
|
|
195f1e631e | ||
|
|
5133c7b359 | ||
|
|
e4754d9c20 | ||
|
|
3a55dd8c85 | ||
|
|
6a6b991a3d | ||
|
|
d8f0e0dd1d | ||
|
|
9ce992c728 | ||
|
|
ed19ffa96d | ||
|
|
b564e9d98b | ||
|
|
cbc9c5a7c6 | ||
|
|
315a896003 | ||
|
|
ab4c5e0f6f | ||
|
|
74ab3a66f9 | ||
|
|
a864c9784a | ||
|
|
0eb971fa53 | ||
|
|
6e132d7dc2 | ||
|
|
7393b26105 |
@@ -1 +1,213 @@
|
||||
# mybatis-generator-plugin
|
||||
# 这是 MyBatis Generator 插件的拓展插件包
|
||||
应该说使用Mybatis就一定离不开[MyBatis Generator](https://github.com/mybatis/generator)这款代码生成插件,而这款插件自身还提供了插件拓展功能用于强化插件本身,官方已经提供了一些[拓展插件](http://www.mybatis.org/generator/reference/plugins.html),本项目的目的也是通过该插件机制来强化Mybatis Generator本身,方便和减少我们平时的代码开发量。
|
||||
>因为插件是本人兴之所至所临时发布的项目(本人已近三年未做JAVA开发,代码水平请大家见谅),但基本插件都是在实际项目中经过检验的请大家放心使用,但因为项目目前主要数据库为MySQL,Mybatis实现使用Mapper.xml方式,所以代码生成时对于其他数据库和注解方式的支持未予考虑,请大家见谅。
|
||||
|
||||
---------------------------------------
|
||||
插件列表:
|
||||
* 查询单条数据插件
|
||||
* MySQL分页插件
|
||||
* 数据Model链式构建插件
|
||||
* Example Criteria 增强插件(example,andIf)
|
||||
* Example 目标包修改插件
|
||||
|
||||
---------------------------------------
|
||||
Maven引用:
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.itfsw</groupId>
|
||||
<artifactId>mybatis-generator-plugin</artifactId>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
```
|
||||
---------------------------------------
|
||||
### 1. 查询单条数据插件
|
||||
对应表Mapper接口增加了方法
|
||||
插件:
|
||||
```xml
|
||||
<!-- 查询单条数据插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"/>
|
||||
```
|
||||
使用:
|
||||
```java
|
||||
public interface TbMapper {
|
||||
/**
|
||||
* 这是Mybatis Generator拓展插件生成的方法(请勿删除).
|
||||
* This method corresponds to the database table tb
|
||||
*
|
||||
* @mbg.generated
|
||||
* @author hewei
|
||||
*/
|
||||
Tb selectOneByExample(TbExample example);
|
||||
}
|
||||
```
|
||||
### 2. MySQL分页插件
|
||||
对应表Example类增加了方法
|
||||
插件:
|
||||
```xml
|
||||
<!-- MySQL分页插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.LimitPlugin"/>
|
||||
```
|
||||
使用:
|
||||
```java
|
||||
public class TbExample {
|
||||
/**
|
||||
* 这是Mybatis Generator拓展插件生成的属性(请勿删除).
|
||||
* This field corresponds to the database table tb
|
||||
*
|
||||
* @mbg.generated
|
||||
* @author hewei
|
||||
*/
|
||||
protected Integer offset;
|
||||
|
||||
/**
|
||||
* 这是Mybatis Generator拓展插件生成的属性(请勿删除).
|
||||
* This field corresponds to the database table tb
|
||||
*
|
||||
* @mbg.generated
|
||||
* @author hewei
|
||||
*/
|
||||
protected Integer rows;
|
||||
/**
|
||||
* 这是Mybatis Generator拓展插件生成的方法(请勿删除).
|
||||
* This method corresponds to the database table rc_user_token
|
||||
*
|
||||
* @mbg.generated
|
||||
* @author hewei
|
||||
*/
|
||||
public void limit(Integer rows) {
|
||||
this.rows = rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* 这是Mybatis Generator拓展插件生成的方法(请勿删除).
|
||||
* This method corresponds to the database table rc_user_token
|
||||
*
|
||||
* @mbg.generated
|
||||
* @author hewei
|
||||
*/
|
||||
public void limit(Integer offset, Integer rows) {
|
||||
this.offset = offset;
|
||||
this.rows = rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* 这是Mybatis Generator拓展插件生成的方法(请勿删除).
|
||||
* This method corresponds to the database table tb
|
||||
*
|
||||
* @mbg.generated
|
||||
* @author hewei
|
||||
*/
|
||||
public void page(Integer page, Integer pageSize) {
|
||||
this.offset = page * pageSize;
|
||||
this.rows = pageSize;
|
||||
}
|
||||
|
||||
// offset 和 rows 的getter&setter
|
||||
|
||||
// 修正了clear方法
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table tb
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public void clear() {
|
||||
oredCriteria.clear();
|
||||
orderByClause = null;
|
||||
distinct = false;
|
||||
rows = null;
|
||||
offset = null;
|
||||
}
|
||||
}
|
||||
```
|
||||
### 3. 数据Model链式构建插件
|
||||
这个是仿jquery的链式调用强化了表的Model的赋值操作
|
||||
插件:
|
||||
```xml
|
||||
<!-- 数据Model链式构建插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin"/>
|
||||
```
|
||||
使用:
|
||||
```java
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
// 直接new表Model的内部Builder类,赋值后调用build()方法返回对象
|
||||
Tb table = new Tb.Builder()
|
||||
.field1("xx")
|
||||
.field2("xx")
|
||||
.field3("xx")
|
||||
.field4("xx")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
```
|
||||
### 4. Example Criteria 增强插件(example,andIf)
|
||||
* 表Example增加Criteria的快速返回example()方法。
|
||||
* Criteria链式调用增强,以前如果有按条件增加的查询语句会打乱链式查询构建,现在有了andIf(boolean ifAdd, CriteriaAdd add)方法可一直使用链式调用下去。
|
||||
插件:
|
||||
```xml
|
||||
<!-- Example Criteria 增强插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.CriteriaBuilderPlugin"/>
|
||||
```
|
||||
使用:
|
||||
```java
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
// -----------------------------------example-----------------------------------
|
||||
// 表Example.Criteria增加了工厂方法example()支持,使用后可链式构建查询条件使用example()返回Example对象
|
||||
TbExample ex = new TbExample()
|
||||
.createCriteria()
|
||||
.andField1EqualTo(1)
|
||||
.andField2EqualTo("xxx")
|
||||
.example();
|
||||
this.tbMapper.selectByExample(ex);
|
||||
|
||||
// -----------------------------------andIf-----------------------------------
|
||||
// Criteria增强了链式调用,现在一些按条件增加的查询条件不会打乱链式调用了
|
||||
// old
|
||||
TbExample oldEx = new TbExample();
|
||||
TbExample.Criteria criteria = oldEx
|
||||
.createCriteria()
|
||||
.andField1EqualTo(1)
|
||||
.andField2EqualTo("xxx");
|
||||
// 如果随机数大于0.5,附加Field3查询条件
|
||||
if (Math.random() > 0.5){
|
||||
criteria.andField3EqualTo(2)
|
||||
.andField4EqualTo(new Date());
|
||||
}
|
||||
this.tbMapper.selectByExample(oldEx);
|
||||
|
||||
// new
|
||||
TbExample newEx;
|
||||
newEx = new TbExample()
|
||||
.createCriteria()
|
||||
.andField1EqualTo(1)
|
||||
.andField2EqualTo("xxx")
|
||||
// 如果随机数大于0.5,附加Field3查询条件
|
||||
.andIf(Math.random() > 0.5, new TbExample.Criteria.CriteriaAdd() {
|
||||
@Override
|
||||
public TbExample.Criteria add(TbExample.Criteria add) {
|
||||
return add.andField3EqualTo(2)
|
||||
.andField4EqualTo(new Date());
|
||||
}
|
||||
})
|
||||
// 当然最简洁的写法是采用java8的Lambda表达式,当然你的项目是Java8+
|
||||
.andIf(Math.random() > 0.5, add -> add
|
||||
.andField3EqualTo(2)
|
||||
.andField4EqualTo(new Date())
|
||||
)
|
||||
.example();
|
||||
this.tbMapper.selectByExample(newEx);
|
||||
}
|
||||
}
|
||||
```
|
||||
### 5. Example 目标包修改插件
|
||||
Mybatis Generator 插件默认把Model类和Example类都生成到一个包下,这样该包下类就会很多不方便区分,该插件目的就是把Example类独立到一个新包下,方便查看。
|
||||
插件:
|
||||
```xml
|
||||
<!-- Example 目标包修改插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin">
|
||||
<!-- 修改Example类生成到目标包下 -->
|
||||
<property name="targetPackage" value="com.itfsw.mybatis.generator.dao.example"/>
|
||||
</plugin>
|
||||
```
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
<groupId>com.itfsw</groupId>
|
||||
<artifactId>mybatis-generator-plugin</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.2</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.0</tag>
|
||||
<tag>v1.0.2</tag>
|
||||
</scm>
|
||||
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++distribution++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
@@ -184,6 +184,7 @@
|
||||
http://central.sonatype.org/pages/working-with-pgp-signatures.html
|
||||
http://www.cnblogs.com/gaoxing/p/4359795.html
|
||||
http://blog.csdn.net/ssrc0604hx/article/details/51513414
|
||||
http://www.trinea.cn/dev-tools/upload-java-jar-or-android-aar-to-maven-center-repository/
|
||||
-->
|
||||
<distributionManagement>
|
||||
<snapshotRepository>
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
package com.itfsw.mybatis.generator.plugins;
|
||||
|
||||
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.InnerInterface;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.InnerInterfaceWrapperToInnerClass;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.PluginAdapter;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
@@ -55,7 +57,10 @@ public class CriteriaBuilderPlugin extends PluginAdapter {
|
||||
List<InnerClass> innerClasses = topLevelClass.getInnerClasses();
|
||||
for (InnerClass innerClass : innerClasses) {
|
||||
if ("Criteria".equals(innerClass.getType().getShortName())) {
|
||||
// 工厂方法
|
||||
addFactoryMethodToCriteria(topLevelClass, innerClass, introspectedTable);
|
||||
// andIf
|
||||
addAndIfMethodToCriteria(topLevelClass, innerClass, introspectedTable);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +69,7 @@ public class CriteriaBuilderPlugin extends PluginAdapter {
|
||||
if (!"createCriteriaInternal".equals(method.getName()))
|
||||
continue;
|
||||
method.getBodyLines().set(0, "Criteria criteria = new Criteria(this);");
|
||||
logger.info("itfsw:CriteriaBuilder修改Example的createCriteriaInternal方法,修改构造Criteria时传入Example对象");
|
||||
logger.debug("itfsw:CriteriaBuilder修改Example的createCriteriaInternal方法,修改构造Criteria时传入Example对象");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -87,7 +92,7 @@ public class CriteriaBuilderPlugin extends PluginAdapter {
|
||||
if (method.isConstructor()) {
|
||||
method.addParameter(new Parameter(topLevelClass.getType(), "example"));
|
||||
method.addBodyLine("this.example = example;");
|
||||
logger.info("itfsw:CriteriaBuilder修改Criteria的构造方法,增加example参数");
|
||||
logger.debug("itfsw:CriteriaBuilder修改Criteria的构造方法,增加example参数");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,6 +103,47 @@ public class CriteriaBuilderPlugin extends PluginAdapter {
|
||||
method.addBodyLine("return this.example;");
|
||||
CommentTools.addGeneralMethodComment(method, introspectedTable);
|
||||
innerClass.addMethod(method);
|
||||
logger.info("itfsw:CriteriaBuilder增加工厂方法example");
|
||||
logger.debug("itfsw:CriteriaBuilder增加工厂方法example");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 增强Criteria的链式调用,添加andIf(boolean addIf, CriteriaAdd add)方法,实现链式调用中按条件增加查询语句
|
||||
*
|
||||
* @param topLevelClass
|
||||
* @param innerClass
|
||||
* @param introspectedTable
|
||||
*/
|
||||
private void addAndIfMethodToCriteria(TopLevelClass topLevelClass, InnerClass innerClass, IntrospectedTable introspectedTable){
|
||||
// 添加接口CriteriaAdd
|
||||
InnerInterface criteriaAddInterface = new InnerInterface("ICriteriaAdd");
|
||||
criteriaAddInterface.setVisibility(JavaVisibility.PUBLIC);
|
||||
CommentTools.addInterfaceComment(criteriaAddInterface, introspectedTable);
|
||||
|
||||
// ICriteriaAdd增加接口add
|
||||
Method addMethod = new Method("add");
|
||||
addMethod.setReturnType(innerClass.getType());
|
||||
addMethod.addParameter(new Parameter(innerClass.getType(), "add"));
|
||||
CommentTools.addGeneralMethodComment(addMethod, introspectedTable);
|
||||
criteriaAddInterface.addMethod(addMethod);
|
||||
logger.debug("itfsw:Criteria.ICriteriaAdd增加接口add");
|
||||
|
||||
InnerClass innerClassWrapper = new InnerInterfaceWrapperToInnerClass(criteriaAddInterface);
|
||||
innerClass.addInnerClass(innerClassWrapper);
|
||||
|
||||
// 添加andIf方法
|
||||
Method method = new Method("andIf");
|
||||
method.setVisibility(JavaVisibility.PUBLIC);
|
||||
method.setReturnType(innerClass.getType());
|
||||
method.addParameter(new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "ifAdd"));
|
||||
method.addParameter(new Parameter(criteriaAddInterface.getType(), "add"));
|
||||
|
||||
method.addBodyLine("if (ifAdd) {");
|
||||
method.addBodyLine("add.add(this);");
|
||||
method.addBodyLine("}");
|
||||
method.addBodyLine("return this;");
|
||||
CommentTools.addGeneralMethodComment(method, introspectedTable);
|
||||
innerClass.addMethod(method);
|
||||
logger.debug("itfsw:Criteria增加方法andIf");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
*
|
||||
* * Copyright (c) 2014.
|
||||
* *
|
||||
* * 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 org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.PluginAdapter;
|
||||
import org.mybatis.generator.config.Context;
|
||||
import org.mybatis.generator.config.JavaModelGeneratorConfiguration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* Example类生成位置修改
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/1/12 12:36
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class ExampleTargetPlugin extends PluginAdapter {
|
||||
public static final String TARGET_PACKAGE_KEY = "targetPackage"; // 配置targetPackage名
|
||||
private static final Logger logger = LoggerFactory.getLogger(CriteriaBuilderPlugin.class);
|
||||
|
||||
private String targetPackage; // 目标包
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean validate(List<String> warnings) {
|
||||
// 获取配置的目标package
|
||||
Properties properties = getProperties();
|
||||
this.targetPackage = properties.getProperty(TARGET_PACKAGE_KEY);
|
||||
if (this.targetPackage == null){
|
||||
logger.warn("请配置com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin插件的目标包名(targetPackage)!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化阶段
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
*
|
||||
* @param introspectedTable
|
||||
*/
|
||||
@Override
|
||||
public void initialized(IntrospectedTable introspectedTable) {
|
||||
String exampleType = introspectedTable.getExampleType();
|
||||
// 修改包名
|
||||
Context context = getContext();
|
||||
JavaModelGeneratorConfiguration configuration = context.getJavaModelGeneratorConfiguration();
|
||||
String targetPackage = configuration.getTargetPackage();
|
||||
String newExampleType = exampleType.replace(targetPackage, this.targetPackage);
|
||||
|
||||
introspectedTable.setExampleType(newExampleType);
|
||||
|
||||
logger.debug("itfsw:修改"+exampleType+"的包到"+this.targetPackage);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -69,7 +69,7 @@ public class LimitPlugin extends PluginAdapter {
|
||||
rows.setType(integerWrapper);
|
||||
CommentTools.addFieldComment(rows, introspectedTable);
|
||||
topLevelClass.addField(rows);
|
||||
logger.info("itfsw:分页插件增加Example的offset和rows字段");
|
||||
logger.debug("itfsw:分页插件增加Example的offset和rows字段");
|
||||
|
||||
// 增加getter && setter 方法
|
||||
Method setOffset = new Method();
|
||||
@@ -103,7 +103,7 @@ public class LimitPlugin extends PluginAdapter {
|
||||
getRows.addBodyLine("return rows;");
|
||||
CommentTools.addGeneralMethodComment(getRows, introspectedTable);
|
||||
topLevelClass.addMethod(getRows);
|
||||
logger.info("itfsw:分页插件增加Example的offset和rows的getter和setter");
|
||||
logger.debug("itfsw:分页插件增加Example的offset和rows的getter和setter");
|
||||
|
||||
// 提供几个快捷方法
|
||||
Method setLimit = new Method();
|
||||
@@ -123,7 +123,7 @@ public class LimitPlugin extends PluginAdapter {
|
||||
setLimit2.addBodyLine("this.rows = rows;");
|
||||
CommentTools.addGeneralMethodComment(setLimit2, introspectedTable);
|
||||
topLevelClass.addMethod(setLimit2);
|
||||
logger.info("itfsw:分页插件增加Example的limit方法");
|
||||
logger.debug("itfsw:分页插件增加Example的limit方法");
|
||||
|
||||
Method setPage = new Method();
|
||||
setPage.setVisibility(JavaVisibility.PUBLIC);
|
||||
@@ -134,7 +134,7 @@ public class LimitPlugin extends PluginAdapter {
|
||||
setPage.addBodyLine("this.rows = pageSize;");
|
||||
CommentTools.addGeneralMethodComment(setPage, introspectedTable);
|
||||
topLevelClass.addMethod(setPage);
|
||||
logger.info("itfsw:分页插件增加Example的page方法");
|
||||
logger.debug("itfsw:分页插件增加Example的page方法");
|
||||
|
||||
// !!! clear 方法增加 offset 和 rows的清理
|
||||
List<Method> methodList = topLevelClass.getMethods();
|
||||
@@ -142,7 +142,7 @@ public class LimitPlugin extends PluginAdapter {
|
||||
if (method.getName().equals("clear")){
|
||||
method.addBodyLine("rows = null;");
|
||||
method.addBodyLine("offset = null;");
|
||||
logger.info("itfsw:分页插件修正Example的clear方法,增加rows和offset字段的清空");
|
||||
logger.debug("itfsw:分页插件修正Example的clear方法,增加rows和offset字段的清空");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ public class ModelBuilderPlugin extends PluginAdapter {
|
||||
innerClass.setVisibility(JavaVisibility.PUBLIC);
|
||||
innerClass.setStatic(true);
|
||||
CommentTools.addClassComment(innerClass, introspectedTable);
|
||||
logger.info("itfsw:生成内部Builder类");
|
||||
logger.debug("itfsw:生成内部Builder类");
|
||||
|
||||
// 构建内部obj变量
|
||||
Field f = new Field("obj", topLevelClass.getType());
|
||||
@@ -77,7 +77,7 @@ public class ModelBuilderPlugin extends PluginAdapter {
|
||||
constructor.addBodyLine(new StringBuilder("this.obj = new ")
|
||||
.append(topLevelClass.getType().getShortName()).append("();").toString());
|
||||
innerClass.addMethod(constructor);
|
||||
logger.info("itfsw:生成内部Builder类构造方法");
|
||||
logger.debug("itfsw:生成内部Builder类构造方法");
|
||||
|
||||
// !!可能Model存在复合主键情况,字段要加上这些
|
||||
if (topLevelClass.getSuperClass() != null && topLevelClass.getSuperClass().compareTo(new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType())) == 0){
|
||||
@@ -94,7 +94,7 @@ public class ModelBuilderPlugin extends PluginAdapter {
|
||||
method.addBodyLine(new StringBuilder().append("return this;").toString());
|
||||
CommentTools.addGeneralMethodComment(method, introspectedTable);
|
||||
innerClass.addMethod(method);
|
||||
logger.info("itfsw:生成内部Builder类的复合主键字段对应方法"+field.getName());
|
||||
logger.debug("itfsw:生成内部Builder类的复合主键字段对应方法"+field.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ public class ModelBuilderPlugin extends PluginAdapter {
|
||||
method.addBodyLine(new StringBuilder().append("return this;").toString());
|
||||
CommentTools.addGeneralMethodComment(method, introspectedTable);
|
||||
innerClass.addMethod(method);
|
||||
logger.info("itfsw:生成内部Builder类的普通字段对应方法"+field.getName());
|
||||
logger.debug("itfsw:生成内部Builder类的普通字段对应方法"+field.getName());
|
||||
}
|
||||
|
||||
Method build = new Method("build");
|
||||
@@ -121,7 +121,7 @@ public class ModelBuilderPlugin extends PluginAdapter {
|
||||
build.addBodyLine("return this.obj;");
|
||||
CommentTools.addGeneralMethodComment(build, introspectedTable);
|
||||
innerClass.addMethod(build);
|
||||
logger.info("itfsw:生成内部Builder类的build方法");
|
||||
logger.debug("itfsw:生成内部Builder类的build方法");
|
||||
|
||||
topLevelClass.addInnerClass(innerClass);
|
||||
return true;
|
||||
|
||||
@@ -63,7 +63,7 @@ public class SelectOneByExamplePlugin extends PluginAdapter {
|
||||
*/
|
||||
@Override
|
||||
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
logger.info("itfsw:生成"+interfaze.getType()+"对应SelectOneByExample方法...");
|
||||
logger.debug("itfsw:生成"+interfaze.getType()+"对应SelectOneByExample方法...");
|
||||
// 方法生成
|
||||
Method method = new Method(METHOD_NAME);
|
||||
// 方法可见性
|
||||
@@ -92,7 +92,7 @@ public class SelectOneByExamplePlugin extends PluginAdapter {
|
||||
*/
|
||||
@Override
|
||||
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
|
||||
logger.info("itfsw:生成表"+introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()+"("+ (introspectedTable.hasBLOBColumns() ? "有" : "无")+"Blob类型)的mapping.xml对应SelectOneByExample方法...");
|
||||
logger.debug("itfsw:生成表"+introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()+"("+ (introspectedTable.hasBLOBColumns() ? "有" : "无")+"Blob类型)的mapping.xml对应SelectOneByExample方法...");
|
||||
|
||||
//数据库表名
|
||||
String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();
|
||||
|
||||
@@ -19,6 +19,7 @@ package com.itfsw.mybatis.generator.plugins.utils;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.Field;
|
||||
import org.mybatis.generator.api.dom.java.InnerClass;
|
||||
import org.mybatis.generator.api.dom.java.Interface;
|
||||
import org.mybatis.generator.api.dom.java.Method;
|
||||
import org.mybatis.generator.api.dom.xml.TextElement;
|
||||
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||
@@ -111,4 +112,24 @@ public class CommentTools {
|
||||
|
||||
xmlElement.addElement(new TextElement("-->")); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成通用接口注解
|
||||
*
|
||||
* @param interf 接口
|
||||
* @param introspectedTable 表
|
||||
*/
|
||||
public static void addInterfaceComment(Interface interf, IntrospectedTable introspectedTable) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
interf.addJavaDocLine("/**"); //$NON-NLS-1$
|
||||
interf.addJavaDocLine(" * 这是Mybatis Generator拓展插件生成的接口(请勿删除)."); //$NON-NLS-1$
|
||||
sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
interf.addJavaDocLine(sb.toString());
|
||||
interf.addJavaDocLine(" *");
|
||||
interf.addJavaDocLine(" * "+MergeConstants.NEW_ELEMENT_TAG);
|
||||
interf.addJavaDocLine(" * @author hewei");
|
||||
interf.addJavaDocLine(" */"); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
*
|
||||
* * Copyright (c) 2014.
|
||||
* *
|
||||
* * 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 org.mybatis.generator.api.dom.OutputUtilities;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.mybatis.generator.api.dom.OutputUtilities.*;
|
||||
import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* 内部接口
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/1/12 17:53
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class InnerInterface extends Interface{
|
||||
public InnerInterface(FullyQualifiedJavaType type) {
|
||||
super(type);
|
||||
}
|
||||
|
||||
public InnerInterface(String type) {
|
||||
super(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化后内容,因为是内部接口,需要增加缩进
|
||||
*
|
||||
* @param indentLevel the indent level
|
||||
* @param compilationUnit the compilation unit
|
||||
* @return the formatted content
|
||||
*/
|
||||
public String getFormattedContent(int indentLevel, CompilationUnit compilationUnit) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (String commentLine : getFileCommentLines()) {
|
||||
sb.append(commentLine);
|
||||
newLine(sb);
|
||||
}
|
||||
|
||||
if (stringHasValue(getType().getPackageName())) {
|
||||
sb.append("package "); //$NON-NLS-1$
|
||||
sb.append(getType().getPackageName());
|
||||
sb.append(';');
|
||||
newLine(sb);
|
||||
newLine(sb);
|
||||
}
|
||||
|
||||
for (String staticImport : getStaticImports()) {
|
||||
sb.append("import static "); //$NON-NLS-1$
|
||||
sb.append(staticImport);
|
||||
sb.append(';');
|
||||
newLine(sb);
|
||||
}
|
||||
|
||||
if (getStaticImports().size() > 0) {
|
||||
newLine(sb);
|
||||
}
|
||||
|
||||
Set<String> importStrings = calculateImports(getImportedTypes());
|
||||
for (String importString : importStrings) {
|
||||
sb.append(importString);
|
||||
newLine(sb);
|
||||
}
|
||||
|
||||
if (importStrings.size() > 0) {
|
||||
newLine(sb);
|
||||
}
|
||||
|
||||
addFormattedJavadoc(sb, indentLevel);
|
||||
addFormattedAnnotations(sb, indentLevel);
|
||||
|
||||
OutputUtilities.javaIndent(sb, indentLevel);
|
||||
|
||||
sb.append(getVisibility().getValue());
|
||||
|
||||
if (isFinal()) {
|
||||
sb.append("final "); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
sb.append("interface "); //$NON-NLS-1$
|
||||
sb.append(getType().getShortName());
|
||||
|
||||
if (getSuperInterfaceTypes().size() > 0) {
|
||||
sb.append(" extends "); //$NON-NLS-1$
|
||||
|
||||
boolean comma = false;
|
||||
for (FullyQualifiedJavaType fqjt : getSuperInterfaceTypes()) {
|
||||
if (comma) {
|
||||
sb.append(", "); //$NON-NLS-1$
|
||||
} else {
|
||||
comma = true;
|
||||
}
|
||||
|
||||
sb.append(JavaDomUtils.calculateTypeName(this, fqjt));
|
||||
}
|
||||
}
|
||||
|
||||
sb.append(" {"); //$NON-NLS-1$
|
||||
indentLevel++;
|
||||
|
||||
Iterator<Method> mtdIter = getMethods().iterator();
|
||||
while (mtdIter.hasNext()) {
|
||||
newLine(sb);
|
||||
Method method = mtdIter.next();
|
||||
sb.append(method.getFormattedContent(indentLevel, true, this));
|
||||
if (mtdIter.hasNext()) {
|
||||
newLine(sb);
|
||||
}
|
||||
}
|
||||
|
||||
indentLevel--;
|
||||
newLine(sb);
|
||||
javaIndent(sb, indentLevel);
|
||||
sb.append('}');
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
*
|
||||
* * Copyright (c) 2014.
|
||||
* *
|
||||
* * 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 org.mybatis.generator.api.dom.java.CompilationUnit;
|
||||
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
|
||||
import org.mybatis.generator.api.dom.java.InnerClass;
|
||||
|
||||
/**
|
||||
* ---------------------------------------------------------------------------
|
||||
* 把InnerInterface包装成InnerClass(Mybatis Generator 没有提供内部接口实现)
|
||||
* ---------------------------------------------------------------------------
|
||||
* @author: hewei
|
||||
* @time:2017/1/12 17:40
|
||||
* ---------------------------------------------------------------------------
|
||||
*/
|
||||
public class InnerInterfaceWrapperToInnerClass extends InnerClass{
|
||||
private InnerInterface innerInterface; // 内部接口
|
||||
|
||||
|
||||
public InnerInterfaceWrapperToInnerClass(FullyQualifiedJavaType type) {
|
||||
super(type);
|
||||
}
|
||||
|
||||
public InnerInterfaceWrapperToInnerClass(String typeName) {
|
||||
super(typeName);
|
||||
}
|
||||
|
||||
public InnerInterfaceWrapperToInnerClass(InnerInterface innerInterface){
|
||||
super(innerInterface.getType());
|
||||
this.innerInterface = innerInterface;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重写获取Java内容方法,调用InnerInterface的实现
|
||||
*
|
||||
* @param indentLevel
|
||||
* @param compilationUnit
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String getFormattedContent(int indentLevel, CompilationUnit compilationUnit) {
|
||||
return this.innerInterface.getFormattedContent(indentLevel, compilationUnit);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user