MyBatis Generator 代码生成工具

 本文完整代码已上传Github仓库:https://github.com/ouyangyewei/mybatis-codegen

1、MBG 的简介

 MyBatis Generator官网:MyBatis Generator Core – Introduction to MyBatis Generator

借助MyBatis Generator(MBG)工具,可以基于数据库表结构自动生成Bean/Mapper/Mapper XML代码,简化了大量重复繁琐的开发步骤,相关信息如下:

2、MGB 自定义插件

MGB支持自定义插件,比如:自动生成代码时带上表/表字段注释、分页、Lombok

2.1、注释插件

package com.github.codegen;

import org.apache.logging.log4j.util.Strings;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * 注释生成器
 * 

* 根据元数据表的字段注释给JavaBean字段添加注释 *

* @author ouyangyewei * @date 2021-09-01 **/ public class CommentGenerator extends DefaultCommentGenerator { private boolean addRemarkComments = false; /** * 设置用户配置的参数 * @param properties */ @Override public void addConfigurationProperties(Properties properties) { super.addConfigurationProperties(properties); this.addRemarkComments = StringUtility.isTrue(properties.getProperty("addRemarkComments")); } /** * 根据表注释设置类文件注释 * @param topLevelClass * @param introspectedTable */ private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { if (this.addRemarkComments) { topLevelClass.addJavaDocLine("/**"); topLevelClass.addJavaDocLine(" * " + introspectedTable.getRemarks()); topLevelClass.addJavaDocLine(" * @author MyBatis Generator"); topLevelClass.addJavaDocLine(" * @date " + DATE_FORMAT.format(new Date())); topLevelClass.addJavaDocLine(" */"); } } /** * 给字段添加注释 * @param field * @param introspectedTable * @param introspectedColumn */ @Override public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { String remarks = introspectedColumn.getRemarks(); // 根据参数和备注信息判断是否添加备注信息 if (this.addRemarkComments && StringUtility.stringHasValue(remarks)) { // 数据库中特殊字符需要转义 if (remarks.contains("\"")) { remarks = remarks.replace("\"","'"); } // model的字段添加注解 field.addJavaDocLine("/**" + Strings.LINE_SEPARATOR + " * " + remarks + Strings.LINE_SEPARATOR + " */"); } } }

2.2、分页插件

package com.github.codegen;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.List;

/**
 * Mybatis Page Limit Plugin
 * 

* 用于为每个Example类添加offset和limit属性方法 *

* @author ouyangyewei * @date 2021-08-31 **/ public class LimitPlugin extends PluginAdapter { @Override public boolean validate(List list) { return true; } /** * 为每个Example类添加offset和rows属性已经set、get方法 * @param topLevelClass * @param introspectedTable * @return */ @Override public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper(); Field limit = new Field("limit", integerWrapper); limit.setVisibility(JavaVisibility.PRIVATE); topLevelClass.addField(limit); Method setLimit = new Method("setLimit"); setLimit.setVisibility(JavaVisibility.PUBLIC); setLimit.addParameter(new Parameter(integerWrapper, "limit")); setLimit.addBodyLine("this.limit = limit;"); topLevelClass.addMethod(setLimit); Method getLimit = new Method("getLimit"); getLimit.setVisibility(JavaVisibility.PUBLIC); getLimit.setReturnType(integerWrapper); getLimit.addBodyLine("return limit;"); topLevelClass.addMethod(getLimit); Field offset = new Field("offset", integerWrapper); offset.setVisibility(JavaVisibility.PRIVATE); topLevelClass.addField(offset); Method setOffset = new Method("setOffset"); setOffset.setVisibility(JavaVisibility.PUBLIC); setOffset.addParameter(new Parameter(integerWrapper, "offset")); setOffset.addBodyLine("this.offset = offset;"); topLevelClass.addMethod(setOffset); Method getOffset = new Method("getOffset"); getOffset

你可能感兴趣的:([问题记录],mybatis,java,mysql)