一、MyBatis Generator是什么?
MyBatis Generator (MBG) 是 MyBatis 的代码生成器。它能够根据数据库表,自动生成 java 实体类、dao 层接口(mapper 接口)及mapper.xml文件。这大大减少了我们建立实体类、dao 接口及 mapper 映射文件的繁琐任务,可以节省很多时间,专注于业务层代码的开发。
二、Mybatis Generator 的maven插件使用
1.依赖引入
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<!--引入mysql连接器 版本号需与添加的依赖一致-->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
</dependencies>
<!--添加扫描generatorConfig.xml文件-->
<configuration>
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
2.配置generatorConfig.xml文件
在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>
<!-- 连接数据库jar包的路径-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接信息:驱动类、链接地址、用户名、密码 -->
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mall? useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC"
userId="root"
password="password">
<!--高版本的 mysql-connector-java 需要设置 nullCatalogMeansCurrent=true-->
<!--解决mysql驱动升级到8.0后不生成指定数据库代码的问题-->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<javaTypeResolver>
<!--类型解析器-->
<!-- 默认false,把jdbc decimal 和 numeric 类型解析为integer -->
<!-- true,把jdbc decimal 和 numeric 类型解析为java.math.bigdecimal-->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 实体类的包名和存放路径 -->
<javaModelGenerator targetPackage="com.example.springboot.pojo" targetProject="src/main/java">
<!-- 是否让schema作为包后缀 默认是false会在 po 目录下在创建一个 “数据库名” 的文件夹,生成 的 po 会放在该文件夹下,也就是说会多一层目录
-->
<property name="enableSubPackages" value="true"/>
<!-- 从数据库返回的值被清理前后的空格-->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件*.xml的位置-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成Mapper接口的包名和位置
type="XMLMAPPER" 会将接口的实现放在 mapper.xml中,也推荐这样配置。
type="ANNOTATEDMAPPER",接口的实现通过注解写在接口上面
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.springboot.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- tableName:数据库中的表名或视图名;domainObjectName:生成的实体类的类名-->
<table tableName="user_base_info" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>