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中写
&
,会因为未转义而报错要将&
写成&
二、找不到数据库连接驱动包
- 错误信息:
[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中加入数据库驱动包的绝对路径,
方法一:
采用绝对路径后,编译会报一个弃用警告
- 警告信息:
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
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
src/main/resources/generator/generator.xml
true
true
spring boot本就是通过maven自动配置,这里建议采用方法二,不建议使用配置绝对路径。