MyBatis Generator (MBG) 使用说明文档,涵盖了使用方式、配置详解、Maven 插件集成、常见问题与优化建议,非常适合作为项目文档或学习笔记保存:
🧩 MyBatis Generator 使用说明(完整实用版)
MyBatis Generator(简称 MBG)是官方提供的代码生成工具,用于根据数据库表自动生成 MyBatis 所需的实体类、Mapper 接口和 XML 映射文件,极大减少了重复工作。
✅ 一、基本功能
MBG 支持生成以下内容:
-
实体类(Model/Entity)
-
Mapper 接口(可选注解方式或 XML)
-
SQL 映射文件(*.xml)
-
条件查询构造器(Example 类,MyBatis3 模式)
-
动态 SQL(配合 MyBatis Dynamic SQL 使用)
⚙️ 二、常用生成模式(
targetRuntime
)
|
targetRuntime |
描述 |
|---|---|
|
MyBatis3 |
默认,生成 XML + Mapper 接口 + Example 类 |
|
MyBatis3Simple |
简化版,无 Example |
|
MyBatis3DynamicSQL |
不生成 XML,生成链式 Java 动态 SQL(MyBatis 3.4.2+) |
|
MyBatis3Kotlin |
Kotlin 版本,需搭配 MyBatis Dynamic SQL Kotlin DSL |
📄 三、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>
<context id="MyBatis3Context" targetRuntime="MyBatis3">
<!-- 去除注释与时间戳 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- JDBC连接 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.199.145:3306/insound"
userId="insound"
password="123456"/>
<!-- 类型转换 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 实体类路径 -->
<javaModelGenerator targetPackage="org.goodday1.mybatis.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- XML 文件路径 -->
<sqlMapGenerator targetPackage="org.goodday1.mybatis.mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- Mapper 接口路径 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="org.goodday1.mybatis.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 生成哪张表 -->
<table tableName="t_target" domainObjectName="Target"
enableCountByExample="true"
enableUpdateByExample="true"
enableDeleteByExample="true"
enableSelectByExample="true"
selectByExampleQueryId="true"/>
</context>
</generatorConfiguration>
📌 注意:
-
targetRuntime 是最关键参数(决定生成代码风格)
-
domainObjectName 控制实体类名(不使用数据库表名原样)
-
可设置多个 <table> 元素
🧪 四、Maven 插件集成方式
在 pom.xml 中添加以下插件配置:
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.1</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite> <!-- 是否覆盖旧文件 -->
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
📦 执行命令:
mvn mybatis-generator:generate
✅ 五、常见问题与解决方案
|
问题描述 |
原因分析与解决 |
|---|---|
|
Result Maps collection already contains value |
同一个 resultMap id 被重复定义,可能在多个 XML 或重复生成 |
|
插件找不到 |
<plugin> 内未指定数据库驱动 |
|
连接失败 |
JDBC URL 拼写错误、未开启远程连接、防火墙限制 |
|
注解太多太丑 |
设置 <context suppressJavaModelAnnotations="true"/> 去除 |
|
生成的字段含 @Generated(...) 注解 |
这是 MyBatis 用于追踪源代码生成器的注解,可配置关闭 |
|
缺少表 |
<table tableName="xxx"/> 忘记配置或拼写错误 |
📚 六、总结理解
|
组件 |
作用说明 |
|---|---|
|
generatorConfig.xml |
配置所有生成规则 |
|
MyBatis3 |
默认 XML + Mapper 接口 + Example 类 |
|
MyBatis3DynamicSQL |
动态 SQL 模式,适合现代链式调用,无 XML |
|
maven-plugin |
支持通过 mvn 命令直接运行生成 |
|
table 节点 |
指定每一张要生成的表,支持高级配置 |
浙公网安备 33010602011771号