首页 > 编程 > Java > 正文

详解mybatis.generator配上最新的mysql 8.0.11的一些坑

2019-11-26 09:37:31
字体:
来源:转载
供稿:网友

一、简介

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/MojoExecutionExceptionProcess finished with exit code 1

拉到后面看报 to use a more specifc time zone value if you want to utilize time zone support. ->

说没有给他使用时区   请给他设置一个具体的时区值

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

加上就可以解决了   

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表