mybatis-generator:generate 生成代碼配置踩坑不少,在此留下筆記以便后續填坑
一、mysql返回時間問題
- 錯誤信息:
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project coisini-mango-server:
The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone.
You must configure either the server or JDBC driver (via the serverTimezone configuration property)
to use a more specifc time zone value if you want to utilize time zone support. -> [Help 1]
JDBC需要配置服務器時區值, 在jdbc連接的url后面加上serverTimezone=GMT即可解決問題,如果使用GMT+8時區,需要寫成GMT%2B8
,如下:
- application.properties的寫法:
url: jdbc:mysql://localhost:3306/coisini?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
- generator.xml的寫法,注:在xml中寫
&
,會因為未轉義而報錯要將&
寫成&
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/coisini?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false" userId="root" password="sunday"></jdbcConnection>
二、找不到數據庫連接驅動包
- 錯誤信息:
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli)
on project coisini-mango-server: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-
plugin:1.3.2:generate failed: Exception getting JDBC Driver: com.mysql.cj.jdbc.Driver -> [Help 1]
關於這個異常,網上大多數說在generatorConfig.xml中加入數據庫驅動包的絕對路徑,
方法一:
<classPathEntry location="G:\Program Files\java\apache-maven-3.6.1\repository\mysql\mysql-connector-java\8.0.13\mysql-connector-java-8.0.13.jar"/>
采用絕對路徑后,編譯會報一個棄用警告
- 警告信息:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.
The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
這里就引出了我們解決找不到數據庫連接驅動包的辦法,
方法二:
- 更改mysql驅動類為com.mysql.cj.jdbc.Driver,
application.properties
和generator.xml
都要改 - 在pom.xml中plugin節點下添加mysql-connector-java的
dependency
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/generator/generator.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
spring boot本就是通過maven自動配置,這里建議采用方法二,不建議使用配置絕對路徑。