1、逆向工程概念

  • 正向工程:先创建 Java 实体类,由框架负责根据实体类生成数据库表。Hibernate 是支持正向工程的。
  • 逆向工程:先创建数据库表,由框架负责根据数据库表,反向生成如下资源:
    • Java 实体类
    • Mapper 接口
    • Mapper 配置文件

2、基本原理

3、搭建环境

① 加入 jar 包

hamcrest-core-1.3.jar
junit-4.12.jar
log4j.jar
mybatis-3.4.1.jar
mybatis-generator-core-1.3.2.jar
mysql-connector-java-5.1.7-bin.jar

② 加入配置文件

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
32
33
34
35
36
37
38
39
40
41
42
43
44
<?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>
<!--
targetRuntime: 执行生成的逆向工程的版本
MyBatis3Simple: 生成基本的CRUD(清新简洁版)
MyBatis3: 生成带条件的CRUD(奢华尊享版)
-->
<context id="DB2Tables" targetRuntime="MyBatis3">

<!-- 数据库的连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis"
userId="root"
password="root">
</jdbcConnection>

<!-- javaBean的生成策略-->
<javaModelGenerator targetPackage="com.atguigu.mybatis.entity" targetProject=".\pro08-mybatis-MBG\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>

<!-- SQL映射文件的生成策略 -->
<sqlMapGenerator targetPackage="com.atguigu.mybatis.mapper" targetProject=".\pro08-mybatis-MBG\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>

<!-- Mapper接口的生成策略 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.mapper" targetProject=".\pro08-mybatis-MBG\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>

<!-- 逆向分析的表 -->
<!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName -->
<!-- domainObjectName属性指定生成出来的实体类的类名 -->
<table tableName="t_emp" domainObjectName="Employee"/>
<table tableName="t_customer" domainObjectName="Customer"/>
<table tableName="t_order" domainObjectName="Order"/>
</context>
</generatorConfiguration>

③java 代码

1
2
3
4
5
6
7
8
9
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generatorConfig.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);

④ 说明

  • 为了能让 File 构造器读取到 generatorConfig.xml 配置文件,配置文件存放的位置是整个工程的根目录

  • 由于上面的原因,配置 targetProject 属性时需要带上 module 的名称

1
targetProject=".\module名称\src"
  • 生成的资源包括:

4、QBC 查询

① 概念

QBC:Query By Criteria

QBC 查询最大的特点就是将 SQL 语句中的 WHERE 子句进行了组件化的封装,让我们可以通过调用 Criteria 对象的方法自由的拼装查询条件。

② 使用

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
32
33
34
35
// 1.创建EmployeeExample对象
EmployeeExample example = new EmployeeExample();

// 2.通过example对象创建Criteria对象
EmployeeExample.Criteria criteria01 = example.createCriteria();
EmployeeExample.Criteria criteria02 = example.or();

// 3.在Criteria对象中封装查询条件
criteria01
.andEmpAgeBetween(9, 99)
.andEmpNameLike("%o%")
.andEmpGenderEqualTo("male")
.andEmpSalaryGreaterThan(500.55);

criteria02
.andEmpAgeBetween(9, 99)
.andEmpNameLike("%o%")
.andEmpGenderEqualTo("male")
.andEmpSalaryGreaterThan(500.55);

SqlSession session = factory.openSession();

EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);

// 4.基于Criteria对象进行查询
List<Employee> employeeList = mapper.selectByExample(example);

for (Employee employee : employeeList) {
System.out.println("employee = " + employee);
}

session.close();

// 最终SQL的效果:
// WHERE ( emp_age between ? and ? and emp_name like ? and emp_gender = ? and emp_salary > ? ) or( emp_age between ? and ? and emp_name like ? and emp_gender = ? and emp_salary > ? )

5、注意

  • 将来会使用 Maven 命令执行生成过程
  • 在实际开发中,建议把 MBG 本身和项目主体代码分开:不要放在同一个工程或模块内