idea集成 MyBatis Generator 插件,自动生成dao,model,sql map文件

过程非常简单,只需要两部就搞定了,对于码农来说还是少写了很多代码,大大提高了编码效率。

1.集成到开发环境中

本文以maven管理的功能来举例,只需要将插件添加到pom.xml文件中即可。(注意此处是以plugin的方式,放在<plugins> </plugins>中间即可)

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
</plugin>

2.编写配置文件 generatorConfig.xml

注意:在idea开发环境下,此文件需要放在resource根目录下,mybatis generator默认加载此目录的配置文件

<?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:\Program Files (x86)\MySQL\Connector.J 5.1\mysql-connector-java-5.1.40-bin.jar"/>
    <!--数据库驱动oracle jar -->
    <!--
    <classPathEntry
            location="E:\app\Acer\product\11.2.0\client_2\jdbc\lib\ojdbc5.jar"/>-->
    <context id="Tables" targetRuntime="MyBatis3">
        <!--去除注释 -->

        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--mysql数据库连接 -->

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/goods" userId="root"
                        password="root">
        </jdbcConnection>
        <!--oracle数据库连接 -->
        <!--
       <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
                       connectionURL="jdbc:oracle:thin:@172.16.36.23:1521/FOKF"
                       userId="YCGCE" password="YCGCE">
           <property name="remarksReporting" value="true"></property>
       </jdbcConnection>-->

        <!--默认false Java type resolver will always use java.math.BigDecimal if
            the database column is of type DECIMAL or NUMERIC. -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建 使用Maven生成在target目录下,会自动创建) -->
        <javaModelGenerator targetPackage="com.model"
                            targetProject="E:\work7\goods\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成SQLMAP文件 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="E:\work7\goods\src\main\resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!--生成Dao文件 可以配置 type="XMLMAPPER"生成xml的dao实现 context id="DB2Tables" 修改targetRuntime="MyBatis3" -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.dao"
                             targetProject="E:\work7\goods\src\main\java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等 -->
        <table tableName="t_user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"/>
        <table tableName="t_admin" domainObjectName="Admin"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"/>
        <table tableName="t_book" domainObjectName="Book"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"/>
        <table tableName="t_cartitem" domainObjectName="CartItem"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"/>
        <table tableName="t_category" domainObjectName="Category"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"/>
        <table tableName="t_order" domainObjectName="Order"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"/>
        <table tableName="t_orderitem" domainObjectName="OrderItem"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"/>

    </context>
</generatorConfiguration>

 

集成工作完成了,看看如何使用! 
so easy,只需在plugins中找到mybatis-generator plugin即可,双击运行或右击 运行都可。如下如所示: 

附上工程目录结构及自动生成的文件

 

 

posted on 2018-02-01 17:00  o_0的园子  阅读(3052)  评论(0编辑  收藏  举报