SpringBoot 學習系列 | (八)IDEA Mybatis 生成逆向工程(generator)
阿新 • • 發佈:2019-01-10
本篇將主要介紹Mybatis的逆向工程如何在SpringBoot環境上實現。
- 環境準備
IDEA、SpringBoot、Mybatis
- 目錄結構
- 表結構
- maven依賴的包與外掛(只貼出Mybatis相關包)
<!--mysql資料庫連線--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> </dependencies>
- maven外掛依賴
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!--mybatis逆向工程外掛--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> </dependency> </dependencies> </plugin> </plugins> </build>
- application.properties檔案配置
- generatorConfig.xml(逆向工程配置檔案,如果使用maven外掛啟動,請保持檔名一致)注:自行根據情況作出修改
<?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="generator.properties"></properties> <!--指定特定資料庫的jdbc驅動jar包的位置--> <classPathEntry location="${jdbc.driverLocation}"/> <context id="default" targetRuntime="MyBatis3"> <!-- optional,旨在建立class時,對註釋進行控制 --> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--jdbc的資料庫連線 --> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}"> </jdbcConnection> <!-- 非必需,型別處理器,在資料庫型別和java型別之間的轉換控制--> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類 targetPackage 指定生成的model生成所在的包名 targetProject 指定在該專案下所在的路徑 --> <javaModelGenerator targetPackage="com.slx.zsxt.model" targetProject="src/main/java"> <!-- 是否允許子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false"/> <!-- 是否對model新增 建構函式 --> <property name="constructorBased" value="true"/> <!-- 是否對類CHAR型別的列的資料進行trim操作 --> <property name="trimStrings" value="true"/> <!-- 建立的Model物件是否 不可改變 即生成的Model物件不會有 setter方法,只有構造方法 --> <property name="immutable" value="false"/> </javaModelGenerator> <!--Mapper對映檔案生成所在的目錄 為每一個數據庫的表生成對應的SqlMap檔案 --> <sqlMapGenerator targetPackage="com.slx.zsxt.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 客戶端程式碼,生成易於使用的針對Model物件和XML配置檔案 的程式碼 type="ANNOTATEDMAPPER",生成Java Model 和基於註解的Mapper物件 type="MIXEDMAPPER",生成基於註解的Java Model 和相應的Mapper物件 type="XMLMAPPER",生成SQLMap XML檔案和獨立的Mapper介面 --> <javaClientGenerator targetPackage="com.slx.zsxt.dao" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="reguser" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="adminuser" domainObjectName="Admin" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="configinfo" domainObjectName="Confinfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="grade" domainObjectName="Grade" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="gradelog" domainObjectName="Gradelog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="reginfo" domainObjectName="Reginfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
- 使用IDEA啟動maven的外掛
1.點選run,找到Edit Configurations...
2.如何沒有找到maven啟動,點選新增maven
3.填入資訊
4.然後通過maven啟動
之後就成功了,在相應的目錄檔案會生成相應的檔案,這裡就補貼圖了。