Mybatis Generator 獲取不到字段注釋的解決
Mybatis Generator 獲取不到字段注釋
環(huán)境限制,暫時(shí)只提供Oracle和Mysql的解決方法,其它數(shù)據(jù)庫(kù)如果遇到同樣問(wèn)題,原理是一樣的,具體就看該數(shù)據(jù)庫(kù)應(yīng)當(dāng)去配置哪個(gè)屬性.
解決方法
下面的配置均指的是Mybatis Generator 的配置文件(一般是叫g(shù)eneratorConfig.xml)的配置:
Oracle 數(shù)據(jù)庫(kù)
<jdbcConnection driverClass="${driver}" connectionURL="{url}" userId="${username}" password="${password}"> <!-- 針對(duì)oracle數(shù)據(jù)庫(kù) --> <property name="remarksReporting" value="true"></property> </jdbcConnection>
MySql 數(shù)據(jù)庫(kù)
方法1
<jdbcConnection driverClass="${driver}" connectionURL="{url}" userId="${username}" password="${password}"> <!-- 針對(duì)mysql數(shù)據(jù)庫(kù) --> <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訪問(wèn)數(shù)據(jù)庫(kù)也是通過(guò)JDBC進(jìn)行,而通過(guò)JDBC連接Oracle、Mysql(其它數(shù)據(jù)庫(kù)暫不清楚)時(shí),想獲取到表及字段注釋是需要額外設(shè)置一些連接屬性的.一般大體上都是如下的代碼(以O(shè)racle為例):
Properties props =newProperties(); props.put("remarksReporting","true");//Oracle dbConn = DriverManager.getConnection(url, props); DatabaseMetaData dbmd = dbConn.getMetaData();
這樣通過(guò)JDBC就能獲取到表或者字段的注釋了.
那么在MBG中怎么設(shè)置呢?總不能去改源碼吧.其實(shí)MBG自身已經(jīng)提供了解決方法.
我們先來(lái)看下MBG連接數(shù)據(jù)庫(kù)的代碼,可以在org.mybatis.generator.internal.JDBCConnectionFactory中找到:
//以下代碼來(lái)自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; }
通過(guò)上面代碼(尤其是我加了注意此行注釋的兩行代碼)我們可以看到,MBG在建立連接時(shí),是把JDBCConnectionConfiguration中的所有properties給設(shè)置進(jìn)去了.那么顯然我們只需要找到在哪配置這些properties就行了.
JDBCConnectionConfiguration對(duì)應(yīng)到XML配置里就是jdbcConnection節(jié)點(diǎn).
再來(lái)看看官方的使用文檔,官方文檔關(guān)于jdbcConnection (點(diǎn)擊查看) 一節(jié)中 <property>子元素的說(shuō)明:
<property> (0..N) Note: any properties specified here will be added to the properties of the JDBC driver.
那么在配置文件中我們?nèi)缦赂膭?dòng)即可:
<jdbcConnection driverClass="${driver}" connectionURL="{url}" userId="${username}" password="${password}"> <!-- 針對(duì)oracle數(shù)據(jù)庫(kù) --> <property name="remarksReporting" value="true"></property> </jdbcConnection>
關(guān)于如何生成自定義注釋,參見(jiàn) mybatis-generator自定義注釋生成
mybatis-generator生成數(shù)據(jù)表中注釋
1.克隆項(xiàng)目
打jar包
git clone https://github.com/backkoms/mybatis-generator-comments.git
編譯打包,install到本地或delopy私服庫(kù)中均可。
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.配置對(duì)應(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í)行生成文件
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis嵌套查詢collection報(bào)錯(cuò):org.apache.ibatis.exceptions.TooMany
本文主要介紹了MyBatis嵌套查詢collection報(bào)錯(cuò):org.apache.ibatis.exceptions.TooManyResultsException,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09Spring?Security短信驗(yàn)證碼實(shí)現(xiàn)詳解
本文主要介紹了Spring?Security短信驗(yàn)證碼的實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11MyBatis-Plus動(dòng)態(tài)返回實(shí)體類示例詳解
這篇文章主要為大家介紹了MyBatis-Plus動(dòng)態(tài)返回實(shí)體類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07關(guān)于各種排列組合java算法實(shí)現(xiàn)方法
這篇文章介紹了幾種用JAVA實(shí)現(xiàn)的排列組合算法,有需要的朋友可以參考一下2013-06-06eclipse創(chuàng)建springboot項(xiàng)目的三種方式總結(jié)
這篇文章主要介紹了eclipse創(chuàng)建springboot項(xiàng)目的三種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07