MyBatis逆向工程

方式一:

 

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

▼-mybatis-generator-project
  ▼-src
  | ▼-mian
  |   ▼-java
  |   |--Generator.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.0modelVersion> <groupId>com.mbg.projectgroupId> <artifactId>mybatis-generator-projectartifactId> <version>0.0.1-SNAPSHOTversion> <properties> <junit-jupiter-api-version>5.3.2junit-jupiter-api-version> <mybatis-version>3.4.6mybatis-version> <mybatis-generator-core-version>1.3.2mybatis-generator-core-version> <mysql-connector-java-version>5.1.47mysql-connector-java-version> properties> <dependencies> <dependency> <groupId>org.junit.jupitergroupId> <artifactId>junit-jupiter-apiartifactId> <version>${junit-jupiter-api-version}version> <scope>testscope> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>${mybatis-version}version> dependency> <dependency> <groupId>org.mybatis.generatorgroupId> <artifactId>mybatis-generator-coreartifactId> <version>${mybatis-generator-core-version}version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>${mysql-connector-java-version}version> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-compiler-pluginartifactId> <version>3.7.0version> <configuration> <source>1.8source> <target>1.8target> <encoding>UTF-8encoding> configuration> plugin> plugins> build> project>

 

3.Generator.java

import java.io.File;
import java.util.ArrayList;
import java.util.List;
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;
public class Generator {
    public static void main(String[] args) throws Exception {
        try {
            Generator generator = new Generator();
            generator.generator();
            System.out.println("Mybatisgenerator工程生成完毕!!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void generator() throws Exception{
        List warnings = new ArrayList();
        boolean overwrite = true;
        //指定 逆向工程配置文件(文件路径切记写对)
        File configFile = new File("src/main/resources/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);
    }
}

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>
            
            <property name="suppressAllComments" value="true" />
        commentGenerator>
        
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/哪个库?useSSL=false" userId="数据库用户名" password="数据库连接密码">
        jdbcConnection>
        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>

        
        <javaModelGenerator targetPackage="com.mybatisgenerator.pojo" targetProject=".\src\main\java">
            
            <property name="enableSubPackages" value="false" />
            
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        
        <sqlMapGenerator targetPackage="com.mybatisgenerator.mapper" targetProject=".\src\main\resources">
            
            <property name="enableSubPackages" value="false" />
        sqlMapGenerator>
        
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatisgenerator.dao" targetProject=".\src\main\java">
            
            <property name="enableSubPackages" value="false" />
        javaClientGenerator>
        
        <table schema="" tableName="t_user">table>
        
    context>
generatorConfiguration>

5.运行Generator中的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

MyBatis逆向工程_第1张图片

 

 

 3.运行MainUI

MyBatis逆向工程_第2张图片

 

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

MyBatis逆向工程_第3张图片

 

 

 4.添加数据库连接

MyBatis逆向工程_第4张图片

 

连接成功如下图:

 MyBatis逆向工程_第5张图片

 

5.生成代码

MyBatis逆向工程_第6张图片

 

 6.结果

MyBatis逆向工程_第7张图片

 

 

MyBatis逆向工程_第8张图片

 

 

 

 

 



你可能感兴趣的:(MyBatis逆向工程)