一、簡介
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下創(chuàng)建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,旨在創(chuàng)建 class 時,對注釋進行控制--> <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類型之間的轉換控制--> <javatyperesolver> <!-- 默認情況下數(shù)據(jù)庫中的 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添加構造函數(shù) --> <property name= "constructorbased" value= "true" /> <!-- 是否清理從數(shù)據(jù)庫中查詢出的字符串左右兩邊的空白字符 --> <property name= "trimstrings" value= "true" /> <!-- 建立modal對象是否不可改變 即生成的modal對象不會有setter方法,只有構造方法 --> <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> |
(復制走改改就好! )
這里提一下要注意的地方啊!
因為用的是mysql-8.0.11
所以配置有所不同
相信你們用8.0.11啟動項目連接數(shù)據(jù)庫的時候就遇到過了
主要就是新版本有新特性,首先,最新官方支持將com.mysql.jdbc.driver改為com.mysql.cj.jdbc.driver,此外mysql8.0是不需要建立ssl連接的,你需要顯示關閉,即url中的usessl=false;最后你需要設置cst,cst可視為美國、澳大利亞、古巴或中國的標準時間。servertimezone是設置時區(qū)的,大家可以查一下相關資料了解一下哦!。
這樣一配置 就成功了 現(xiàn)在我們來測試一下 吧!
在右側打開maven面板在plugin下打開mybatis-generator下的mybatis-generator:fenerate
右鍵run它!
配置沒錯就會一路啟動成功 entity mapper xml都已經(jīng)生成好了
看看生成的文件
已經(jīng)成功了 !!!(點個贊吧!)
再來說說 遇到的一些問題吧!
報錯的代碼
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. ->
說沒有給他使用時區(qū) 請給他設置一個具體的時區(qū)值
我們就得在connectionurl的配置上加 ?servertimezone=utc
加上就可以解決了
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_37350706/article/details/81429154