• IDEA使用mybatis generator自动生成代码


    主要就三步:

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

        <dependencies>
      <!-- 自动生产mapper Begin! -->
      <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>5.1.34</version>
      </dependency>
        <!-- 自动生产mapper End! -->
        </dependencies>
     
     
    <build>
      <plugins>
       <!-- 自动生产mapper Begin! -->
       <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>
       <!-- 自动生产mapper End! -->
      </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>-->
     
            <commentGenerator>
                <!--  suppressAllComments设置为false,addRemarkComments设置为true会将数据库中的注释自动添加过来。虽然很多废弃说明,但是可以使用正则替换并删除空行,整体来说还是很方便的,具体使用见后续-->
                <property name="suppressAllComments" value="false" />
                <property name="suppressDate" value="false" />
                <property name="addRemarkComments" 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. 在实体类文件中,用正则替换,将包含废弃说明的行替换为空白;
      正则表达式:^.* 大师兄.*$。将大师兄替换为需要的字符串即可。
    2. 删除自动生成的get、set方法;
    3. 使用Ctrl + Alt +L自动格式化代码。然后上述步骤中替换生成的空白行会被自动删除。
    4. 使用 Alt +Insert自动生成get、set方法

    上述步骤绝对比你为每个字段添加注释要方便(如果字段太多的话)。

    5、参考:

    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 最完整配置详解 - 简书
    4. Intellij IDEA 中使用 MyBatis-generator 自动生成 MyBatis 代码 - 志哥的成长笔记 - SegmentFault 思否
  • 相关阅读:
    GCC-windows的预编译版本——nuwen MinGW
    stm32 flash和sram
    NAND Flash和NOR Flash的比较
    emwin如何在windows10下vs2015或2017进行仿真。
    IIC简介(转载)
    ubuntu版本查看
    下载速度更加快的 SourceForge 镜像
    使用virtualbox安装的Ubuntu,窗口分辨率过小,使用增强工具完成和vmtools一样的功能。
    C#中的结构体与类的区别
    理解C#值类型和引用类型
  • 原文地址:https://www.cnblogs.com/buwuliao/p/11099706.html
Copyright © 2020-2023  润新知