学习笔记——Mybatis逆向工程MBG;MyBatis逆向工程MBG使用步骤

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

2023-01-12

一、逆向工程

1、逆向工程

数据库中表影响程序中代码(表影响java对象)。

MyBatis Generator:简称MGB,是一个专门为MyBatis框架使用定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。

2、正向工程

应用程序中代码影响数据库表中数据(java对象影响表)

二、MGB简介

(1)MyBatis Generator:即MBG

(2)MBG是一个专门为MyBatis框架使用者定制的代码生成器

(3)MBG可以快速的根据表生成对应的映射文件、接口、以及bean类

(4)只可以生成单表CRUD,但是表连接、存储过程等这些复杂sql的定义需要我们手工编写

2023-01-13

 三、逆向工程的使用步骤

1、官方文档

http://mybatis.org/generator/

2、导入逆向工程的jar包

放在模块的“pom.xml”中的<dependecies>中

<!-- 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>

3、编写MBG的配置文件mbg.xml(重要几处配置)

相应的配置文件在官方文档中,可以找到

向下滑动

 

 

上述配置文件是官方给出的示例配置文件,之后将其中相关部分改为和自己模块匹配的内容

 4、运行程序(代码生成器)

 

四、创建MBG逆向工程项目

1、创建一个maven项目,命名为“day05_mybatis_MBG”

 (1)在模块的“pom.xml”中导入jar包

    <dependencies>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- 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>

    </dependencies>

(2)在“day05_mybatis_MBG.src.main.resources”中创建“mybatis-config.xml”,设置“db.properties”和“log4j.xml”

(3)在“day05_mybatis_MBG.src.main.resources”中创建“mbg.xml“

(4)设置测试文件

在“day05_mubatis_MBG.src.test.java”中创建测试类“TestMBG”

 

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6