1. 程式人生 > >Springboot mybatis generate 自動生成實體類和Mapper

Springboot mybatis generate 自動生成實體類和Mapper

一、資料庫

    資料庫使用的是禪道專案管理軟體的資料表。


二:使用IDEA 建立Spring boot 專案

2.1:選擇Spring Initializ 


下一步後:


點選下一步後,分別在Web選項中勾選Web,在Template Engines選項中勾選 Thymeleaf,在SQL選項中勾選MySQL和MyBatis。然後一直下一步到完成。

三、專案結構為:


四、POM檔案配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>dd.springboot.demo</groupId>
    <artifactId>dd-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>dd-demo</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <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>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--新增mybatis generator maven外掛-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <!--generatorConfig.xml位置-->
                    <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <phase>generate-sources</phase>
                    </execution>
                </executions>
                <!--此處必須新增mysql驅動包-->
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <scope>runtime</scope>
                        <version>5.1.46</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

application.properties 檔案配置如下

#mysql drive
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/zentao?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password =root
# view
#spring.mvc.view.prefix=classpath:/templates/
#spring.mvc.view.suffix=.html
#
spring.thymeleaf.cache=true
spring.thymeleaf.prefix=classpath:/templates/views/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8

五、在resources目錄下建立mybatis-generator資料夾,並mybatis-generator資料夾中建立generatorConfig.xml檔案和mybatisGeneratorinit.properties檔案

mybatisGeneratorinit.properties配置如下

jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/zentao?useUnicode=true&characterEncoding=utf-8&useSSL=true
jdbc_user=root
jdbc_password=root models=E:\\CodeTest\\dd-demo\\src\\main\\java resources=E:\\CodeTest\\dd-demo\\src\\main\\resources dao=E:\\CodeTest\\dd-demo\\src\\main\\java

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>
    <!--執行generator外掛生成檔案的命令: call mvn mybatis-generator:generate -e -->
    <!-- 引入配置檔案 -->
    <properties resource="mybatis-generator/mybatisGeneratorinit.properties"/>
    <!--classPathEntry:資料庫的JDBC驅動,換成你自己的驅動位置 可選 -->
    <!--<classPathEntry location="E:\mybatis\mysql-connector-java-5.1.24-bin.jar" /> -->

    <!-- 一個數據庫一個context -->
    <!--defaultModelType="flat" 大資料欄位,不分表 -->
    <context id="MysqlTables" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <!-- 自動識別資料庫關鍵字,預設false,如果設定為true,根據SqlReservedWords中定義的關鍵字列表;
        一般保留預設值,遇到資料庫關鍵字(Java關鍵字),使用columnOverride覆蓋 -->
        <property name="autoDelimitKeywords" value="true" />
        <!-- 生成的Java檔案的編碼 -->
        <property name="javaFileEncoding" value="utf-8" />
        <!-- beginningDelimiter和endingDelimiter:指明資料庫的用於標記資料庫物件名的符號,比如ORACLE就是雙引號,MYSQL預設是`反引號; -->
        <property name="beginningDelimiter" value="`" />
        <property name="endingDelimiter" value="`" />

        <!-- 格式化java程式碼 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
        <!-- 格式化XML程式碼 -->
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />

        <!-- 註釋 -->
        <commentGenerator >
            <property name="suppressAllComments" value="false"/><!-- 是否取消註釋 -->
            <property name="suppressDate" value="true" /> <!-- 是否生成註釋代時間戳-->
        </commentGenerator>

        <!-- jdbc連線 -->
        <jdbcConnection driverClass="${jdbc_driver}" connectionURL="${jdbc_url}" userId="${jdbc_user}" password="${jdbc_password}" />
        <!-- 型別轉換 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自動轉化以下型別(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成實體類地址 -->
        <javaModelGenerator targetPackage="dd.springboot.demo.models" targetProject="${models}" >
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成mapxml檔案 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="${resources}" >
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- 生成mapxml對應client,也就是介面dao -->
        <javaClientGenerator targetPackage="dd.springboot.demo.dao" targetProject="${dao}" type="XMLMAPPER" >
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- table可以有多個,每個資料庫中的表都可以寫一個table,tableName表示要匹配的資料庫表,也可以在tableName屬性中通過使用%萬用字元來匹配所有資料庫表,只有匹配的表才會自動生成檔案 -->
        <!--如果想生成一個表則tableName="table_name"-->
        <table tableName="%"
               enableCountByExample="true"
               enableUpdateByExample="true"
               enableDeleteByExample="true"
               enableSelectByExample="true"
               selectByExampleQueryId="true">
            <property name="useActualColumnNames" value="false" />
            <!-- 資料庫表主鍵 -->
            <generatedKey column="id" sqlStatement="Mysql" identity="true" />
        </table>
    </context>
</generatorConfiguration>

六、生成屍體類、對映XML和Dao


結果如下: