SpringBoot 整合 Mybatis Generator自動生成程式碼
阿新 • • 發佈:2018-12-11
Mybatis是目前主流的ORM框架,相比於hibernate的全自動,它是半自動化需要手寫sql語句、介面、實體物件,後來推出的Generator自動生成程式碼,可以幫我們提高開發效率。
本文目的:SpringBoot 整合 Mybatis Generator自動生成dao、entity、mapper.xml實現單表增刪改查。
1.建立SpringBoot專案
File→New→Project… 選擇Spring Initializr,選擇JDK版本,預設初始化URL 填寫專案名稱,java版本,其他描述資訊 選擇專案存放路徑 選擇web、mybatis、mysql依賴 Next–>Finish完成專案建立
2. mybatis-generator-maven外掛的配置
開啟專案的pom.xml檔案新增
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin>
完整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.xyz</groupId> <artifactId>mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mybatis</name> <description>Spring Boot 整合 Mybatis</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.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> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build> </project>
3. 專案結構構建
在專案目錄下(這裡是mybatis)新增controller、service、dao、entity包,在resources下新增mapper包存放對映檔案。
4. application.yml配置
#埠號配置
server:
port: 8088
spring:
#模板引擎配置
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML
encoding: UTF-8
cache: false
servlet:
content-type: text/html
#靜態檔案配置
resources:
static-locations: classpath:/static,classpath:/META-INF/resources,classpath:/templates/
#jdbc配置
datasource:
url: jdbc:mysql://localhost:3306/video?useUnicode=true&characterEncoding=utf8
username: xyz
password: xyz
driver-class-name: com.mysql.jdbc.Driver
#mybatis配置
mybatis:
#對映檔案路徑
mapper-locations: classpath:mapper/*.xml
#模型所在的保命
type-aliases-package: com.xyz.mybatis.entity
5. generatorConfig.xml配置
在resources檔案下建立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>
<!--classPathEntry:資料庫的JDBC驅動,換成你自己的驅動位置 可選 -->
<classPathEntry location="E:\IdeaProjects\mysql-connector-java-5.1.47.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="true"/><!-- 是否取消註釋 -->
<property name="suppressDate" value="false"/> <!-- 是否生成註釋代時間戳-->
</commentGenerator>
<!-- jdbc連線-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/video?serverTimezone=UTC" userId="xyz"
password="xyz"/>
<!-- 型別轉換 -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自動轉化以下型別(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成實體類地址 -->
<javaModelGenerator targetPackage="com.xyz.mybatis.entity" targetProject="src/main/java">
<!-- 是否讓schema作為包的字尾 -->
<property name="enableSubPackages" value="false"/>
<!-- 從資料庫返回的值去掉前後空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成map.xml檔案存放地址 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成介面dao -->
<javaClientGenerator targetPackage="com.xyz.mybatis.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- table可以有多個,每個資料庫中的表都可以寫一個table,tableName表示要匹配的資料庫表,也可以在tableName屬性中通過使用%萬用字元來匹配所有資料庫表,只有匹配的表才會自動生成檔案 enableSelectByPrimaryKey相應的配置表示是否生成相應的介面 -->
<table tableName="t_manager" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"
enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true">
<property name="useActualColumnNames" value="true"/>
</table>
</context>
</generatorConfiguration>
注意: classPathEntry location=“E:\IdeaProjects\mysql-connector-java-5.1.47.jar”,建議用5.X系列的,否則可能生成的介面會缺少
6. 在idea中新增一個mybatis generator maven外掛啟動選項,點選Run,選擇Edit Configuration… 點選加號"+"新增,選擇maven,填寫名稱(這裡用mybatis generator),命令列:mybatis-generator:generate -e
7. 選擇 Mybatis Generator 啟動,自動在dao、entity、mapper包下生成程式碼
注意: 利用Mybatis Generator自動生成程式碼,對於已經存在的檔案會存在覆蓋和在原有檔案上追加的可能性,不宜多次生成。如需重新生成,需要刪除已生成的原始檔。