Springboot+Mybatis generator自動(dòng)生成代碼

編寫一些數(shù)據(jù)庫(kù)固定套路如CRUD操作的代碼,時(shí)間久了,其實(shí)還真的是一件比較繁瑣乏味的事情。肯定有人會(huì)想到,既然是固定套路,有著固定的模式,完全可以用代碼工具來(lái)實(shí)現(xiàn)嘛!哈哈,你別不信,還真的有人開發(fā)出了這樣的優(yōu)秀的工具,那就是Mybatis generator。

1.1 創(chuàng)建Springboot工程

創(chuàng)建一個(gè)Springboot 模塊工程,操作步驟可以參考之前寫的《使用IDEA搭建和部署SpringBoot多模塊項(xiàng)目(Multi-Module)》,創(chuàng)建后的springboot-mybatisgenerator目錄如下圖。


directory.png

dao:數(shù)據(jù)庫(kù)mapper接口存放目錄
entity:存放數(shù)據(jù)表對(duì)應(yīng)的bean實(shí)體存放目錄
service:服務(wù)層接口類存放目錄
mapper:存放xml文件目錄

2.1 配置mybatis-generator插件

數(shù)據(jù)庫(kù)表創(chuàng)建好后,就可以使用插件來(lái)自動(dòng)生成mapper和xml文件了,但在這之前,需對(duì)插進(jìn)進(jìn)行一些配置。

mybatis-generator主要包括兩步:

第一步設(shè)置插件的配置參數(shù),這里需要配置的信息包括4個(gè)

  1. 數(shù)據(jù)庫(kù)相關(guān)信息配置,包括驅(qū)動(dòng)名稱、數(shù)據(jù)庫(kù)名、用戶名、密碼
  2. 生成實(shí)體類的位置,確保targetPackage包名設(shè)置正確
  3. 生成的Mpper.xml存放位置,這里將mapper保存到resources目錄下的mapper文件夾中
  4. 接口文件的存放位置
    mybatis-generator-config.xml插件配置文件
<!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="context" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <!-- 數(shù)據(jù)庫(kù)的相關(guān)配置 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://192.168.245.101:3306/studyDB"
                        userId="root"
                        password="xxxxxx"/>

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

        <!-- 實(shí)體類生成的位置 -->
        <javaModelGenerator targetPackage="com.qinghaihu.springbootmybatisgenerator.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        
        <!-- *Mapper.xml 文件的位置 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        
        <!-- Mapper 接口文件的位置 -->
        <javaClientGenerator targetPackage="com.qinghaihu.springbootmybatisgenerator.dao" targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        
        <!-- 相關(guān)表的配置 -->
        <table tableName="sys_user" domainObjectName="SysUser" enableCountByExample="false"  enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
        <!--
        <table tableName="department" domainObjectName="Department" enableCountByExample="false"  enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
        -->
    </context>
</generatorConfiguration>

第二步配置運(yùn)行時(shí)的參數(shù)
在pom文件build中增加plugin節(jié)點(diǎn)配置

    <build>
        <plugins>
            <!-- mybatis generator plugin config -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <!--配置文件的位置-->
                    <configurationFile>${basedir}/src/main/resources/mybatisGeneratorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

配置好插件后,IntelliJ IDEA右側(cè)的maven面板plugin中,就能看到添加的mybatis-generator.


mybatis generator plugin.png

雙擊mybatis-generator:generate后,在指定目錄自動(dòng)生成entity、mapper和xml文件。


generated files.png

mapper文件中,CRUD相關(guān)操作代碼插件也已經(jīng)幫我們生成。


Mapper detalis.png

3 項(xiàng)目源碼

https://github.com/zhanglianzhong/sprinboot-tech.git

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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