1.逆向工程简介

MyBatis Generator: 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写
官方文档地址:
http://www.mybatis.org/generator/
官方工程地址:
https://github.com/mybatis/generator/releases

2.逆向工程的配置

2.1导入逆向工程的jar包

maven仓库地址:https://mvnrepository.com/

在maven仓库网站中搜mybatis-generator-core

Snipaste_2023-05-16_11-15-58

这里以1.3.6为例,复制到pom.xml导入包

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>

2.2配置时注意一些必须属性

自己在配置的时候要不要忘了一些必须属性,如:数据库表名

Snipaste_2023-05-16_11-43-45

2.3编写MBG的配置文件mbg.xml(重要几处配置),参考官方文档

Snipaste_2023-05-16_11-26-04

Snipaste_2023-05-16_11-26-15

注意:如果使用的是MySQL8,在jdbcConnection标签中还需要添加以下标签<property name=”nullCatalogMeansCurrent” value=true” />

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE generatorConfiguration PUBLIC
"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--
id属性:设置一个唯一标识
targetRuntime属性值说明:
MyBatis3Simple:基本的增删改查
MyBatis3:带条件查询的增删改查
-->
<context id="simple" targetRuntime="MyBatis3Simple">
<!--设置连接数据库的相关信息-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis"
userId="root"
password="root"/>

<!--设置JavaBean的生成策略-->
<javaModelGenerator targetPackage="com.atguigu.mybatis.mbg.beans" targetProject="src"/>

<!--设置SQL映射文件的生成策略-->
<sqlMapGenerator targetPackage="com.atguigu.mybatis.mbg.mapper" targetProject="src"/>

<!--设置Mapper接口的生成策略-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.mbg.mapper" targetProject="src"/>

<!--逆向分析的表-->
<table tableName="employees" domainObjectName="Employee"/>
<table tableName="departments" domainObjectName="Department"/>
</context>
</generatorConfiguration>

2.4参考官方文档创建代码生成器运行代码

Snipaste_2023-05-16_11-28-39

1
2
3
4
5
6
7
8
9
10
11
@Test
public void testMGB() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mbg.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}

3.逆向工程的使用

参考Mybatis官方文档:

https://mybatis.org/mybatis-3/zh/getting-started.html

3.1从 XML 中构建 SqlSessionFactory

Snipaste_2023-05-16_11-32-44

3.2从 SqlSessionFactory 中获取 SqlSession

Snipaste_2023-05-16_11-35-22

3.3完整的基本查询测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
测试获取一个对象
*/
@Test
public void testSelectByPrimaryKey() throws IOException {
//设置MyBatis全局配置文件的路径
String resource = "mybatis-config.xml";
//读取类路径下的配置文件得到输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取SqlSession对象,相当于Connection对象
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
//获取Mapper代理对象
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
Employee employee = mapper.selectByPrimaryKey(1);
System.out.println(employee);
} finally {
//关闭sqlSession
sqlSession.close();
}
}

4.补充

具体的配置可以看Mybatis中的教程,想用带条件的SQL则在xml文件里改一下targetRuntime属性值说明(之前用的是MyBatis3Simple)

1
2
3
4
5
<!--
targetRuntime属性值说明:
MyBatis3Simple:基本的增删改查
MyBatis3:带条件查询的增删改查
-->

Snipaste_2023-05-16_11-38-21

Snipaste_2023-05-16_11-54-50