1. 什么是MyBatis Generator?
MyBatis Generator 是 MyBatis 提供的一个代码生成工具。可以帮我们生成 表对应的持久化对象(po)、操作数据库的接口(dao)、CRUD sql的xml(mapper)。
集成MyBatis Generator前提是,springboot项目中已经集成了MyBatis。至于如何集成mybatis,请查看: SpringBoot学习(三):集成Mybatis
2. 添加插件
修改pom.xml
文件,添加编译插件
|
配置说明:
configurationFile: mybatis的代码生成器的配置文件
overwrite : 允许覆盖生成的文件,需要注意的是: MyBatis Generator 只会覆盖旧的 po、dao,对于 *mapper.xml 不会覆盖,而是追加,这样做的目的是防止用户自己写的 sql 语句一不小心都被 MyBatis Generator 给覆盖了。
verbose: 当设置true时,控制台会输出
mvn mybatis-generator:generate -e
具体过程
3. 添加配置
3.1 MyBatis Generator 生成器配置
MyBatis Generator 插件启动后,会根据你在 pom.xml
中配置的路径(src/main/resources/mybatis-generator/config.xml
)找到该配置文件。这个配置文件才是详细都配置 MyBatis Generator 生成代码的各种细节。
config.xml
配置详情
|
3.2 外入配置文件
<properties resource="mybatis-generator/generatorjdbc.properties"/>
**配置文件在: ** /src/main/resources/mybatis-generator/generatorjdbc.properties
**配置详情: **
|
踩坑点: 本来是打算复用application.yaml
,但是一直会报错: generate failed: Exception getting JDBC Driver
,看网上很多教程都是基于application.properties
,无奈只能单独写一个文件(generatorjdbc.properties)引入。
4. 运行效果
问题: 如上图所示生成,虽然去除表前缀但是生成的文件大小写还是有问题,比如:
micro_user_information
去除后生成UserinformationModel
,解决方法,以及同步到 [3.1 MyBatis Generator 生成器配置](#3.1 MyBatis Generator 生成器配置)
5.优化生成器
生成器代码是很方便,但是我想做些定制化修改,比如:
- dao目录下面的类后缀都是Dao、model目录下面的类后缀都是Model,
- 生成的使用示例代码,想单独放在另外一个目录中,而不是和dao的其他文件混着放。
- 注释很多无用的英文,能不能优化
- model(实体类)中,支持注解lombok注解等等。
像上面这些需求,以前的大佬早就有成熟的轮子,这里只需要会使用就行;我采用的是itfsw/mybatis-generator-plugin。
5.1 插件中添加依赖
|
注意点:
- mybatis代码生成器版本不能太高,不然无法正常使用
itfsw.plugin
。
5.2 优化配置
修改生成器配置src/main/resources/mybatis-generator/config.xml
|
5.3 自定义注释
- 需要修改外入配置文件
/src/main/resources/mybatis-generator/generatorjdbc.properties
|
在生成器配置中引入配置文件
src/main/resources/mybatis-generator/config.xml
<!-- 自定义注释插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.CommentPlugin">
<!-- 自定义模板路径 -->
<property name="template" value="${mybatis.comment.file}"/>
</plugin>定义注释模板
src/main/resources/mybatis-generator/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.
*
* The default implementation does nothing.
*
* @param compilationUnit
* the compilation unit
*/
-->
<comment ID="addJavaFileComment"><![CDATA[
/**
* @author : Mr.Liuqh
* @date : ${.now?string["yyyy-MM-dd HH:mm:ss"]}
*/
]]>
</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.
*
* 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.
*
* <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>
</#if>
*
* 表字段:: ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
*/
<#else>
</#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>
*
* 表名: ${introspectedTable.fullyQualifiedTable}
*/
]]>
</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"><![CDATA[
/****
*
* ${introspectedTable.fullyQualifiedTable}
*
* ${mgb}<#if markAsDoNotDelete?? && markAsDoNotDelete> do_not_delete_during_merge</#if>
*/
]]>
</comment>
<!-- #############################################################################################################
/**
* Adds the enum comment.
*
* @param innerEnum
* the inner enum
* @param introspectedTable
* the introspected table
*/
-->
<comment ID="addEnumComment"><![CDATA[
/***
*
* ${introspectedTable.fullyQualifiedTable}
*
*/
]]>
</comment>
<!-- #############################################################################################################
/**
* Adds the interface comment.
*
* @param innerInterface
* the inner interface
* @param introspectedTable
* the introspected table
*/
-->
<comment ID="addInterfaceComment"><![CDATA[
/***
*
* ${introspectedTable.fullyQualifiedTable}
*/
]]>
</comment>
<!-- #############################################################################################################
/**
* Adds the getter comment.
*
* @param method
* the method
* @param introspectedTable
* the introspected table
* @param introspectedColumn
* the introspected column
*/
-->
<comment ID="addGetterComment"><![CDATA[
<#if introspectedColumn??>
/**
* 表字段: ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
*
* @return the value of ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
*/
<#else>
/**
* field ${method.name?replace("get","")?replace("is", "")?uncap_first}
*
* @return the value of field ${method.name?replace("get","")?replace("is", "")?uncap_first}
*
*/
</#if>
]]>
</comment>
<!-- #############################################################################################################
/**
* Adds the setter comment.
*
* @param method
* the method
* @param introspectedTable
* the introspected table
* @param introspectedColumn
* the introspected column
*/
-->
<comment ID="addSetterComment"><![CDATA[
<#if introspectedColumn??>
/**
* 表字段: ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
*
* @param ${method.parameters[0].name} the value for ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
*/
<#else>
/**
*
* field ${method.name?replace("set","")?uncap_first}
*
* @param ${method.parameters[0].name} the value for field ${method.name?replace("set","")?uncap_first}
*
*/
</#if>
]]>
</comment>
<!-- #############################################################################################################
/**
* Adds the general method comment.
*
* @param method
* the method
* @param introspectedTable
* the introspected table
*/
-->
<comment ID="addGeneralMethodComment">
</comment>
</template>
5.4 优化后生成效果
6.完整配置
6.1 pom.xml
|
7. 踩坑记录
7.1 操作Dao方法时报错: Invalid bound statement (not found)
|
解决方法:
修改application.yaml
关于mybatis配置,如下
|
7.2 报错: …bean of type ‘com.hui.javalearn.dao.UserDao’ that could not be found
在一个新的工程中,用MyBatis Gennrator自动生成DAO,Example,Mapper以及对应的XML后,然后使用自动装载的方式加载Mapper,在执行时报错。
|
解决方法: 在启动类中添加Mapper扫描路径,具体如下
|