Mybatis逆向工程

Mybatis逆向工程

配置说明



<generatorConfiguration>
    

    

    
    
    <classPathEntry location="./mysql-connector-java-5.1.40.jar"/>
    <context id="mysql" defaultModelType="flat" targetRuntime="MyBatis3Simple">
        
        <property name="javaFileEncoding" value="UTF-8" />
        
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter" />
        
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter" />
        
        <property name="beginningDelimiter" value="'">property>
        <property name="endingDelimiter" value="'">property>
        
        <commentGenerator>
            <property name="suppressDate" value="true">property>
            <property name="addRemarkComments" value="true">property>
        commentGenerator>
        
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/mybatis"
                userId="root"
                password="19491001">
        jdbcConnection>
        
        <javaModelGenerator targetPackage="top.simba1949.common" targetProject="F:/java">
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="top.simba1949.mapper" targetProject="F:/java">sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="top.simba1949.mapper" targetProject="F:/java">javaClientGenerator>
        
        <table tableName="%">
            <generatedKey column="id" sqlStatement="mysql">generatedKey>
        table>
    context>

generatorConfiguration>

Java 方式生成

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>top.simba1949groupId>
    <artifactId>Generator-JavaartifactId>
    <version>1.0-SNAPSHOTversion>

    <dependencies>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.1version>
        dependency>
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.3.7version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.40version>
        dependency>
    dependencies>
project>

generatorConfig.xml 配置



<generatorConfiguration>
    <context id="mysql" defaultModelType="flat" targetRuntime="MyBatis3Simple">
        
        <property name="javaFileEncoding" value="UTF-8" />
        
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter" />
        
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter" />
        
        <property name="beginningDelimiter" value="'">property>
        <property name="endingDelimiter" value="'">property>
        
        <commentGenerator>
            <property name="suppressDate" value="true">property>
            <property name="addRemarkComments" value="true">property>
        commentGenerator>
        
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/mybatis"
                userId="root"
                password="19491001">
        jdbcConnection>
        
        <javaModelGenerator targetPackage="top.simba1949.common" targetProject="F:/java">
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="top.simba1949.mapper" targetProject="F:/java">sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="top.simba1949.mapper" targetProject="F:/java">javaClientGenerator>
        
        <table tableName="%">
            <generatedKey column="id" sqlStatement="mysql">generatedKey>
        table>
    context>

generatorConfiguration>

GeneratorMain.java 配置

package top.simba1949.generator;

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;

/**
 * @author [email protected]
 * @date 2018/8/7 9:57
 */
public class GeneratorMain {
    public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        ArrayList<String> warnings = new ArrayList<String>();
        boolean overWrite = true;

        String path = GeneratorMain.class.getClassLoader().getResource("").getPath();
        File configFile = new File(path + "/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);
    }
}

Maven 方式生成

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>top.simba1949groupId>
    <artifactId>Generator-MavenartifactId>
    <version>1.0-SNAPSHOTversion>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.7version>
                <configuration>
	                
	                <configurationFile>./src/main/resources/generatorConfig.xmlconfigurationFile>
                    <verbose>trueverbose>
                    <overwrite>trueoverwrite>
                configuration>
                <dependencies>
                    
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>5.1.40version>
                    dependency>
                dependencies>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifactsid>
                        <goals>
                            <goal>generategoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
project>

generatorConfig.xml 配置



<generatorConfiguration>
    <context id="mysql" defaultModelType="flat" targetRuntime="MyBatis3Simple">
        
        <property name="javaFileEncoding" value="UTF-8" />
        
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter" />
        
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter" />
        
        <property name="beginningDelimiter" value="'">property>
        <property name="endingDelimiter" value="'">property>
        
        <commentGenerator>
            <property name="suppressDate" value="true">property>
            <property name="addRemarkComments" value="true">property>
        commentGenerator>
        
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/mybatis"
                userId="root"
                password="19491001">
        jdbcConnection>
        
        <javaModelGenerator targetPackage="top.simba1949.common" targetProject="F:/java">
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="top.simba1949.mapper" targetProject="F:/java">sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="top.simba1949.mapper" targetProject="F:/java">javaClientGenerator>
        
        <table tableName="%">
            <generatedKey column="id" sqlStatement="mysql">generatedKey>
        table>
    context>

generatorConfiguration>

Mybatis逆向工程_第1张图片

命令方式

generatorConfig.xml 配置



<generatorConfiguration>
    
    <classPathEntry location="./mysql-connector-java-5.1.40.jar"/>
    <context id="mysql" defaultModelType="flat" targetRuntime="MyBatis3Simple">
        
        <property name="javaFileEncoding" value="UTF-8" />
        
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter" />
        
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter" />
        
        <property name="beginningDelimiter" value="'">property>
        <property name="endingDelimiter" value="'">property>
        
        <commentGenerator>
            <property name="suppressDate" value="true">property>
            <property name="addRemarkComments" value="true">property>
        commentGenerator>
        
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/mybatis"
                userId="root"
                password="19491001">
        jdbcConnection>
        
        <javaModelGenerator targetPackage="top.simba1949.common" targetProject="F:/Java">
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="top.simba1949.mapper" targetProject="F:/Java">sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="top.simba1949.mapper" targetProject="F:/Java">javaClientGenerator>
        
        <table tableName="%">
            <generatedKey column="id" sqlStatement="mysql">generatedKey>
        table>
    context>

generatorConfiguration>

Mybatis逆向工程_第2张图片

命令如下

在终端下使用

java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml

你可能感兴趣的:(ORM)