在写自己的开发框架时,需要用到特殊的sql语句,使用默认的generatorConfig.xml文件配置,无法实现,我们可以自己定义一个generator插件
一、先新建一个项目,作为插件项目
首先引入依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mytui.mybatis.plugins</groupId>
<artifactId>GeneratorPlugins</artifactId>
<version>1.0.1</version>
<name>GeneratorPlugins</name>
<description>GeneratorPlugins</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>com.mytui.mybatis.plugins.GeneratorPlugins</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.5.RELEASE</version>
</plugin>
<!-- 单独打包一个类-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.mytui.mybatis.plugins.generatorplugins.CustomizeMapperXmlPlugin</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
二、再新建插件类,继承 PluginAdapter,通过重新 sqlMapDocumentGenerated方法,在该方法内实现自己sql语句构建
/**
* 重新,添加自定义sql语句构建
* @param document
* @param introspectedTable
* @return
*/
@Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
super.sqlMapDocumentGenerated(document,introspectedTable);
Attribute attribute=null;
XmlElement rootElement = document.getRootElement();
XmlElement selectElem=new XmlElement("select");
//添加id属性
attribute=new Attribute("id","selectByFilter");
selectElem.addAttribute(attribute);
//添加parameterType属性
attribute=new Attribute("parameterType","String");
selectElem.addAttribute(attribute);
//添加resultType属性
attribute=new Attribute("resultType","BaseResultMap");
selectElem.addAttribute(attribute);
//构架自定义SQL
StringBuilder builder=new StringBuilder();
builder.append("select \n\t");
builder.append("<include refid=\"Base_Column_List\" /> \n\t");
builder.append("from \n\t");
builder.append(introspectedTable.getFullyQualifiedTableNameAtRuntime()+" \n\t");
builder.append("where 1=1 \n\t");
builder.append("<if test=\" filter !=null and filter !='' \" > and ${filter}</if>");
TextElement textElem=new TextElement(builder.toString());
selectElem.addElement(textElem);
//把自定义元素添加到root文档中
rootElement.addElement(selectElem);
return true;
}
三、在Build下,Build Artifacts