IDEA14中使用generator

工具:Intellij IDEA 14 、 MyBatis-generator 、Maven
目標(biāo):在Intellij IDEA 14中使用MyBatis-generator 自動(dòng)生成MyBatis的entity和Mapper文件的自動(dòng)生成。

  1. IDEA 配置maven比較普片和簡單,此過程略過!
  2. 將mybatis-generator-maven-plugin 插件配置到maven的pom.xml文件中,<build><plugins>節(jié)點(diǎn)下添加:
IDEA配置generator圖1.png

3.在resources目錄下,建立名為 generatorConfig.xml的配置文件,作為mybatis-generator-maven-plugin 插件的執(zhí)行目標(biāo)!模板如下所示:

<?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>
    <!--導(dǎo)入屬性配置-->
    <properties resource="datasource.properties"></properties>

    <!--指定特定數(shù)據(jù)庫的jdbc驅(qū)動(dòng)jar包的位置-->
    <classPathEntry location="${jdbc.driverLocation}"/>

    <context id="default" targetRuntime="MyBatis3">

        <!-- optional,旨在創(chuàng)建class時(shí),對注釋進(jìn)行控制 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc的數(shù)據(jù)庫連接 -->
        <jdbcConnection
                driverClass="${jdbc.driverClassName}"
                connectionURL="${jdbc.url}"
                userId="${jdbc.username}"
                password="${jdbc.password}">
        </jdbcConnection>


        <!-- 非必需,類型處理器,在數(shù)據(jù)庫類型和java類型之間的轉(zhuǎn)換控制-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>


        <!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在該項(xiàng)目下所在的路徑
        -->
        <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">-->
        <javaModelGenerator targetPackage="com.zhchji.module.sms.entity" targetProject="./src/main/java">
            <!-- 是否允許子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否對model添加 構(gòu)造函數(shù) -->
            <property name="constructorBased" value="true"/>
            <!-- 是否對類CHAR類型的列的數(shù)據(jù)進(jìn)行trim操作 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立的Model對象是否 不可改變  即生成的Model對象不會(huì)有 setter方法,只有構(gòu)造方法 -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!--mapper映射文件生成所在的目錄 為每一個(gè)數(shù)據(jù)庫的表生成對應(yīng)的SqlMap文件 -->
        <!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">-->
        <sqlMapGenerator targetPackage="mappings" targetProject="./src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 客戶端代碼,生成易于使用的針對Model對象和XML配置文件 的代碼
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper對象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相應(yīng)的Mapper對象
                type="XMLMAPPER",生成SQLMap XML文件和獨(dú)立的Mapper接口
        -->

        <!-- targetPackage:mapper接口dao生成的位置 -->
        <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhchji.module.sms.dao" targetProject="./src/main/java">
            <!-- enableSubPackages:是否讓schema作為包的后綴 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>


        <table tableName="shipping" domainObjectName="Shipping" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="cart" domainObjectName="Cart" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <!--<table tableName="cart_item" domainObjectName="CartItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <table tableName="category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="order_item" domainObjectName="OrderItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="pay_info" domainObjectName="PayInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <columnOverride column="detail" jdbcType="VARCHAR" />
            <columnOverride column="sub_images" jdbcType="VARCHAR" />
        </table>
        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>


        <!-- geelynote mybatis插件的搭建 -->
    </context>
</generatorConfiguration>

注:當(dāng)出現(xiàn)如圖錯(cuò)誤,并提示URI is not registered (Settings | Project Settings | Schemas and DTDs)時(shí):

IDEA配置generator圖2.png

解決方法如圖所示:

IDEA配置generator圖3.png
  1. 為了增加配置的靈活性,在resources目錄下新建外置的屬性配置文件generator.properties(注:命名根據(jù)generatorConfig.xml配置文件來確定),來配置數(shù)據(jù)庫相關(guān)屬性:
jdbc.driverLocation=D:\\maven-jar-v2\\mysql\\mysql-connector-java\\5.1.6\\mysql-connector-java-5.1.6.jar
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.110:3306/zhchji?useUnicode=true&characterEncoding=utf-8
jdbc.username=zhchji
jdbc.password=zhchji901028
  1. 在IDEA為項(xiàng)目添加一個(gè)運(yùn)行選項(xiàng),使用maven運(yùn)行mybatis-generator-maven-plugin 插件


    IDEA配置generator圖4.png
IDEA配置generator圖5.png

上一步后會(huì)有個(gè)彈框(如圖):
修改Name為generator(這里隨便啦),在Command line 輸入mybatis-generator:generate -e(注:-e 是為了讓該插件輸出詳細(xì)信息,這樣可以幫助我們定位問題)

IDEA配置generator圖6.png

Apply ----》OK !

  1. 驗(yàn)證是否添加成功
    方法一:
IDEA配置generator圖7.png

方法二:

IDEA配置generator圖8.png
  1. 第六步點(diǎn)擊運(yùn)行,出現(xiàn)BUILD SUCCESS,則大功告成!如果有錯(cuò)誤,控制臺(tái)將打出詳細(xì),這時(shí)-e就起作用了!
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容