小编在一次使用mybatis-generator生成映射时,由于从mysql5切换到了mysql8.0,报了如下错误:

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------
[INFO] Building mybatis-generator 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.7:generate (default-cli) @ mybatis-generator ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.497 s
[INFO] Finished at: 2019-04-09T21:47:48+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.7:generate (default-cli) on project mybatis-generator: Could not create connection to database server. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

错误分析:

  • 报错为:Could not create connection to database server.
    这应该是我的连接有问题,但是我当时并找不出错误;
    经过一番尝试发现我的驱动问题(Mysql8.0+驱动名为com.mysql.cj.jdbc.Driver),但是当我修改后,发现mysql-connector-java的jar包也存在问题,version由5.1.35改为8.0.15;修改再次尝试发现开始报明显的timezone问题,这时猜测到是jdbc连接参数缺少serverTimezone,最后完美解决。

1. 修改后的maven引入mysql连接包的版本:


    org.mybatis.generator
    mybatis-generator-maven-plugin
    1.3.7
    
        
            mysql
            mysql-connector-java
            8.0.15
        
    
    

2. MySQL8.0+的数据库连接驱动从com.mysql.jdbc.Driver更换到了com.mysql.cj.jdbc.Driver

3. MySQL8.0+的数据库连接需要指定serverTimezone=UTC;
如果直接在jdbc连接URL中添加serverTimezone=UTC会报错误
,如下:

[ERROR] XML Parser Error on line 27: 对实体 "serverTimezone" 的引用必须以 ';' 分隔符结尾。

只需要修改为”&serverTimezone=UTC”即可,如下例子:**



文章来源于互联网:MySQL8的jdbc配置错误问题(MySQL8数据库变动)

发表评论