springboot整合mybatis外掛使用逆向工程生成domain
LZ為了熟悉springboot,準備寫一個視訊分享網站。而在整合spirngboot和mybatis以及tk外掛逆向生成工程時出現了很多問題。
在專案終於可以從資料庫中獲取資料之後,LZ將碰到的一些很難受的錯誤記錄下來。僅供大家參考。
LZ最近才開始用上idea,以前用的都是eclipse。所以也是用這個專案熟悉idea。
- 首先pom檔案
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<mybatis.version>1.3.1</mybatis.version>
<mapper.version>3.4.3</mapper.version>
<mybatis-generator.version>1.3.3</mybatis-generator.version>
<mybatis-tk.version>1.1.4</mybatis-tk.version>
<pagehelper.version>1.2.1</pagehelper.version>
<mybatis.generator.version>1.3.5</mybatis.generator.version>
</properties>
<dependencies>
<!-- SpringBoot - Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot - MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- SpringBoot - MyBatis 逆向工程 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>${mybatis-generator.version}</version>
</dependency>
<!-- MyBatis 通用 Mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mybatis-tk.version}</version>
</dependency>
<!-- Mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- MyBatis 逆向工程 外掛 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${mybatis.generator.version}</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>${mapper.version}</version>
</dependency>
</dependencies>
<configuration>
<!-- 允許移動生成的檔案 -->
<verbose>true</verbose>
<!-- 是否覆蓋 -->
<overwrite>true</overwrite>
<!-- 配置檔案 -->
<configurationFile>
${basedir}/src/main/resources/generator/generatorConfig.xml
</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
LZ試過了很多的plugins的寫法,發現還是這個比較適合我。依賴的話我這裡缺少一些單元測試和webservice這類的依賴。大家需要自己補上。
好了,接下來便是很關鍵的地方了:依賴的版本問題。
LZ在逆向工程和測試controller的時候,發現一直報出 Could not get JDBC Connection;
這裡很坑的原因是因為LZ的mac上mysql的版本是8以上的。所以當你的mysql依賴的版本比較低就會報錯了。
隨後便是逆向生成工具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>
<!-- 引入 application.properties -->
<properties resource="application.properties" />
<!-- MyBatis3Simple:不生成 Example相關類及方法 -->
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`" />
<property name="endingDelimiter" value="`" />
<!-- 指定生成 Mapper 的繼承模板 -->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="${generator.mappers}" />
</plugin>
<!-- 生成 JavaBean 物件重寫 toString方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<!-- 生成 JavaBean 物件繼承 Serializable 類 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<!-- 生成 JavaBean 物件重寫 equals 和 hashCode 方法 -->
<!-- <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" /> -->
<!-- jdbc 連線配置 -->
<jdbcConnection driverClass="${spring.datasource.driver-class-name}"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}">
</jdbcConnection>
<javaModelGenerator targetPackage="${generator.javaModel-targetPackage}"
targetProject="${generator.targetProject}" />
<sqlMapGenerator targetPackage="${generator.sqlMap-targetPackage}"
targetProject="${generator.targetProject}" />
<javaClientGenerator targetPackage="${generator.javaClient-targetPackage}"
targetProject="${generator.targetProject}" type="XMLMAPPER" />
<!-- Mysql 配置 -->
<!-- <generatedKey column="id" sqlStatement="Mysql" identity="true" /> -->
<!-- Oracle 配置 -->
<!-- <generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/> -->
<!-- tableName:資料庫表名,domainObjectName:生成檔名 ,schema:資料來源-->
<table tableName="VEDIOUSER">
<!--mysql 配置-->
<generatedKey column="id" sqlStatement="Mysql" />
<!--oracle 配置-->
<!--<generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>-->
</table>
</context>
</generatorConfiguration>
這裡的引數你可以直接在裡面配置,也可以配置在application.properties裡面
server.port=8888
spring.datasource.url=jdbc:mysql://localhost:3306/vedioshare?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=w19960517
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# Generator
generator.targetProject=src/main/java
generator.mappers=com.vsw.common.base.IBaseMapper
generator.javaModel-targetPackage=com.vsw.domain
generator.sqlMap-targetPackage=com.vsw.mapper
generator.javaClient-targetPackage=com.vsw.mapper
mybatis.type-aliases-package= com.vsw.domain
mybatis.mapper-locations= classpath:mapper/*.xml
#mapper.mappers=tk.mybatis.mapper.common.Mapper
## - tk.mybatis.mapper.common.Mapper2
#
#mapper.mappernotEmpty=true
#mapper.identity= Mysql