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的路徑
                     不寫默認在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下創建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:設置生成的文件適用於那個 mybatis 版本 -->
    <context id="default" targetRuntime="MyBatis3">

        <!--optional,旨在創建class時,對註釋進行控制-->
        <commentGenerator>
            <property name="suppressDate" value="true" />
            <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--jdbc的數據庫連接-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/ajyl_medical_model?serverTimezone=UTC"
                        userId="root"
                        password="123456">
        </jdbcConnection>

        <!--非必須,類型處理器,在數據庫類型和java類型之間的轉換控制-->
        <javaTypeResolver>
            <!-- 默認情況下數據庫中的 decimal,bigInt 在 Java 對應是 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添加構造函數 -->
            <property name="constructorBased" value="true" />
            <!-- 是否清理從數據庫中查詢出的字符串左右兩邊的空白字符 -->
            <property name="trimStrings" value="true" />
            <!-- 建立modal對象是否不可改變 即生成的modal對象不會有setter方法,只有構造方法 -->
            <property name="immutable" value="false" />
        </javaModelGenerator>

        <!-- targetPackage 和 targetProject:生成的 mapper 文件的包和位置 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resource">
            <!-- 針對數據庫的一個配置,是否把 schema 作爲字包名 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- targetPackage 和 targetProject:生成的 interface 文件的包和位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.ajyl.modules.asset.dao" targetProject="src/main/java">
            <!-- 針對 oracle 數據庫的一個配置,是否把 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>

(複製走改改就好!  )

這裏提一下要注意的地方啊!

因爲用的是mysql-8.0.11

所以配置有所不同  

相信你們用8.0.11啓動項目連接數據庫的時候就遇到過了

主要就是新版本有新特性,首先,最新官方支持將com.mysql.jdbc.Driver改爲com.mysql.cj.jdbc.Driver,此外mysql8.0是不需要建立ssl連接的,你需要顯示關閉,即url中的useSSL=false;最後你需要設置CST,CST可視爲美國、澳大利亞、古巴或中國的標準時間。serverTimezone是設置時區的,大家可以查一下相關資料瞭解一下哦!。

這樣一配置 就成功了  現在我們來測試一下  吧!

在右側打開maven面板在Plugin下打開Mybatis-generator下的mybatis-generator:fenerate

右鍵Run它!

配置沒錯就會一路啓動成功   entity mapper xml都已經生成好了 

看看生成的文件

已經成功了  !!!(點個贊吧!)

 

 

再來說說    遇到的一些問題吧!

報錯的代碼

[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. ->

說沒有給他使用時區   請給他設置一個具體的時區值

我們就得在connectionURL的配置上加        ?serverTimezone=UTC

加上就可以解決了   

OVER  安排

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章