詳解mybatis.generator配上最新的mysql 8.0.11的一些坑
一、簡介
mybatis-geneator是一款mybatis自動代碼生成工具,可以通過配置,自動生成Entity、mapper和xml文件。
二、配置(配置的話 按著我這個來配置吧 ! )
在pom文件的<build>下的<plugins>添加以下配置
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>
<!--這里是配置generatorConfig.xml的路徑
不寫默認(rèn)在resources目錄下找generatorConfig.xml文件
-->
</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
</plugin>
再在resources下創(chuàng)建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>
<!-- context 是逆向工程的主要配置信息 -->
<!-- id:起個名字 -->
<!-- targetRuntime:設(shè)置生成的文件適用于那個 mybatis 版本 -->
<context id="default" targetRuntime="MyBatis3">
<!--optional,旨在創(chuàng)建class時,對注釋進(jìn)行控制-->
<commentGenerator>
<property name="suppressDate" value="true" />
<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--jdbc的數(shù)據(jù)庫連接-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ajyl_medical_model?serverTimezone=UTC" userId="root" password="123456"></jdbcConnection>
<!--非必須,類型處理器,在數(shù)據(jù)庫類型和java類型之間的轉(zhuǎn)換控制-->
<javaTypeResolver>
<!-- 默認(rèn)情況下數(shù)據(jù)庫中的 decimal,bigInt 在 Java 對應(yīng)是 sql 下的 BigDecimal 類 -->
<!-- 不是 double 和 long 類型 -->
<!-- 使用常用的基本類型代替 sql 包下的引用類型 -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetPackage:生成的實體類所在的包 -->
<!-- targetProject:生成的實體類所在的硬盤位置 -->
<javaModelGenerator targetPackage="com.ajyl.modules.asset.entity"
targetProject="src/main/java">
<!-- 是否允許子包 -->
<property name="enableSubPackages" value="false" />
<!-- 是否對modal添加構(gòu)造函數(shù) -->
<property name="constructorBased" value="true" />
<!-- 是否清理從數(shù)據(jù)庫中查詢出的字符串左右兩邊的空白字符 -->
<property name="trimStrings" value="true" />
<!-- 建立modal對象是否不可改變 即生成的modal對象不會有setter方法,只有構(gòu)造方法 -->
<property name="immutable" value="false" />
</javaModelGenerator>
<!-- targetPackage 和 targetProject:生成的 mapper 文件的包和位置 -->
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resource">
<!-- 針對數(shù)據(jù)庫的一個配置,是否把 schema 作為字包名 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage 和 targetProject:生成的 interface 文件的包和位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.ajyl.modules.asset.dao" targetProject="src/main/java">
<!-- 針對 oracle 數(shù)據(jù)庫的一個配置,是否把 schema 作為字包名 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<table tableName="asset_product_feedback" domainObjectName="AssetProductFeedback"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
(復(fù)制走改改就好! )
這里提一下要注意的地方啊!
因為用的是mysql-8.0.11
所以配置有所不同
相信你們用8.0.11啟動項目連接數(shù)據(jù)庫的時候就遇到過了
主要就是新版本有新特性,首先,最新官方支持將com.mysql.jdbc.Driver改為com.mysql.cj.jdbc.Driver,此外mysql8.0是不需要建立ssl連接的,你需要顯示關(guān)閉,即url中的useSSL=false;最后你需要設(shè)置CST,CST可視為美國、澳大利亞、古巴或中國的標(biāo)準(zhǔn)時間。serverTimezone是設(shè)置時區(qū)的,大家可以查一下相關(guān)資料了解一下哦!。

這樣一配置 就成功了 現(xiàn)在我們來測試一下 吧!
在右側(cè)打開maven面板在Plugin下打開Mybatis-generator下的mybatis-generator:fenerate
右鍵Run它!

配置沒錯就會一路啟動成功 entity mapper xml都已經(jīng)生成好了

看看生成的文件

已經(jīng)成功了 !?。。c個贊吧?。?/p>
再來說說 遇到的一些問題吧!

報錯的代碼
[INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.581 s [INFO] Finished at: 2018-08-05T11:51:49+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate (default-cli) on project smart-campus: 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] [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 Process finished with exit code 1
拉到后面看報 to use a more specifc time zone value if you want to utilize time zone support. ->
說沒有給他使用時區(qū) 請給他設(shè)置一個具體的時區(qū)值
我們就得在connectionURL的配置上加 ?serverTimezone=UTC

加上就可以解決了
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java如何導(dǎo)出多個excel并打包壓縮成.zip文件
本文介紹了Java如何導(dǎo)出多個excel文件并將這些文件打包壓縮成zip格式,首先,需要從數(shù)據(jù)庫中獲取數(shù)據(jù)并導(dǎo)出到指定位置形成excel文件,接著,將這些數(shù)據(jù)分散到不同的excel文件中,最后,使用相關(guān)的Java工具類對這些excel文件進(jìn)行打包壓縮2024-09-09
springboot + rabbitmq 如何實現(xiàn)消息確認(rèn)機制(踩坑經(jīng)驗)
這篇文章主要介紹了springboot + rabbitmq 如何實現(xiàn)消息確認(rèn)機制,本文給大家分享小編實際開發(fā)中的一點踩坑經(jīng)驗,內(nèi)容簡單易懂,需要的朋友可以參考下2020-07-07
JAVA中的靜態(tài)代理、動態(tài)代理以及CGLIB動態(tài)代理總結(jié)
本篇文章主要介紹了JAVA中的靜態(tài)代理、動態(tài)代理以及CGLIB動態(tài)代理總結(jié),具有一定的參考價值,有興趣的可以了解一下2017-08-08
將List集合中的map對象轉(zhuǎn)為List<對象>形式實例代碼
這篇文章主要介紹了將List集合中的map對象轉(zhuǎn)為List<對象>形式實例代碼,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
Java使用CompletableFuture進(jìn)行非阻塞IO詳解
這篇文章主要介紹了Java使用CompletableFuture進(jìn)行非阻塞IO詳解,CompletableFuture是Java中的一個類,用于支持異步編程和處理異步任務(wù)的結(jié)果,它提供了一種方便的方式來處理異步操作,并允許我們以非阻塞的方式執(zhí)行任務(wù),需要的朋友可以參考下2023-09-09
Idea 2020.2安裝MyBatis Log Plugin 不可用的解決方法
小編在使用時發(fā)現(xiàn)Idea 2020.2 MyBatis Log Plugin 收費了,這個可以替代用,小編特此把解決方案分享到腳本之家平臺供大家參考,感興趣的朋友一起看看吧2020-11-11
IDEA 當(dāng)前在線人數(shù)和歷史訪問量的示例代碼
這篇文章主要介紹了IDEA 當(dāng)前在線人數(shù)和歷史訪問量的實例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
Mybatis-plus自動填充不生效或自動填充數(shù)據(jù)為null原因及解決方案
本文主要介紹了Mybatis-plus自動填充不生效或自動填充數(shù)據(jù)為null原因及解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05


