Mybatis Generator 獲取不到字段注釋
環境限制,暫時只提供Oracle和Mysql的解決方法,其它數據庫如果遇到同樣問題,原理是一樣的,具體就看該數據庫應當去配置哪個屬性.
解決方法
下面的配置均指的是Mybatis Generator 的配置文件(一般是叫generatorConfig.xml)的配置:
Oracle 數據庫
1
2
3
4
5
|
<jdbcConnection driverClass= "${driver}" connectionURL= "{url}" userId= "${username}" password = "${password}" > <! -- 針對oracle數據庫 --> <property name = "remarksReporting" value= "true" ></property> </jdbcConnection> |
MySql 數據庫
方法1
1
2
3
4
5
|
<jdbcConnection driverClass= "${driver}" connectionURL= "{url}" userId= "${username}" password = "${password}" > <! -- 針對mysql數據庫 --> <property name = "useInformationSchema" value= "true" ></property> </jdbcConnection> |
方法2
mysql的connectionURL中添加 useInformationSchema=true.大體上就是:
1
|
connectionURL= "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&useInformationSchema=true" |
兩種方法任選其一.
詳解
MBG訪問數據庫也是通過JDBC進行,而通過JDBC連接Oracle、Mysql(其它數據庫暫不清楚)時,想獲取到表及字段注釋是需要額外設置一些連接屬性的.一般大體上都是如下的代碼(以Oracle為例):
1
2
3
4
|
Properties props =newProperties(); props.put( "remarksReporting" , "true" );//Oracle dbConn = DriverManager.getConnection(url, props); DatabaseMetaData dbmd = dbConn.getMetaData(); |
這樣通過JDBC就能獲取到表或者字段的注釋了.
那么在MBG中怎么設置呢?總不能去改源碼吧.其實MBG自身已經提供了解決方法.
我們先來看下MBG連接數據庫的代碼,可以在org.mybatis.generator.internal.JDBCConnectionFactory中找到:
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
|
//以下代碼來自Mybatis Generator 1.3.5 /** * This constructor is called when there is a JDBCConnectionConfiguration * specified in the configuration. * * @param config */ public JDBCConnectionFactory(JDBCConnectionConfiguration config) { super (); userId = config.getUserId(); password = config.getPassword(); connectionURL = config.getConnectionURL(); driverClass = config.getDriverClass(); otherProperties = config.getProperties(); //注意此行 } public Connection getConnection() throws SQLException { Driver driver = getDriver(); Properties props = new Properties(); if (stringHasValue(userId)) { props.setProperty( "user" , userId); //$NON-NLS-1$ } if (stringHasValue(password)) { props.setProperty( "password" , password); //$NON-NLS-1$ } props.putAll(otherProperties); //注意此行 Connection conn = driver.connect(connectionURL, props); if (conn == null ) { throw new SQLException(getString( "RuntimeError.7" )); //$NON-NLS-1$ } return conn; } |
通過上面代碼(尤其是我加了注意此行注釋的兩行代碼)我們可以看到,MBG在建立連接時,是把JDBCConnectionConfiguration中的所有properties給設置進去了.那么顯然我們只需要找到在哪配置這些properties就行了.
JDBCConnectionConfiguration對應到XML配置里就是jdbcConnection節點.
再來看看官方的使用文檔,官方文檔關于jdbcConnection (點擊查看) 一節中 <property>子元素的說明:
<property> (0..N) Note: any properties specified here will be added to the properties of the JDBC driver.
那么在配置文件中我們如下改動即可:
1
2
3
4
5
|
<jdbcConnection driverClass= "${driver}" connectionURL= "{url}" userId= "${username}" password = "${password}" > <! -- 針對oracle數據庫 --> <property name = "remarksReporting" value= "true" ></property> </jdbcConnection> |
關于如何生成自定義注釋,參見 mybatis-generator自定義注釋生成
mybatis-generator生成數據表中注釋
1.克隆項目
打jar包
git clone https://github.com/backkoms/mybatis-generator-comments.git
編譯打包,install到本地或delopy私服庫中均可。
2.修改pom文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< plugin > < groupId >org.mybatis.generator</ groupId > < artifactId >mybatis-generator-maven-plugin</ artifactId > < version >${plugins-mybatis-generator.version}</ version > < configuration > < verbose >true</ verbose > < overwrite >true</ overwrite > </ configuration > < dependencies > < dependency > < groupId >com.haier.hairy</ groupId > < artifactId >mybatis-generator-core</ artifactId > < version >1.0.1</ version > </ dependency > </ dependencies > </ plugin > |
3.配置對應的解析生成包
1
2
3
4
5
|
< commentGenerator type = "org.mybatis.generator.internal.CustomeCommentGenerator" > < property name = "javaFileEncoding" value = "UTF-8" /> < property name = "suppressDate" value = "true" /> < property name = "suppressAllComments" value = "false" /> </ commentGenerator > |
執行命令:mvn mybatis-generator:generate
查看執行生成文件
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_21251983/article/details/52849079