springboot 之使用Generator自動生成代碼

一,springboot項目建好之後,在pom.xml文件裏添加maven依賴

<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <!--generator配置文件所在位置-->
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.39</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>3.4.0</version>
                    </dependency>
                </dependencies>
            </plugin>

在這裏我使用了mybatis的tk.mybatis插件,不需要的話去掉就行了

二,配置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>
    <properties resource="application.properties"/>
    <!--mysql驅動包-->
    <classPathEntry location="${jdbc.location}" />

    <!--數據庫驅動-->
    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!--插件-->
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
        </plugin>

        <!-- 註釋配置 -->
        <commentGenerator>
            <!-- 是否生成註釋代時間戳 -->
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!-- JDBC連接 -->
        <jdbcConnection
                driverClass="${spring.datasource.driver-class-name}"
                connectionURL="${spring.datasource.url}"
                userId="${spring.datasource.username}"
                password="${spring.datasource.password}">
        </jdbcConnection>
        <!--model的生成規則和路徑-->
        <javaModelGenerator targetPackage="com.example.generator.entity"
                            targetProject="src/main/java"/>
        <!--mapper映射文件的依賴生成規則和路徑-->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="./src/main/resources"/>
        
        <!--mapper生成規則和路徑-->
        <javaClientGenerator targetPackage="com.example.generator.mapper"
                             targetProject="src/main/java" type="XMLMAPPER"/>

        <!--</table>-->
        <table tableName="material_change_location_recode" domainObjectName="MaterialChangeLocationRecode" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"/>
    </context>
    <!--
     enableInsert(默認true):指定是否生成insert語句;
     enableSelectByPrimaryKey(默認true):指定是否生成按照主鍵查詢對象的語句(就是getById或get);
     enableSelectByExample(默認true):MyBatis3Simple爲false,指定是否生成動態查詢語句;
     enableUpdateByPrimaryKey(默認true):指定是否生成按照主鍵修改對象的語句(即update);
     enableDeleteByPrimaryKey(默認true):指定是否生成按照主鍵刪除對象的語句(即delete);
     enableDeleteByExample(默認true):MyBatis3Simple爲false,指定是否生成動態刪除語句;
     enableCountByExample(默認true):MyBatis3Simple爲false,指定是否生成動態查詢總條數語句(用於分頁的總條數查詢);
     enableUpdateByExample(默認true):MyBatis3Simple爲false,指定是否生成動態修改語句(只修改對象中不爲空的屬性);
     -->
</generatorConfiguration>

mysql驅動包:

指向本地倉庫mysql驅動包的路徑

三,編寫maven命令

點擊+選擇maven

填寫命令

運行之後,生成文件如下

發佈了8 篇原創文章 · 獲贊 1 · 訪問量 3947
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章