好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

一、简介

mybatis -geneator是一款mybatis自动代码生成工具,可以通过配置,自动生成entity、mapper和xml文件。

二、配置(配置的话  按着我这个来配置吧 !  )

在pom文件的<build>下的<plugins>添加以下配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<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

配置的信息如下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

<?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都已经生成好了 

看看生成的文件

已经成功了  !!!(点个赞吧!)

再来说说    遇到的一些问题吧!

报错的代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

[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

加上就可以解决了   

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

原文链接:https://blog.csdn.net/qq_37350706/article/details/81429154

查看更多关于详解mybatis.generator配上最新的mysql 8.0.11的一些坑的详细内容...

  阅读:10次