IDEA Maven Mybatis generator 自动生成代码(实例讲解)(转)

IDEA Maven Mybatis generator 自动生成代码(实例讲解)

 

MyBatis Generator

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

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

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

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

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

MBG使用

Idea创建maven项目

 

配置pom.xml

 

添加依赖

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

添加插件

 <!--mybatis-generator-maven-plugin-->
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <configuration>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                </plugin>
<!-- /. -->

在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="false" />
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/ssm"
                        userId="root"
                        password="root">
        </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="cn.itcast.domain"
                            targetProject=".srcmainjava">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

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

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

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

        <!--&lt;!&ndash; 要生成哪些表 &ndash;&gt;-->
        <!--<table tableName="t_user" domainObjectName="user"-->
               <!--enableCountByExample="false" enableUpdateByExample="false"-->
               <!--enableDeleteByExample="false" enableSelectByExample="false"-->
               <!--selectByExampleQueryId="false"></table>-->
    </context>
</generatorConfiguration>

特别注意的一点:一定要在配置文件中加入本地的mysql-connector-java-5.1.43-bin.jar,

 下载地址:https://mvnrepository.com/artifact/mysql/mysql-connector-java

 我的配置如下: <classPathEntry location="D:/Java/lib/mysql-connector-java-5.1.43.jar" />

 这个需要大家根据自己存放的路径配置。

配置完成后,一定要点击Build->Rebuild project,生成target文件夹,不然生产代码的时候是生产在target文件下下面,没有这个文件夹会报错,当然也可以配置生成在其他文件夹下面。

MBG的配置文件(重要几处配置)

  1)jdbcConnection配置数据库连接信息

  2)javaModelGenerator配置javaBean的生成策略

  3)sqlMapGenerator 配置sql映射文件生成策略

  4)javaClientGenerator配置Mapper接口的生成策略

  5)table 配置要逆向解析的数据表

    tableName:表名

    domainObjectName:对应的javaBean名 

 

 执行生成代码

 

之后弹出运行配置框,为当前配置配置一个名称,配置名称Name

然后在 “Command line” 选项中输入“mybatis-generator:generate -e
这里加了“-e ”选项是为了让该插件输出详细信息,这样可以帮助我们定位问题。

 点击运行

 生成成功

原文地址:https://www.cnblogs.com/mkl7/p/10842431.html