来源:https://www.bilibili.com/video/BV1V7411w7VW?p=5
上一节:https://blog.csdn.net/qq_40893824/article/details/107344110
下一节:https://blog.csdn.net/qq_40893824/article/details/107376282
MyBatis 框架需要:
1 实体类
2 自定义 Mapper 接口
3 Mapper.xml
传统:上述 3 组件要开发者手动创建
逆向工程帮开发者来自动创建这 3 组件
MyBatis Generator,简称 MBG
它专门为 MyBatis 框架开发者定制的代码生成器器
自动生成 MyBatis 框架所需的实体类、Mapper 接口、Mapper.xml
支持基本的 增删查改 操作
但相对复杂的 SQL 要开发者自己完成
新建工程
![](https://i-blog.csdnimg.cn/blog_migrate/3df84be6ad9394fd2a31b2a742128f9a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c2b86475eef9e2eae8c3ce28214367db.png)
数据库
use mybatis;
create table t_user(
id smallint primary key ,
username varchar(20),
password varchar(18),
age smallint
)
添加数据:
idea 工程
1 pom 文件中,添加代码:
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
![](https://i-blog.csdnimg.cn/blog_migrate/87b6eb2acbb47c3e787fb771d0721624.png)
2 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>
<context id="testTables" targetRuntime="Mybatis3">
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"
userId="root" password="123456">
</jdbcConnection>
<javaModelGenerator
targetPackage="com.southwind.entity"
targetProject="./src/main/java">
</javaModelGenerator>
<sqlMapGenerator
targetPackage="com.southwind.repository"
targetProject="./src/main/java">
</sqlMapGenerator>
<javaClientGenerator
type="XMLMAPPER"
targetPackage="com.southwind.repository"
targetProject="./src/main/java">
</javaClientGenerator>
<table tableName="t_user" domainObjectName="User"></table>
</context>
</generatorConfiguration>
解析:
<generatorConfiguration>
<context id="自定义名字" targetRuntime="Mybatis3"> targetRuntime="Mybatis3" 不能变动
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"
userId="root" password="123456">
</jdbcConnection>
<javaModelGenerator
下面是目标路径 分成了 两部分,
targetPackage="工程路径下的路径(自动创建)"
targetProject="工程路径(要存在,有意义)">
</javaModelGenerator>
<sqlMapGenerator
原理同 javaModelGenerator,生成路径 分成两部分
targetPackage="com.southwind.repository"
targetProject="./src/main/java">
</sqlMapGenerator>
<javaClientGenerator
type="XMLMAPPER"
targetPackage="com.southwind.repository"
targetProject="./src/main/java">
</javaClientGenerator>
<table tableName="数据库表名" domainObjectName="自动生成的实体类名字"></table>
</context>
</generatorConfiguration>
![](https://i-blog.csdnimg.cn/blog_migrate/f01ac1f68da04c5f6a8812af065acec5.png)
3 java 中,创包 com.southwind.test,其内 新建 类 Main,加入代码:
package com.southwind.test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> warings = new ArrayList<String>();
boolean overwrite = true;
String gencig = "/generatorConfig.xml";
File configFile = new File(Main.class.getResource(gencig).getFile());
ConfigurationParser configurationParser = new ConfigurationParser(warings);
Configuration configuration = null;
try {
configuration = configurationParser.parseConfiguration(configFile);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = null;
try {
myBatisGenerator = new MyBatisGenerator(configuration, callback, warings);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
try {
myBatisGenerator.generate(null);
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/0a4b24fe42e601966ef99afe87885b9f.png)
启动 Main:
运行成功!
上一节:https://blog.csdn.net/qq_40893824/article/details/107344110
下一节:https://blog.csdn.net/qq_40893824/article/details/107376282