欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis-generator:generate 生成代码配置踩坑详解

程序员文章站 2022-04-22 08:24:43
...

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&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;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.propertiesgenerator.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自动配置,这里建议采用方法二,不建议使用配置绝对路径。

详细代码移步:mybatis 生成代码配置 mybatis-generator:generate 的使用详解