mybatis代码生成+自定义注解+自定义注释实例

网友投稿 458 2022-09-23


mybatis代码生成+自定义注解+自定义注释实例

目录mybatis代码生成配置文件配置类自定义的lombok注解配置代码注释配置

mybatis代码生成

org.mybatis

mybatis

3.4.4

org.mybatis.generator

mybatis-generator-core

1.3.5

配置文件

在resources文件夹中创建文件mbgConfiguration.xml

PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

connectionURL="jdbc:mysql://localhost:3306/project1?useUnicode=true&useSSL=false&characterEncoding=utf8"

userId="test"

password="123456"/>

targetProject="src/main/java">

connectionURL="jdbc:mysql://localhost:3306/project1?useUnicode=true&useSSL=false&characterEncoding=utf8"

userId="test"

password="123456"/>

targetProject="src/main/java">

targetProject="src/main/java">

配置类

自定义的lombok注解配置

效果:

在类上添加注解

@Data

@AllArgsConstructor

@NoArgsConstructor

@Builder

public class User {

}

在时间字段上自动添加注解

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

@jsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private Date endTime;

代码

import org.mybatis.generator.api.IntrospectedColumn;

import org.mybatis.generator.api.IntrospectedTable;

import org.mybatis.generator.api.Plugin;

import org.mybatis.generator.api.PluginAdapter;

import org.mybatis.generator.api.dom.java.*;

import java.util.*;

/** 自定义的lombok注解配置

* @author jingshiyu

* @date 2019/9/17 12:04:47

* @desc

*/

public class LombokPlugin extends PluginAdapter {

private final Collection annotations;

/**

* LombokPlugin constructor

*/

public LombokPlugin() {

annotations = new LinkedHashSet<>(Annotations.values().length);

}

/**

* @param warnings list of warnings

* @return always true

*/

@Override

public boolean validate(List warnings) {

return true;

}

/**

* Intercepts base record class generation 获取表

*

* @param topLevelClass the generated base record class

* @param introspectedTable The class containing information about the table as

* introspected from the database

* @return always true

*/

@Override

public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

addAnnotations(topLevelClass);

return true;

}

/**

* Intercepts primary key class generation

*

* @param topLevelClass the generated primary key class

* @param introspectedTable The class containing information about the table as

* introspected from the database

* @return always true

*/

@Override

public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

addAnnotations(topLevelClass);

return true;

}

/**

* Intercepts "record with blob" class generation

*

* @param topLevelClass the generated record with BLOBs class

* @param introspectedTable The class containing information about the table as

* introspected from the database

* @return always true

*/

@Override

public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

addAnnotations(topLevelClass);

return true;

}

/**

* 设置get set方法(使用lombok不需要,直接返回false)

*/

@Override

public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {

return false;

}

/**

* 设置set方法

*/

@Override

public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {

return false;

}

/**

* 设置lombok注解

*/

private void addAnnotations(TopLevelClass topLevelClass) {

for (Annotations annotation : annotations) {

topLevelClass.addImportedType(annotation.javaType);

topLevelClass.addAnnotation(annotation.asAnnotation());

}

}

/**

* entity类设置

* @param properties

*/

@Override

public void setProperties(Properties properties) {

super.setProperties(properties);

//@Data is default annotation

annotations.add(Annotations.DATA);

annotations.add(Annotations.ALL_ARGS_CONSTRUCTOR);

annotations.add(Annotations.NO_ARGS_CONSTRUCTOR);

annotations.add(Annotations.BUILDER);

for (String annotationName : properties.stringPropertyNames()) {

if (annotationName.contains(".")) {

continue;

}

String value = properties.getProperty(annotationName);

if (!Boolean.parseBoolean(value)) {

// The annotation is disabled, skip it

continue;

}

Annotations annotation = Annotations.getValueOf(annotationName);

if (annotation == null) {

continue;

}

String optionsPrefix = annotationName + ".";

for (String propertyName : properties.stringPropertyNames()) {

if (!propertyName.startsWith(optionsPrefix)) {

// A property not related to this annotation

continue;

}

String propertyValue = properties.getProperty(propertyName);

annotation.appendOptions(propertyName, propertyValue);

annotations.add(annotation);

annotations.addAll(Annotations.getDependencies(annotation));

}

}

}

/**

* mapper类设置注解

*/

@Override

public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper"));

interfaze.addAnnotation("@Mapper");

return true;

}

/**

* entity字段设置

*/

@Override

public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, Plugin.ModelClassType modelClassType) {

if (field.getType().getShortNameWithoutTypeArguments().equals("Date")) {

field.getAnnotations().add(Annotations.DATE_TIME_FORMAT.asAnnotation());

field.getAnnotations().add(Annotations.JSON_FORMAT.asAnnotation());

topLevelClass.addImportedType(Annotations.DATE_TIME_FORMAT.javaType);

topLevelClass.addImportedType(Annotations.JSON_FORMAT.javaType);

}

return true;

}

private enum Annotations {

DATA("data", "@Data", "lombok.Data"),

BUILDER("builder", "@Builder", "lombok.Builder"),

ALL_ARGS_CONSTRUCTOR("allArgsConstructor", "@AllArgsConstructor", "lombok.AllArgsConstructor"),

NO_ARGS_CONSTRUCTOR("noArgsConstructor", "@NoArgsConstructor", "lombok.NoArgsConstructor"),

ACCESSORS("accessors", "@Accessors", "lombok.experimental.Accessors"),

TO_STRING("toString", "@ToString", "lombok.ToString"),

DATE_TIME_FORMAT("dateTimeFormat", "@DateTimeFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")", "org.springframework.format.annotation.DateTimeFormat"),

JSON_FORMAT("jsonFormat", "@JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")", "com.fasterxml.jackson.annotation.JsonFormat");

private final String paramName;

private final String name;

private final FullyQualifiedJavaType javaType;

private final List options;

Annotations(String paramName, String name, String className) {

this.paramName = paramName;

this.name = name;

this.javaType = new FullyQualifiedJavaType(className);

this.options = new ArrayList();

}

private static Annotations getValueOf(String paramName) {

for (Annotations annotation : Annotations.values()) {

if (String.CASE_INSENSITIVE_ORDER.compare(paramName, annotation.paramName) == 0) {

return annotation;

}

}

return null;

}

private static Collection getDependencies(Annotations annotation) {

if (annotation == ALL_ARGS_CONSTRUCTOR) {

return Collections.singleton(NO_ARGS_CONSTRUCTOR);

} else {

return Collections.emptyList();

}

}

// A trivial quoting.

// Because Lombok annotation options type is almost String or boolean.

private static String quote(String value) {

if (Boolean.TRUE.toString().equals(value) || Boolean.FALSE.toString().equals(value))

// case of boolean, not passed as an array.

{

return value;

}

return value.replaceAll("[\\\\w]+", "\"$0\"");

}

private void appendOptions(String key, String value) {

String keyPart = key.substring(key.indexOf(".") + 1);

String valuePart = value.contains(",") ? String.format("{%s}", value) : value;

this.options.add(String.format("%s=%s", keyPart, quote(valuePart)));

}

private String asAnnotation() {

if (options.isEmpty()) {

return name;

}

StringBuilder sb = new StringBuilder();

sb.append(name);

sb.append("(");

boolean first = true;

for (String option : options) {

if (first) {

first = false;

} else {

sb.append(", ");

}

sb.append(option);

}

sb.append(")");

return sb.toString();

}

}

}

代码注释配置

代码生成时,使用数据控的注释给字段添加文档注释

效果

/**

* 编号

*/

private String planId;

代码

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import org.mybatis.generator.api.CommentGenerator;

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.XmlElement;

import org.mybatis.generator.config.MergeConstants;

import org.mybatis.generator.config.PropertyRegistry;

import org.mybatis.generator.internal.util.StringUtility;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Properties;

import static org.mybatis.generator.internal.util.StringUtility.isTrue;

/**

* mybatis逆向工程默认的注释修改(使用表的注释)

*

* @author jingshiyu

* @date 2019/7/17 17:39:36

* @desc

*/

public class MyCommentGenerator implements CommentGenerator {

/**

* The properties.

*/

private Properties properties;

/**

* The suppress date.

*/

private boolean suppressDate;

/**

* The suppress all comments.

*/

private boolean suppressAllComments;

/**

* 是否添加doc注释,true:不添加,false:添加

* The addition of table remark's comments.

* If suppressAllComments is true, this option is ignored

*/

private boolean addRemarkComments;

private SimpleDateFormat dateFormat;

public MyCommentGenerator() {

super();

properties = new Properties();

suppressDate = false;

suppressAllComments = false;

addRemarkComments = false;

}

@Override

public void addJavaFileComment(CompilationUnit compilationUnit) {

}

/**

* 实体类对应的mapper.xml注释,mapper类不加注释,如有需要参考 DefaultCommentGenerator

*/

@Override

public void addComment(XmlElement xmlElement) {

if (suppressAllComments) {

return;

}

}

@Override

public void addRootComment(XmlElement rootElement) {

}

@Override

public void addConfigurationProperties(Properties properties) {

this.properties.putAll(properties);

suppressDate = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));

suppressAllComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));

addRemarkComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_ADD_REMARK_COMMENTS));

String dateFormatString = properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_DATE_FORMAT);

if (StringUtility.stringHasValue(dateFormatString)) {

dateFormat = new SimpleDateFormat(dateFormatString);

}

}

protected void addJavadocTag(JavaElement javaElement, boolean markAsDohttp://NotDelete) {

javaElement.addJavaDocLine(" *");

StringBuilder sb = new StringBuilder();

sb.append(" * ");

sb.append(MergeConstants.NEW_ELEMENT_TAG);

if (markAsDoNotDelete) {

sb.append(" do_not_delete_during_merge");

}

String s = getDateString();

if (s != null) {

sb.append(' ');

sb.append(s);

}

javaElement.addJavaDocLine(sb.toString());

}

protected String getDateString() {

if (suppressDate) {

return null;

} else if (dateFormat != null) {

return dateFormat.format(new Date());

} else {

return new Date().toString();

}

}

/**

* 设置class的注解

*/

@Override

public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {

if (suppressAllComments) {

return;

}

StringBuilder sb = new StringBuilder();

innerClass.addJavaDocLine("/**");

// sb.append(" * This class corresponds to the database table ");

sb.append(introspectedTable.getFullyQualifiedTable());

innerClass.addJavaDocLine(sb.toString());

addJavadocTag(innerClass, false);

innerClass.addJavaDocLine(" */");

}

/**

* 方法注释

*/

@Override

public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

if (suppressAllComments || !addRemarkComments) {

return;

}

StringBuilder sb = new StringBuilder();

topLevelClass.addJavaDocLine("/**");

//设置数据库的备注

String remarks = introspectedTable.getRemarks();

if (addRemarkComments && StringUtility.stringHasValue(remarks)) {

topLevelClass.addJavaDocLine(" * Database Table Remarks:");

String[] remarkLines = remarks.split(System.getProperty("line.separator"));

for (String remarkLine : remarkLines) {

topLevelClass.addJavaDocLine(" * " + remarkLine);

}

}

topLevelClass.addJavaDocLine(" *");

sb.append(introspectedTable.getFullyQualifiedTable());

topLevelClass.addJavaDocLine(sb.toString());

addJavadocTag(topLevelClass, true);

topLevelClass.addJavaDocLine(" */");

}

/**

* 添加枚举的注释

*/

@Override

public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {

if (suppressAllComments) {

return;

}

StringBuilder sb = new StringBuilder();

innerEnum.addJavaDocLine("/**");

sb.append(introspectedTable.getFullyQualifiedTable());

innerEnum.addJavaDocLine(sb.toString());

addJavadocTag(innerEnum, false);

innerEnum.addJavaDocLine(" */");

}

/**

* 实体类字段注释

*/

@Override

public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {

if (suppressAllComments || StringUtils.isEmpty(introspectedColumn.getRemarks())) {

return;

}

field.addJavaDocLine("/**");

StringBuilder sb = new StringBuilder();

// introspectedColumn.getRemarks() 就是获取字段注释

sb.append(" * " + introspectedColumn.getRemarks());

field.addJavaDocLine(sb.toString());

field.addJavaDocLine(" */");

}

/**

* 实体类的静态字段

*/

@Override

public void addFieldComment(Field field, IntrospectedTable introspectedTable) {

if (suppressAllComments) {

return;

}

}

/**

* 实体类toString方法

*/

@Override

public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {

if (suppressAllComments) {

return;

}

}

/**

* 实体类getter方法注释

*/

@Override

public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {

if (suppressAllComments) {

return;

}

}

/**

* 实体类setter注释

*/

@Override

public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {

if (suppressAllComments) {

return;

}

}

/**

* 类注释

* @param innerClass

* @param introspectedTable

* @param markAsDoNotDelete

*/

@Override

public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {

if (suppressAllComments) {

return;

}

StringBuilder sb = new StringBuilder();

innerClass.addJavaDocLine("/**");

sb.append(introspectedTable.getFullyQualifiedTable());

innerClass.addJavaDocLine(sb.toString());

addJavadocTag(innerClass, markAsDoNotDelete);

innerClass.addJavaDocLine(" */");

}

}

调用

import org.mybatis.generator.api.MyBatisGenerator;

import org.mybatis.generator.config.Configuration;

import org.mybatis.generator.config.xml.ConfigurationParser;

import org.mybatis.generator.exception.InvalidConfigurationException;

import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;

import java.util.ArrayList;

import java.util.List;

/**

* java代码生成器(会生成example的entity)

* mybatis逆向工程

*

* @author jingshiyu

* @date 2019/7/17 17:24

* @desc java代码生成器(会生成example的entity)

*/

public class JavaExampleGenerator {

public static void main(String[] args) {

List warnings = new ArrayList();

boolean overwrite = true;

//如果这里出现空指针,直接写绝对路径即可。

String genCfg = "/mbgConfiguration.xml";

File configFile = new File(JavaExampleGenerator.class.getResource(genCfg).getFile());

ConfigurationParser cp = new ConfigurationParser(warnings);

Configuration config = null;

try {

config = cp.parseConfiguration(configFile);

} catch (Exception e) {

e.printStackTrace();

}

DefaultShellCallback callback = new DefaultShellCallback(overwrite);

MyBatisGenerator myBatisGenerator = null;

try {

myBatisGenerator = new MyBatisGenerator(config, callback, warnings);

} catch (InvalidConfigurationException e) {

e.printStackTrace();

}

try {

myBatisGenerator.generate(null);

} catch (Exception e) {

e.printStackTrace();

}

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:路由总结之静态、RIP、OSPF、IS-IS、BGP、策略路由和路由策略(ospf 静态路由优先级)
下一篇:实际工作网络架构案例说明(网络架构方案)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~