MyBatis逆向工程

方式一:

 

1.新建Maven工程(mybatis-generator-project)结构如下

▼-mybatis-generator-project
  ▼-src
  | ▼-mian
  |   ▼-java
  |   |--MybatisGeneratorMainClass.java
  |   ▼-resources
  |   |--generatorConfig.xml
  |-pom.xml	

2.配置pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation
="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mbg.project</groupId> <artifactId>mybatis-generator-project</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 版本信息 --> <properties> <junit-jupiter-api-version>5.3.2</junit-jupiter-api-version> <mybatis-version>3.4.6</mybatis-version> <mybatis-generator-core-version>1.3.2</mybatis-generator-core-version> <mysql-connector-java-version>5.1.47</mysql-connector-java-version> </properties> <!-- jar包依耐 --> <dependencies> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit-jupiter-api-version}</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis-version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${mybatis-generator-core-version}</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-java-version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>

 

3. MybatisGeneratorMainClass.java

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MybatisGeneratorMainClass {

    public static MybatisGeneratorMainClass own = new MybatisGeneratorMainClass();

    public static void main(String[] args) throws Exception {
        try {
            own.generator();
            System.out.println("Mybatisgenerator工程生成完毕!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void generator() throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        ConfigurationParser cp = new ConfigurationParser(warnings);
        InputStream resourceAsStream = own.getClass().getClassLoader().getResourceAsStream("generatorConfig.xml");
        Configuration config = cp.parseConfiguration(resourceAsStream);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }

}

 

4.配置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="mybatisGenerator" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://IP地址:数据库访问端口/库名?其他参数"
                userId="数据库用户名"
                password="数据库连接密码">
        </jdbcConnection>

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
            NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成pojo类的位置 -->
        <javaModelGenerator targetPackage="pojo" targetProject=".\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="mapper" targetProject=".\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 指定数据库中的表 -->
        <table schema="" tableName="表名称"></table>

    </context>
</generatorConfiguration>

 

5.运行main()方法

注意:JDK版本低于8编译,会报Exception in thread "main" java.lang.NoClassDefFoundError: java/time/temporal/TemporalAccessor

 

方式二:

1.利用IDEA clone github上的逆向工程

地址:https://github.com/zouzg/mybatis-generator-gui

2.clone成功后,配置好工程自带的依耐库

Project Strucure --> Modules --> mybatis-generator-gui工程 -->Dependencies --> 添加JARs or directories... --> mybatis-generator-gui工程resources文件夹下的lib

 

 

 3.运行MainUI

 

成功运行main()方法如下图:

 

 

 4.添加数据库连接

 

连接成功如下图:

 

 

5.生成代码

 

 6.结果

 

 

 

 

 

 

 



posted @ 2019-09-18 22:02  youngyajun  阅读(211)  评论(0编辑  收藏  举报