Mybatis逆向工程构建代码

1.首先创建一个Maven工程,pom依赖中添加

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>

    </dependencies>

2.直接在项目目录下新建配置文件


QQ截图20190301194024.png

配置文件的内容为:

<?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">

        <!-- 配置pojo的序列化 -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql:///pinyougoudb" userId="root"
                        password="2237">
        </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.logoxiang.pojo"
                            targetProject=".\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="com.logoxiang.mapper"
                         targetProject=".\src\main\resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.logoxiang.mapper"
                             targetProject=".\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 指定数据库表 -->
        <table schema="" tableName="tb_address"/>
        <table schema="" tableName="tb_areas"/>
        <table schema="" tableName="tb_brand"/>
        <table schema="" tableName="tb_cities"/>
        <table schema="" tableName="tb_content"/>
        <table schema="" tableName="tb_content_category"/>
        <table schema="" tableName="tb_freight_template"/>
        <table schema="" tableName="tb_goods"/>
        <table schema="" tableName="tb_goods_desc"/>
        <table schema="" tableName="tb_item"/>
        <table schema="" tableName="tb_item_cat"/>
        <table schema="" tableName="tb_order"/>
        <table schema="" tableName="tb_order_item"/>
        <table schema="" tableName="tb_pay_log"/>
        <table schema="" tableName="tb_provinces"/>
        <table schema="" tableName="tb_seckill_goods"/>
        <table schema="" tableName="tb_seckill_order"/>
        <table schema="" tableName="tb_seller"/>
        <table schema="" tableName="tb_specification"/>
        <table schema="" tableName="tb_specification_option"/>
        <table schema="" tableName="tb_type_template"/>
        <table schema="" tableName="tb_user"/>
     </context>
</generatorConfiguration>

3.创建运行类:

/**
 * @Author: logoxiang
 * @Date: 2019/1/26 13:52
 */
public class GeneratorSqlMap {

    public void generator() throws Exception{

        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //指定 逆向工程配置文件
        File configFile = new File("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);

    }
    public static void main(String[] args) throws Exception {
        try {
            GeneratorSqlMap generatorSqlmap = new GeneratorSqlMap();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

4.注意我们的逆向工程是依赖数据库中的表,所以咱们首先要把表建起来,在运行逆向工程

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.父工程的pom.xml中导入依赖和插件,当前完整的文件如下: <?xml version="1.0"encod...
    瑜Z酱阅读 10,045评论 0 5
  • 第1章 Maven 介绍 什么是 Maven 什么是 Maven Maven 的正确发音是[ˈmevən],而不是...
    强某某阅读 7,022评论 0 25
  • 简介 Apache Maven是一个软件项目管理和理解工具。基于项目对象模型(POM)的概念,Maven可以从一个...
    我为峰2014阅读 6,340评论 0 1
  • 无谓的争吵 爆发在你我之间 蕴含甜言蜜语的嘴 正迸发着伤人的利箭 看不见言笑晏晏的脸 只剩硝烟弥漫 意气风发的少年...
    陈皮桃汁阅读 1,501评论 2 2
  • 曾几何时,觉得自己停止了成长,更恶劣的是,觉得自己越来越不成熟。 结婚后自己越来越感性,说话喜欢加上“感觉”二字,...
    jun54阅读 1,072评论 0 0