第二篇 Springboot mybatis generate根據數據庫表自動生成實體類、Mapper和Mapper.xml


源碼鏈接:https://pan.baidu.com/s/1iP4UguBufHbcIEv4Ux4wDw
提取碼:j6z9

目錄結構如下:只需增加一個generatorConfig.xml文件和在pom.xml中配置下Mybatis generator代碼生成插件即可

pom.xml中增加如下配置:

pom.xml中相關依賴及build插件部分代碼

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.1</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <version>2.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <!-- maven工程在默認情況下src/main/java目錄下的mapper文件是不發布到target目錄下的 -->
    <!-- 增加下面resource配置將resources目錄下的文件和java目錄下的配置文件添加資源映射-->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.*</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <!-- Mybatis generator代碼生成插件 配置 -->
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.1</version>
            <configuration>
                <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                <overwrite>true</overwrite>
                <verbose>true</verbose>
            </configuration>
        </plugin>
    </plugins>
</build>

generatorConfig.xml文件內容如下:

<!--數據庫驅動包路徑 -->
<classPathEntry location="C:\Users\heave\.m2\repository\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar"/>

<context id="DB2Tables" targetRuntime="MyBatis3">
    <!--關閉注釋 -->
    <commentGenerator>
        <property name="suppressAllComments" value="true"/>
    </commentGenerator>

    <!--數據庫連接信息 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/big_strong?characterEncoding=utf-8"
                    userId="root" password="123456">
    </jdbcConnection>

    <!--生成的model 包路徑 -->
    <javaModelGenerator targetPackage="big.strong.disheng.user.model" targetProject="src/main/java">
        <property name="enableSubPackages" value="ture"/>
        <property name="trimStrings" value="false"/>
    </javaModelGenerator>

    <!--生成xml mapper文件 路徑 -->
    <sqlMapGenerator targetPackage="big.strong.disheng.user.dao" targetProject="src/main/java">
        <property name="enableSubPackages" value="ture"/>
    </sqlMapGenerator>

    <!-- 生成的Dao接口 的包路徑 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="big.strong.disheng.user.dao" targetProject="src/main/java">
        <property name="enableSubPackages" value="ture"/>
    </javaClientGenerator>
    <!--對應數據庫表名 tableName數據庫表名稱,domainObjectName生成model類的名稱 -->
    <table tableName="role" domainObjectName="Role" enableCountByExample="false"
           enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
        <property name="useActualColumnNames" value="true"/>
    </table>
    <table tableName="permission" domainObjectName="Permission" enableCountByExample="false"
           enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
        <property name="useActualColumnNames" value="true"/>
    </table>
    <table tableName="user_role" domainObjectName="UserRole" enableCountByExample="false"
           enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
        <property name="useActualColumnNames" value="true"/>
    </table>
    <table tableName="role_permission" domainObjectName="RolePermission" enableCountByExample="false"
           enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
        <property name="useActualColumnNames" value="true"/>
    </table>
</context>

雙擊之后控制台會輸出

完成

源碼鏈接:https://pan.baidu.com/s/1iP4UguBufHbcIEv4Ux4wDw
提取碼:j6z9


免責聲明!

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



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