欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis Generator 獲取不到字段注釋的解決

 更新時間:2021年09月08日 15:04:58   作者:一頭磕在鍵盤上  
這篇文章主要介紹了Mybatis Generator 獲取不到字段注釋的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Mybatis Generator 獲取不到字段注釋

環(huán)境限制,暫時只提供Oracle和Mysql的解決方法,其它數(shù)據(jù)庫如果遇到同樣問題,原理是一樣的,具體就看該數(shù)據(jù)庫應(yīng)當(dā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進(jìn)行,而通過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è)置進(jìn)去了.那么顯然我們只需要找到在哪配置這些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)文章

  • MyBatis嵌套查詢collection報錯:org.apache.ibatis.exceptions.TooManyResultsException

    MyBatis嵌套查詢collection報錯:org.apache.ibatis.exceptions.TooMany

    本文主要介紹了MyBatis嵌套查詢collection報錯:org.apache.ibatis.exceptions.TooManyResultsException,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-09-09
  • Spring?Security短信驗證碼實現(xiàn)詳解

    Spring?Security短信驗證碼實現(xiàn)詳解

    本文主要介紹了Spring?Security短信驗證碼的實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-11-11
  • MyBatis-Plus動態(tài)返回實體類示例詳解

    MyBatis-Plus動態(tài)返回實體類示例詳解

    這篇文章主要為大家介紹了MyBatis-Plus動態(tài)返回實體類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • IDEA集成MyBatis Generator插件的使用

    IDEA集成MyBatis Generator插件的使用

    這篇文章主要介紹了IDEA集成MyBatis Generator插件的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • IDEA如何添加配置文件到classpath中

    IDEA如何添加配置文件到classpath中

    這篇文章主要介紹了IDEA如何添加配置文件到classpath中,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • ServletContext中常用方法介紹

    ServletContext中常用方法介紹

    本篇文章是對ServletContext中的常用方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-07-07
  • Spring之Environment類的使用方式

    Spring之Environment類的使用方式

    這篇文章主要介紹了Spring之Environment類的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 關(guān)于各種排列組合java算法實現(xiàn)方法

    關(guān)于各種排列組合java算法實現(xiàn)方法

    這篇文章介紹了幾種用JAVA實現(xiàn)的排列組合算法,有需要的朋友可以參考一下
    2013-06-06
  • eclipse創(chuàng)建springboot項目的三種方式總結(jié)

    eclipse創(chuàng)建springboot項目的三種方式總結(jié)

    這篇文章主要介紹了eclipse創(chuàng)建springboot項目的三種方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • tcc分布式事務(wù)框架體系解析

    tcc分布式事務(wù)框架體系解析

    這篇文章主要為大家介紹了tcc分布式事務(wù)框架體系結(jié)構(gòu)的解析說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03

最新評論