MyBatis-generator 自动生成MyBatis代码

在pom.xml里面引入mybatis.generator

<!--逆向工程 -->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.5</version>
</dependency>

在resources/mybatis下创建配置文件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="testTables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://127.0.0.1:3306/数据库名" userId="数据库用户名"
            password="数据库密码">
        </jdbcConnection>

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

        <!-- targetProject:生成PO类的位置 -->
        <javaModelGenerator targetPackage="cn.w959.exhibit.bus.entity"
            targetProject=".\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="mapper\bus"
            targetProject=".\src\main\resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="cn.w959.exhibit.bus.dao" targetProject=".\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 指定数据库表 -->
        <table tableName="ping" domainObjectName="AnswerCollect">
        </table>
        <!-- <table tableName="a_question_collect" domainObjectName="QuestionCollect">
            <columnOverride column="category" javaType="java.lang.String" jdbcType="VARCHAR" />
            <columnOverride column="category_sub" javaType="java.lang.String" jdbcType="VARCHAR" />
            <columnOverride column="name" javaType="java.lang.String" jdbcType="VARCHAR" />
            <columnOverride column="tag" javaType="java.lang.String" jdbcType="VARCHAR" />
            <columnOverride column="title" javaType="java.lang.String" jdbcType="VARCHAR" />
            <columnOverride column="description" javaType="java.lang.String" jdbcType="VARCHAR" />
        </table> -->
    </context>
</generatorConfiguration>

创建GeneratorUtils类

package com.majiameng.utils;

import java.io.File;
import java.io.Serializable;
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;


/**
 * Title:      GeneratorUtils
 * Company:    www.majiameng.cn
 * @author:    tinymeng <666@majiameng.com>
 * @version    V1.0
 * Description: mybatis逆向工程
 */
public class GeneratorUtils implements Serializable{
    
    private static final long serialVersionUID = 1L;

    public static void main(String []args) throws Exception {
        List<String>warnings=new ArrayList<>();
        Configuration configuration=new ConfigurationParser(warnings).parseConfiguration(new File("src/main/resources/mybatis/generatorConfig.xml"));
        DefaultShellCallback callback=new DefaultShellCallback(true);
        MyBatisGenerator generator=new MyBatisGenerator(configuration, callback, warnings);
        generator.generate(null);
    }
}

执行 GeneratorUtils 就会生成对应的MyBatis代码了

Last modification:September 2nd, 2019 at 06:31 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment