Mybatis逆向工程

Scroll Down

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"&gt;

<generatorConfiguration>
    <!—不生成 example类—>
    <context id=”DB2Tables” targetRuntime=”MyBatis3Simple” defaultModelType=”flat”>
    <!—<context id=”testTables” targetRuntime=”MyBatis3”>—>
        <commentGenerator>
            <!— 是否去除自动生成的注释 true:是 : false:否 —>
            <property name=”suppressAllComments” value=”true” />
        </commentGenerator>
        <!—数据库连接的信息:驱动类、连接地址、用户名、密码 —>
        <jdbcConnection driverClass=”com.mysql.jdbc.Driver”
                        connectionURL=”jdbc:mysql://localhost:3306/mx” userId=”root”
                        password=”123456”>
        </jdbcConnection>
        <!— <jdbcConnection driver connectionURL=”jdbc:oracle:thin:@127.0.0.1:1521:yycg”
            userId=”yycg” password=”yycg”> </jdbcConnection> —>

        <!— 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL
            和 NUMERIC 类型解析为java.math.BigDecimal —>
        <javaTypeResolver>
            <property name=”forceBigDecimals” value=”false” />
        </javaTypeResolver>
        <!— targetProject:生成PO类的位置 —>
        <javaModelGenerator targetPackage=”com.entity”
                            targetProject=”src”>
            <!— enableSubPackages:是否让schema作为包的后缀 —>
            <property name=”enableSubPackages” value=”false” />
            <!— 从数据库返回的值被清理前后的空格 —>
            <property name=”trimStrings” value=”true” />
        </javaModelGenerator>
        <!— targetProject:mapper映射文件生成的位置 —>
        <sqlMapGenerator targetPackage=”com.mapper”
                         targetProject=”src”>
            <!— enableSubPackages:是否让schema作为包的后缀 —>
            <property name=”enableSubPackages” value=”false” />
        </sqlMapGenerator>
        <!— targetPackage:mapper接口生成的位置 —>
        <javaClientGenerator type=”XMLMAPPER”
                             targetPackage=”com.mapper” targetProject=”src”>
            <!— enableSubPackages:是否让schema作为包的后缀 —>
            <property name=”enableSubPackages” value=”false” />
        </javaClientGenerator>
        <!— 指定数据库表 不生成 example类 —>
        <table tableName=”resources”>
        </table>
    </context>
</generatorConfiguration>

核心代码

List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File(“src/main/rescourses/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);

jar包

        <!—逆向工程—>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
        <!—mysql驱动包—>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>