Mybatis Generator 獲取不到字段注釋的解決
Mybatis Generator 獲取不到字段注釋
環(huán)境限制,暫時只提供Oracle和Mysql的解決方法,其它數(shù)據(jù)庫如果遇到同樣問題,原理是一樣的,具體就看該數(shù)據(jù)庫應(yīng)當去配置哪個屬性.
解決方法
下面的配置均指的是Mybatis Generator 的配置文件(一般是叫g(shù)eneratorConfig.xml)的配置:
Oracle 數(shù)據(jù)庫
<jdbcConnection driverClass="${driver}"
connectionURL="{url}" userId="${username}" password="${password}">
<!-- 針對oracle數(shù)據(jù)庫 -->
<property name="remarksReporting" value="true"></property>
</jdbcConnection>
MySql 數(shù)據(jù)庫
方法1
<jdbcConnection driverClass="${driver}"
connectionURL="{url}" userId="${username}" password="${password}">
<!-- 針對mysql數(shù)據(jù)庫 -->
<property name="useInformationSchema" value="true"></property>
</jdbcConnection>
方法2
mysql的connectionURL中添加 useInformationSchema=true.大體上就是:
connectionURL="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&useInformationSchema=true"
兩種方法任選其一.
詳解
MBG訪問數(shù)據(jù)庫也是通過JDBC進行,而通過JDBC連接Oracle、Mysql(其它數(shù)據(jù)庫暫不清楚)時,想獲取到表及字段注釋是需要額外設(shè)置一些連接屬性的.一般大體上都是如下的代碼(以O(shè)racle為例):
Properties props =newProperties();
props.put("remarksReporting","true");//Oracle
dbConn = DriverManager.getConnection(url, props);
DatabaseMetaData dbmd = dbConn.getMetaData();
這樣通過JDBC就能獲取到表或者字段的注釋了.
那么在MBG中怎么設(shè)置呢?總不能去改源碼吧.其實MBG自身已經(jīng)提供了解決方法.
我們先來看下MBG連接數(shù)據(jù)庫的代碼,可以在org.mybatis.generator.internal.JDBCConnectionFactory中找到:
//以下代碼來自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給設(shè)置進去了.那么顯然我們只需要找到在哪配置這些properties就行了.
JDBCConnectionConfiguration對應(yīng)到XML配置里就是jdbcConnection節(jié)點.
再來看看官方的使用文檔,官方文檔關(guān)于jdbcConnection (點擊查看) 一節(jié)中 <property>子元素的說明:
<property> (0..N) Note: any properties specified here will be added to the properties of the JDBC driver.
那么在配置文件中我們?nèi)缦赂膭蛹纯?
<jdbcConnection driverClass="${driver}"
connectionURL="{url}" userId="${username}" password="${password}">
<!-- 針對oracle數(shù)據(jù)庫 -->
<property name="remarksReporting" value="true"></property>
</jdbcConnection>
關(guān)于如何生成自定義注釋,參見 mybatis-generator自定義注釋生成
mybatis-generator生成數(shù)據(jù)表中注釋
1.克隆項目
打jar包
git clone https://github.com/backkoms/mybatis-generator-comments.git
編譯打包,install到本地或delopy私服庫中均可。
2.修改pom文件
<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.配置對應(yīng)的解析生成包
<commentGenerator type="org.mybatis.generator.internal.CustomeCommentGenerator">
<property name="javaFileEncoding" value="UTF-8"/>
<property name="suppressDate" value="true" />
<property name="suppressAllComments" value="false" />
</commentGenerator>
執(zhí)行命令:mvn mybatis-generator:generate
查看執(zhí)行生成文件

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
半小時實現(xiàn)Java手擼網(wǎng)絡(luò)爬蟲框架(附完整源碼)
最近在做一個搜索相關(guān)的項目,需要爬取網(wǎng)絡(luò)上的一些鏈接存儲到索引庫中,自己寫了一個簡單的網(wǎng)絡(luò)爬蟲,感興趣的可以了解一下2021-06-06

