小白的springboot之路(四)、mybatis-generator自動生成mapper和model、dao


0-、前言

  在用mybatis開發項目中,數據庫動輒上百張數據表,如果你一個一個去手動編寫,比較耗費時間;還好,我們有mybatis-generator插件,只需簡單幾步就能自動生成mybatis的model、mapper和Dao文件,很方便;

  題外話:注意,mybatis-generator的項目建議單獨去建一個項目,生成model、mapper、dao后再根據需要拷到實際項目中去;不要集成到實際項目中,以免對實際項目造成影響,因為集成在項目中,一不小心生成了,全部覆蓋了原來的文件,那你自己添加的很多功能代碼就game over了,切勿冒險;

1-、使用方法:

  使用mybatis-generator非常簡單,只需幾步即可:

1-1、新建一個項目,添加依賴:

注意,我指定了版本,其他版本多次失敗,我使用1.3.7版本,穩定好用不失敗;里面的<configurationFile>是我們下面新建的XML配置文件地址

  <dependencies>

            <!-- 1、添加mybatis.generator依賴 -->
            <dependency>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-core</artifactId>
                <version>1.3.7</version>
            </dependency>

        </dependencies>

        <build>

            <plugins>

                <!--2、添加mybatis.generator插件 -->
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.7</version>
                    <configuration>
                        <!--配置XML文件地址,以下為根目錄,如果是resources,則為:src/main/resources/generatorConfig.xml -->
<!--                        <configurationFile>generatorConfig.xml</configurationFile>-->
                        <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                        <!--允許移動生成的文件 -->
                        <verbose>true</verbose>
                        <!--是否覆蓋 -->
                        <overwrite>true</overwrite>
                    </configuration>
                </plugin>

            </plugins>
        </build>

1-2、添加配置文件:

都加了注釋,自己看注釋即可,不多解釋,需要說一下的是<!---6 ->,

A、如果是單個生成表生成,就像6-1一個一個表添加就行;

B、如果要批量生成,就直接像6-2一樣就行,可以用%匹配任意表:tableName="%"匹配所有表;tableName="t_sys_%"表示匹配所有t_sys_開頭的表;可以根據自己需要來匹配任意表來生成;

<?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>
    <!--1、指定特定數據庫的jdbc驅動jar包的位置千萬千萬要指定正確,不然就創建不了文件-->
    <classPathEntry location="C:\maven\repository\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar"/>

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自動生成的注釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--2、配置數據庫連接信息  -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mydb"
                        userId="root"
                        password="88888888">
        </jdbcConnection>

        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--3、指定Model生成的位置  -->
        <javaModelGenerator
                targetPackage="com.anson.model"
                targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>


        <!--4、指定sql映射文件生成的位置  -->
        <sqlMapGenerator
                targetPackage="mapper"
                targetProject=".\src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!--4.1、如果放程序包中-->
<!--        <sqlMapGenerator -->
<!--                targetPackage="com.anson.mapper"-->
<!--                targetProject=".\src\main\java">-->
<!--            <property name="enableSubPackages" value="true"/>-->
<!--        </sqlMapGenerator>-->


        <!--5、指定dao接口生成的位置 .mapper接口  -->
        <!--  type生成類型含義,項目中基本都是用:XMLMAPPER
        type="ANNOTATEDMAPPER",生成Java Model 和基於注解的Mapper對象
        type="MIXEDMAPPER",生成基於注解的Java Model 和相應的Mapper對象
        type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口
        -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.anson.dao"
                             targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>


        <!-- 6-1、單個表生成策略  -->
        <!--生成對應表及類名-->
<!--        <table tableName="t_dept" domainObjectName="Dept" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">-->
        <!--       </table>-->

        <!-- 6-2、整個數據庫批量生成策略 -->
        <table tableName="%"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="false" />
            <!-- 數據庫表主鍵 -->
            <!--   <generatedKey column="id" sqlStatement="Mysql" identity="true" /> -->
        </table>

    </context>
</generatorConfiguration>

好,完畢,就這么簡單,下面開始生成:

1-3、在右側maven中找到插件,直接雙擊就能生成了

 

 雙擊成功生成后,可以看到項目中在自己XML文件填寫的包和路徑下,生成了相關文件

 

 把相關文件烤到你的項目中就可以直接使用了,怎么樣,是不是很神奇,是不是很簡單~

 

 源碼地址:https://github.com/anson-yang/cloverDemo.git


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM