Skip to content

Commit

Permalink
feat: 添加plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
JackySoft authored and jianbing.chen committed Dec 18, 2024
1 parent 46d40c5 commit a3fe09e
Show file tree
Hide file tree
Showing 14 changed files with 2,230 additions and 3 deletions.
38 changes: 38 additions & 0 deletions backend/java/xtr-mybatis-generator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
56 changes: 56 additions & 0 deletions backend/java/xtr-mybatis-generator/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.xtr</groupId>
<artifactId>xtr-mybatis-generator</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.xtr</groupId>
<artifactId>xtr-comm-public</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>com.xtr</groupId>
<artifactId>xtr-jdbc-plugin</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>

<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>

<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.xtr;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.VerboseProgressCallback;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
* <p>类说明</p>
*
* @author 张峰 [email protected]
* @createTime: 2018/7/12 12:22
*/
public class MybatisGenerator {

public static void main(String[] args) {
try {
List<String> warnings = new ArrayList<>();
boolean overwrite = true;
//这个路径要注意
String path = "D:\\IdeaProjects\\xtr-mybatis-generator\\src\\main\\resources\\mybatis-generator.xml";
System.out.println(path);
File configFile = new File(path);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(new VerboseProgressCallback());
warnings.forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.mybatis.generator.api;

import org.apache.commons.lang.StringUtils;

import java.util.List;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
import static org.mybatis.generator.internal.util.messages.Messages.getString;

/**
* <p>类说明</p>
*
* @author 张峰 [email protected]
* @createTime: 2019-03-08 16:58
*/
public abstract class AbstractGeneratorPlugin extends PluginAdapter {

protected IntrospectedTable introspectedTable;


@Override
public boolean validate(List<String> warnings) {
boolean valid = true;

if (!stringHasValue(properties
.getProperty("targetProject"))) {
warnings.add(getString("ValidationError.18",
"MapperConfigPlugin",
"targetProject"));
valid = false;
}

if (!stringHasValue(properties
.getProperty("targetPackage"))) {
warnings.add(getString("ValidationError.18",
"MapperConfigPlugin",
"targetPackage"));
valid = false;
}

return valid;
}

/**
* 获取文件名
*
* @return
*/
protected String getFailName() {
String keyword = properties.getProperty("keyword");
if (StringUtils.isNotEmpty(keyword) && keyword.length() > 1) {
keyword = keyword.substring(0, 1).toUpperCase() + keyword.substring(1);
} else {
keyword = "";
}
return introspectedTable.getFullyQualifiedTable().getDomainObjectName() + keyword + "Mapper";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.mybatis.generator.api;

import com.xtr.comm.basic.BasicMapper;
import com.xtr.comm.util.DateUtil;
import com.xtr.jdbc.config.datasource.DataBaseType;
import com.xtr.jdbc.config.datasource.UseDataSource;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;

import java.util.ArrayList;
import java.util.List;

/**
* <p>类说明</p>
*
* @author 张峰 [email protected]
* @createTime: 2018/5/7 13:55
*/
public class JavaMappperGeneratorPlugin extends AbstractGeneratorPlugin {


@Override
public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
this.introspectedTable = introspectedTable;
List<GeneratedJavaFile> list = new ArrayList<GeneratedJavaFile>();
FullyQualifiedJavaType type = new FullyQualifiedJavaType(getFailName());
//设置包路径
type.setPackageName(properties.getProperty("targetPackage"));

Interface interfaze = new Interface(type);
interfaze.setVisibility(JavaVisibility.PUBLIC);
interfaze.getFormattedContent();
//添加属性配置
addAttribute(interfaze);
//添加类注释
addModelClassComment(interfaze);
//生成文件并添加到list中
list.add(new GeneratedJavaFile(interfaze, properties.getProperty("targetProject"), getContext().getJavaFormatter()));
return list;
}

/**
* 添加对应的属性
*
* @param interfaze
*/
private void addAttribute(Interface interfaze) {
FullyQualifiedJavaType fullyQualifiedJavaType = new FullyQualifiedJavaType(BasicMapper.class.getName());
fullyQualifiedJavaType.addTypeArgument(new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()));
interfaze.addImportedType(new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()));
interfaze.addSuperInterface(fullyQualifiedJavaType);
interfaze.addImportedType(new FullyQualifiedJavaType(BasicMapper.class.getName()));
if (StringUtils.equals(DataBaseType.OLD_DATA_SOURCE.getSchemaName(), this.introspectedTable.context.getTableConfigurations().get(0).getSchema())) {
interfaze.addImportedType(new FullyQualifiedJavaType(DataBaseType.class.getName()));
interfaze.addImportedType(new FullyQualifiedJavaType(UseDataSource.class.getName()));
}
if (StringUtils.equalsAny(this.introspectedTable.context.getTableConfigurations().get(0).getSchema(), DataBaseType.OLD_DATA_SOURCE.getSchemaName(), DataBaseType.SBT_DATA_SOURCE.getSchemaName())) {
interfaze.addImportedType(new FullyQualifiedJavaType(DataBaseType.class.getName()));
interfaze.addImportedType(new FullyQualifiedJavaType(UseDataSource.class.getName()));
}
}

/**
* 添加类注释
*
* @param interfaze
*/
private void addModelClassComment(Interface interfaze) {
interfaze.addJavaDocLine("/**");
if (StringUtils.isNotBlank(introspectedTable.getRemarks())) {
interfaze.addJavaDocLine(" * <p>" + introspectedTable.getRemarks() + "</p>");
} else {
interfaze.addJavaDocLine(" * <p>" + introspectedTable.getFullyQualifiedTable() + "</p>");
}
interfaze.addJavaDocLine(" *");
interfaze.addJavaDocLine(" * @author " + getContext().getCommentGeneratorConfiguration().getProperty("author"));
interfaze.addJavaDocLine(" * @create: " + DateUtil.getCurrDate(DateUtil.YYYY_MM_DD_HH_MM_SS));
interfaze.addJavaDocLine(" */");
if (StringUtils.equals(DataBaseType.OLD_DATA_SOURCE.getSchemaName(), this.introspectedTable.context.getTableConfigurations().get(0).getSchema())) {
interfaze.addJavaDocLine("@UseDataSource(source = DataBaseType.OLD_DATA_SOURCE)");
} else if (StringUtils.equals(DataBaseType.SBT_DATA_SOURCE.getSchemaName(), this.introspectedTable.context.getTableConfigurations().get(0).getSchema())) {
interfaze.addJavaDocLine("@UseDataSource(source = DataBaseType.SBT_DATA_SOURCE)");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.mybatis.generator.api;


import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.XmlConstants;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.ResultMapWithBLOBsElementGenerator;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.ResultMapWithoutBLOBsElementGenerator;

import java.util.ArrayList;
import java.util.List;

/**
* <p>类说明</p>
*
* @author 张峰 [email protected]
* @createTime: 2018/5/7 13:56
*/
public class XmlMappperGeneratorPlugin extends AbstractGeneratorPlugin {


@Override
public List<GeneratedXmlFile> contextGenerateAdditionalXmlFiles(IntrospectedTable introspectedTable) {
this.introspectedTable = introspectedTable;

Document document = new Document(
XmlConstants.MYBATIS3_MAPPER_PUBLIC_ID,
XmlConstants.MYBATIS3_MAPPER_SYSTEM_ID);
XmlElement mapper = new XmlElement("mapper");
mapper.addAttribute(new Attribute("namespace",
getNameSpace()));
addResultMapWithBLOBsElement(mapper);
addResultMapWithoutBLOBsElement(mapper);
document.setRootElement(mapper);
GeneratedXmlFile gxf = new GeneratedXmlFile(document, properties
.getProperty("fileName", getFailName() + ".xml"),
properties.getProperty("targetPackage"),
properties.getProperty("targetProject"),
false, context.getXmlFormatter());

List<GeneratedXmlFile> answer = new ArrayList<GeneratedXmlFile>(1);
answer.add(gxf);
return answer;
}

/**
* 获取命名空间
*
* @return
*/
private String getNameSpace() {
return properties.getProperty("targetPackage") + "." + getFailName();
}

protected void addResultMapWithoutBLOBsElement(XmlElement parentElement) {
if (introspectedTable.getRules().generateBaseResultMap()) {
AbstractXmlElementGenerator elementGenerator = new ResultMapWithoutBLOBsElementGenerator(false);
initializeAndExecuteGenerator(elementGenerator, parentElement);
}
}

protected void addResultMapWithBLOBsElement(XmlElement parentElement) {
if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
AbstractXmlElementGenerator elementGenerator = new ResultMapWithBLOBsElementGenerator();
initializeAndExecuteGenerator(elementGenerator, parentElement);
}
}

protected void initializeAndExecuteGenerator(
AbstractXmlElementGenerator elementGenerator,
XmlElement parentElement) {
elementGenerator.setContext(context);
elementGenerator.setIntrospectedTable(introspectedTable);
elementGenerator.addElements(parentElement);
}

}
Loading

0 comments on commit a3fe09e

Please sign in to comment.