【Java】MyBatis应用(四)逆向工程

正文索引 [隐藏]

MyBatis框架需要:实体类、自定义Mapper接口、Mapper.xml
传统发开中上述三个组件需要开发者手动创建,逆向工程可以帮助开发者自动创建三个组件,减轻开发者的工作量,提高工作效率

如何使用逆向工程

MyBatis Generator,简称MBG,是一个专门为Mybatis框架开发者定制的代码生成器,可自动生成MaBatis框架所需的实体类、Mapper接口和Mappler.xml,支持基本的CRUD操作,但是一些相对复杂的SQL需要开发者自己来完成

  • 新建Maven工程,pom.xml
  • <dependencies>
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.21</version>
    </dependency>
    <dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator</artifactId>
    <version>1.3.2</version>
    </dependency>
    </dependencies>
  • 创建MBG配置文件generatorConfig.xml
  • 1、jdbcConnect配置数据库连接信息
    2、javaModelGenerator配置JavaBean的生成策略
    3、sqlMapGenerator配置SQL映射文件生成策略
    4、JavaClientGenerator配置Mapper接口的生成策略
    5、table配置目标数据表(tbaleName:表名,domainObjectName:JavaBean类名)

    <?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="mybatis" targetRuntime="MyBatis3">
    <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
    connectionURL="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"
    userId="root" password="123456">
    </jdbcConnection>
    <javaModelGenerator targetPackage="com.mybatistest.entity" targetProject=".\src\main\java">
    </javaModelGenerator>
    <sqlMapGenerator targetPackage="com.mybatistest.repository" targetProject=".\src\main\java">
    </sqlMapGenerator>
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatistest.repository" targetProject=".\src\main\java">
    </javaClientGenerator>
    <table tableName="t_user" domainObjectName="User">
    </table>
    </context>
    </generatorConfiguration>

  • 创建Generator执行类
  • package com.mybatis.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();
    }
    }
    }
blank