SpringBoot+mabatis:逆向工程
阿新 • • 發佈:2018-11-12
mybatis+SpringBoot逆向工程
準備:
1. mysql資料庫裡有建好的表
2.相關的開發環境
步驟:
1. 新建Maven專案
2. 在pom.xml裡新增相關依賴和bulid外掛
3. 編寫mybatis逆向工程的配置檔案 generatorConfig.xml
4. maven build
pom.xml
<?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>com.rambo</groupId> <artifactId>mbg</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 設定編碼和Java版本 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <!-- 設定頂層SpringBoot版本號 --> <dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- 測試模組 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 緩衝模組 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <!-- jdbc模組 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- mybatis模組 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- mysql模組 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <!-- SpringBoot外掛 --> <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.5</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
generatorConfig.xml(放在src/main/resources資料夾下)
<?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="D:\generator_mybatis\mysql-connector-java-5.1.24-bin.jar" /> --> <!-- 一個數據庫一個context --> <!--defaultModelType="flat" 大資料欄位,不分表 --> <context id="context1"> <!-- 自動識別資料庫關鍵字,預設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="true"/><!-- 是否取消註釋 --> <property name="suppressDate" value="true" /> <!-- 是否生成註釋代時間戳--> </commentGenerator> <!-- jdbc連線 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8" userId="root" password="123456"> </jdbcConnection> <!-- 型別轉換 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自動轉化以下型別(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成的實體包路徑,包含實體類和Example類(用來定製sql的) --> <javaModelGenerator targetPackage="com.rambo.domain" targetProject="src/main/java" /> <!-- 生成的Mapper.xml路徑 --> <sqlMapGenerator targetPackage="mybatis-mapping" targetProject="src/main/resources" /> <!-- 生成的mapper類 --> <javaClientGenerator targetPackage="com.rambo.dao" targetProject="src/main/java" type="XMLMAPPER" /> <!-- table可以有多個,每個資料庫中的表都可以寫一個table,tableName表示要匹配的資料庫表,--> <!-- 也可以在tableName屬性中通過使用%萬用字元來匹配所有資料庫表,只有匹配的表才會自動生成檔案 --> <table schema="" tableName="tb_user" domainObjectName="User" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"> <property name="useActualColumnNames" value="false" /> <generatedKey column="id" sqlStatement="JDBC" identity="true" /> </table> </context> </generatorConfiguration>