用IDEA基於maven項目使用mybatis-generator-plugin生成mapper和pojo

原文地址


雖然MyBatis很方便,但是想要手寫全部的mapper還是很累人的,好在MyBatis官方推出了自動化工具,可以根據數據庫和定義好的配置直接生成DAO層及以下的全部代碼,非常方便.
首先wom我們自己建一個maven項目,我這裏就不詳細寫
mybatis-generator使用配置

打開pom.xml文件,添加3個依賴和mybatis-generator插件,分別是1.mybatis3.xjar包 2.逆向工程核心包 3.數據庫連接包 4.log4j.jar用於輸出日誌

<build>
        <plugins>
            <plugin>
                <!--
                用maven mybatis插件
                如果不在plugin裏面添加依賴包得引用的話,會找不到相關得jar包,
                在plugin外部得jar包,他不會去找到並執行,
                所以要把plugin運行依賴得jar配置都放在裏面
                -->
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                        <version>1.2.17</version>
                    </dependency>
                    <dependency>
                        <groupId>org.mybatis</groupId>
                        <artifactId>mybatis</artifactId>
                        <version>3.2.6</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.30</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

在src/main/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>
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--數據庫連接的信息:驅動類、連接地址、用戶名、密碼 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mybatis"
                        userId="root"
                        password="123qwe">
        </jdbcConnection>
        <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
            connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
            userId="yycg"
            password="yycg">
        </jdbcConnection> -->

        <!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer,爲 true時把JDBC DECIMAL 和
            NUMERIC 類型解析爲java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成PO類的位置 -->
        <javaModelGenerator targetPackage="po"
                            targetProject="src">
            <!-- enableSubPackages:是否讓schema作爲包的後綴 -->
            <property name="enableSubPackages" value="false" />
            <!-- 從數據庫返回的值被清理前後的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src">
            <!-- enableSubPackages:是否讓schema作爲包的後綴 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="mapper"
                             targetProject="src">
            <!-- enableSubPackages:是否讓schema作爲包的後綴 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 指定數據庫表 -->
        <table tableName="items"></table>
        <table tableName="orders"></table>
        <table tableName="orderdetail"></table>
        <!-- <table schema="" tableName="sys_user"></table>
        <table schema="" tableName="sys_role"></table>
        <table schema="" tableName="sys_permission"></table>
        <table schema="" tableName="sys_user_role"></table>
        <table schema="" tableName="sys_role_permission"></table> -->

    <table tableName="t_warningSetting" domainObjectName="WarningSetting" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

        <!-- 有些表的字段需要指定java類型
         <table schema="" tableName="">
            <columnOverride column="" javaType="" />
        </table> -->
    </context>
</generatorConfiguration>

需要修改的地方:

javaModelGenerator,生成PO類的位置 
sqlMapGenerator,mapper映射文件生成的位置 
javaClientGenerator,mapper接口生成的位置 
table,其tableName屬性對應數據庫中相應表

點擊IDEA右邊的maven projects標籤,按下圖進行操作 
這裏寫圖片描述

運行插件後最後的工程目錄結構如下: 
這裏寫圖片描述

mybatis-generator的應用

mybatis-generator往往是單獨的建立一個普通工程如A,通過運行逆向工程生成相應的mapper和po後然後再將這兩個包拷貝到我們使用到ssm框架創建的web項目,而不是直接在web項目中使用逆向工程。

通過運行上述的程序,我們便通過數據庫中的錶快速的生成了相應的po類和mapper,而不用我們程序員自己再編寫相應的po類和mapper,爲我們帶來了很大的方便,所以這個一定要學會,在後續開發中只要使用到mybatis的地方我們都會通過mybatis的逆向工程自動爲我們生成mapper和po類。


原文地址


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章