ideamaven版的MBG逆向工程

一、简介

  简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。

  支持基本的增删改查,以及QBC风格的条件查询。

  但是表连接、存储过程等这些复杂sql的定义需要我们手工编写

• 官方文档地址 http://www.mybatis.org/generator/

• 官方工程地址 https://github.com/mybatis/generator/releases

2.1、依赖

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

2.2添加插件: 里面还要在添加一个mysql连接驱动的依赖 。

   <!--mybatis-generator-maven-plugin-->
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.2</version>
        <configuration>
          <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
          <verbose>true</verbose>
          <overwrite>true</overwrite>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
          </dependency>
        </dependencies>
      </plugin>

2.3在resources源文件夹下面创建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>
<!--    <classPathEntry location="D:/Java/lib/mysql-connector-java-5.1.43.jar" />-->
    <context id="test" targetRuntime="MyBatis3">

        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
        <commentGenerator>
            <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
            <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/crmpro"
                        userId="root"
                        password="ych521mm">
        </jdbcConnection>
        <javaTypeResolver>
            <!-- This property is used to specify whether MyBatis Generator should
             force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 指定javaBean的生成策略  文件夹自己定义-->
        <javaModelGenerator targetPackage="pojo"
                            targetProject=".src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- sqlMapGenerator:sql映射生成策略: -->
        <sqlMapGenerator targetPackage="dao"
                         targetProject=".src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- javaClientGenerator:指定mapper接口所在的位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="dao"
                             targetProject=".src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 指定要逆向分析哪些表:根据表要创建javaBean -->
        <table tableName="account" domainObjectName="Account"></table>

        <!-- 要生成哪些表 -->
        <table tableName="employee" domainObjectName="Employee"
        enableCountByExample="false" enableUpdateByExample="false"
        enableDeleteByExample="false" enableSelectByExample="false"
        selectByExampleQueryId="false">
        </table>

    </context>
</generatorConfiguration>

这样就在idea搭配maven环境下完成代码的生成!!!!

原文地址:https://www.cnblogs.com/ych961107/p/11937918.html