MyBatis Generator是 MyBatis的代码生成器。根据配置文件为所有版本的 MyBatis 生成代码。它只需要很少量的简单配置就可以完成大量的数据库表到Java对象的生成工作以及生成Mapper接口文件,Mapper XML配置文件的生成,拥有零出错和速度快的优点,让开发人员解放出来更专注于业务逻辑的开发。避免程序员繁琐配置XML文件和重复生成对象操作
1. MyBatis Generator好处
1)快速,项目配置完只需一个命令或者点击一下generate就可以创建完成
2)简单,只需要简单少量的配置信息
3)高效,如果数据库表字段非常多,使用MyBatis Generator可以提高开发效率
2. MyBatis Generator缺点
1)使用MyBatis Generator可能会由于不使用导致我们遗忘一些技术点
2)MyBatis Generator生成的代码包含了很多不需要的代码和方法,给我们带来了不便
3. MyBatis Generator主要功能
1)生成与数据库表对应的实体类,如User
2)生成实体类对应的操作数据库接口方法,如UserMapper
3)生成UserMapper接口方法的XML映射文件,如UserMapper.XML
**
**
1. 配置MyBatis Generator前的准备工作
编译器环境:IDEA 2020.3
JDK版本信息:JDK1.8
数据库版本信息:mysql8.0
SpringBoot项目版本信息:2.3.7.RELEASE
2.配置pom.xml
我们需要在pom.xml中配置mybatis-spring-boot-starter的mybatis依赖以及maven下mybatis-generator的生成插件,下面依赖或插件前加注解的是需要导入的,导入后需要重新下载依赖,pom.xml配置代码如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.1version>
<relativePath/>
parent>
<groupId>com.hebeugroupId>
<artifactId>miniprogramartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>miniprogramname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASEspring-boot.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.3version>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.1version>
<configuration>
<source>1.8source>
<target>1.8target>
<encoding>UTF-8encoding>
configuration>
plugin>
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>1.4.0version>
<dependencies>
<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-coreartifactId>
<version>1.4.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
dependencies>
<executions>
<execution>
<id>mybatis generatorid>
<phase>packagephase>
<goals>
<goal>generategoal>
goals>
execution>
executions>
<configuration>
<verbose>trueverbose>
<overwrite>trueoverwrite>
<configurationFile>
src/main/resources/generatorConfig.xml
configurationFile>
configuration>
plugin>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>2.3.7.RELEASEversion>
<configuration>
<mainClass>com.hebeu.MiniprogramApplicationmainClass>
configuration>
<executions>
<execution>
<id>repackageid>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
3.在src/main/resources下新建generatorConfig.xml
在src/main/resources下新建的generatorConfig.xml文件是mybatis-generator的核心配置文件,该配置文件告诉mybatis-generator:
1)如何连接到数据库
2)要生成什么对象,以及如何生成它们
3)应该使用哪些表来生成对象
如果想了解xml详细配置参考: MyBatis 生成器中文官网
generatorConfig.xml代码如下:
修改自己的数据库账号密码和生成的路径信息
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="DB2Tables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>
<commentGenerator>
<property name="suppressAllComments" value="true"/>
commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai"
userId="root"
password="123456">
<property name="nullCatalogMeansCurrent" value="true"/>
jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
javaTypeResolver>
<javaModelGenerator targetPackage="com.hao.pojo" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
javaModelGenerator>
<sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.hao.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
javaClientGenerator>
<table tableName="user" domainObjectName="User" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
context>
generatorConfiguration>
4.运行插件生成
点击右侧maven栏,找到Plugins的mybatis-generator:generate,双击运行插件,插件位置如下图:
运行后生成的文件,如下图所示:
到这里已经配置运行完成,mybatis-generator的配置过程中可能会遇到许多坑,可以在评论区留言一起解决