IDEA使用mybatis generator自动生成代码

主要就三步:

1、pom 文件中引入jar包并配置 build 属性

    <dependencies>
  <!-- 自动生产mapper -->
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.34</version>
  </dependency>
    </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.5</version>
    <configuration>
     <verbose>true</verbose>
     <overwrite>true</overwrite>
    </configuration>
   </plugin>
  </plugins>
 </build>

2、代码中配置 generatorConfig.xml 文件,放入resource根目录下

具体说明可以参考注释.

<?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>
    <!-- 配置mysql 驱动jar包路径.用了绝对路径 -->
    <classPathEntry location="c:/Users/panghaichen/.m2/repository/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar" />

    <context id="wangyongzhi_mysql_tables" targetRuntime="MyBatis3">
        <!-- 防止生成的代码中有很多注释,加入下面的配置控制 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true" />
        </commentGenerator>

        <!-- 数据库连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://10.248.224.12:21202/dmall_virtual_business?useUnicode=true&amp;characterEncoding=UTF-8"
                        userId="marketing"
                        password="3KLPHtdmKAwtA18">
        </jdbcConnection>

        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 数据表对应的model层输出目录:DO实体类 -->
        <javaModelGenerator targetPackage="main.java.com.dmall.virtual.dao.auto" targetProject="src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- sql mapper 映射配置文件输出目录:XML文件 -->
        <sqlMapGenerator targetPackage="main.java.com.dmall.virtual.dao.auto" targetProject="src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- mybatis3中的mapper接口输出目录:DAO接口类 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="main.java.com.dmall.virtual.dao.auto" targetProject="src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 数据表进行生成操作 schema:相当于库名; tableName:表名; domainObjectName:对应的DO类名-->
        <table schema="dmall_virtual_business" tableName="denomination_manage" domainObjectName="DenominationManage"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

        <table schema="dmall_virtual_business" tableName="denomination_manage_log" domainObjectName="DenominationManageLog"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

3、运行插件生成代码

4、参考:

  1. Maven 配置之 MyBatis Generator Maven Plugin (三步学会使用 MyBatis Generator 生成代码)- 四个空格
  2. MyBatis Generator Core – Introduction to MyBatis Generator
    注:官网,及介绍。详细信息可以自行观看。
  3. MyBatis Generator Core – MyBatis Generator XML Configuration File Reference
    注:XML文件全部配置属性可参考以上。有一个中文可参考:Mybatis Generator 最完整配置详解 - 简书

猜你喜欢

转载自www.cnblogs.com/buwuliao/p/11099706.html