一、pom.xml添加插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<!--mybatis-generator-maven插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<!--依赖数据库连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<!--依赖模板-->
<dependency>
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
注意放到插件 <plugins>里,别直接放到<dependencies>
二、添加配置文件
在resources下创建配置文件generatorConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<!-- 在pom.xml中使用了maven插件,所以不用使用eclipse插件, 可以直接cmd到service执行
"mvn -Dmybatis.generator.configurationFile=/Users/fangxm/Documents/git_vcb/vcb-project/framework/src/main/resources/generatorConfig.xml -Dmybatis.generator.overwrite=true mybatis-generator:generate"
命令生成MyBatis文件 -->
<!--<classPathEntry location="D:/registry/mysql/mysql-connector-java/5.1.22/mysql-connector-java-5.1.22.jar"/>-->
<context id="context1" targetRuntime="MyBatis3">
<plugin type="com.itfsw.mybatis.generator.plugins.CommentPlugin">
<!-- 自定义模板路径 -->
<property name="template" value="src/main/resources/mybatis-generator-comment.ftl" />
</plugin>
<commentGenerator>
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8" userId="root"
password="123456"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="edu.duan.springboot.entity" targetProject="./src/main/java"/>
<sqlMapGenerator targetPackage="edu.duan.springboot.dao" targetProject="./src/main/resources"/>
<javaClientGenerator targetPackage="edu.duan.springboot.dao" targetProject="./src/main/java" type="MIXEDMAPPER"/>
<table tableName="matched" domainObjectName="Matched" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="connection" domainObjectName="Connection" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="user" domainObjectName="User" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<generatedKey column="uid" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>
以及 mybatis-generator-comment.ftl
<?xml version="1.0" encoding="UTF-8"?>
<template>
<!-- #############################################################################################################
/**
* This method is called to add a file level comment to a generated java file. This method could be used to add a
* general file comment (such as a copyright notice). However, note that the Java file merge function in Eclipse
* does not deal with this comment. If you run the generator repeatedly, you will only retain the comment from the
* initial run.
* <p>
*
* The default implementation does nothing.
*
* @param compilationUnit
* the compilation unit
*/
-->
<comment ID="addJavaFileComment"></comment>
<!-- #############################################################################################################
/**
* This method should add a suitable comment as a child element of the specified xmlElement to warn users that the
* element was generated and is subject to regeneration.
*
* @param xmlElement
* the xml element
*/
-->
<comment ID="addComment"><![CDATA[
<!--
WARNING - ${mgb}
This element is automatically generated by MyBatis Generator, do not modify.
@project https://github.com/itfsw/mybatis-generator-plugin
-->
]]></comment>
<!-- #############################################################################################################
/**
* This method is called to add a comment as the first child of the root element. This method could be used to add a
* general file comment (such as a copyright notice). However, note that the XML file merge function does not deal
* with this comment. If you run the generator repeatedly, you will only retain the comment from the initial run.
* <p>
*
* The default implementation does nothing.
*
* @param rootElement
* the root element
*/
-->
<comment ID="addRootComment"></comment>
<!-- #############################################################################################################
/**
* This method should add a Javadoc comment to the specified field. The field is related to the specified table and
* is used to hold the value of the specified column.
* <p>
*
* <b>Important:</b> This method should add a the nonstandard JavaDoc tag "@mbg.generated" to the comment. Without
* this tag, the Eclipse based Java merge feature will fail.
*
* @param field
* the field
* @param introspectedTable
* the introspected table
* @param introspectedColumn
* the introspected column
*/
-->
<comment ID="addFieldComment"><![CDATA[
<#if introspectedColumn??>
<#if introspectedColumn.remarks?? && introspectedColumn.remarks != ''>
/**
<#list introspectedColumn.remarks?split("\n") as remark>
* ${remark}
</#list>
*/
<#else>
/**
*
*/
</#if>
</#if>
]]></comment>
<!-- #############################################################################################################
/**
* Adds a comment for a model class. The Java code merger should
* be notified not to delete the entire class in case any manual
* changes have been made. So this method will always use the
* "do not delete" annotation.
*
* Because of difficulties with the Java file merger, the default implementation
* of this method should NOT add comments. Comments should only be added if
* specifically requested by the user (for example, by enabling table remark comments).
*
* @param topLevelClass
* the top level class
* @param introspectedTable
* the introspected table
*/
-->
<comment ID="addModelClassComment"><![CDATA[
/**
<#if introspectedTable.remarks?? && introspectedTable.remarks != ''>
<#list introspectedTable.remarks?split("\n") as remark>
* ${remark}
</#list>
</#if>
*/
]]></comment>
<!-- #############################################################################################################
/**
* Adds the inner class comment.
*
* @param innerClass
* the inner class
* @param introspectedTable
* the introspected table
* @param markAsDoNotDelete
* the mark as do not delete
*/
-->
<comment ID="addClassComment"/>
<!-- #############################################################################################################
/**
* Adds the enum comment.
*
* @param innerEnum
* the inner enum
* @param introspectedTable
* the introspected table
*/
-->
<comment ID="addEnumComment"/>
<!-- #############################################################################################################
/**
* Adds the interface comment.
*
* @param innerInterface
* the inner interface
* @param introspectedTable
* the introspected table
*/
-->
<comment ID="addInterfaceComment"/>
<!-- #############################################################################################################
/**
* Adds the getter comment.
*
* @param method
* the method
* @param introspectedTable
* the introspected table
* @param introspectedColumn
* the introspected column
*/
-->
<comment ID="addGetterComment"/>
<!-- #############################################################################################################
/**
* Adds the setter comment.
*
* @param method
* the method
* @param introspectedTable
* the introspected table
* @param introspectedColumn
* the introspected column
*/
-->
<comment ID="addSetterComment"/>
<!-- #############################################################################################################
/**
* Adds the general method comment.
*
* @param method
* the method
* @param introspectedTable
* the introspected table
*/
-->
<comment ID="addGeneralMethodComment"/>
</template>
其中,generatorConfig.xml 中的mapper路径、表实体类路径、数据库用户密码,以及各个表名主键需要自己修改设置
mybatis-generator-comment.ftl 是为了生成实体类的时候注释规范,是否使用的差别如下两图
三、配置完成后运行插件
目前只知道idea的运行方式
打开maven project,找到generator运行