• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Mybatis Generator 获取不到字段注释的解决

mybatis 搞代码 4年前 (2022-01-09) 10次浏览 已收录 0个评论

Mybatis Generator 获来1源gaodai#ma#com搞*代#码1网取不到字段注释

环境限制,暂时只提供Oracle和Mysql的解决方法,其它数据库如果遇到同样问题,原理是一样的,具体就看该数据库应当去配置哪个属性.

解决方法

下面的配置均指的是Mybatis Generator 的配置文件(一般是叫generatorConfig.xml)的配置:

Oracle 数据库

<jdbcConnection driverClass="${driver}"
    connectionURL="{url}" userId="${username}" password="${password}">
    <!-- 针对oracle数据库 -->
    <property name="remarksReporting" value="true"></property>
</jdbcConnection>

MySql 数据库

方法1

<jdbcConnection driverClass="${driver}"
    connectionURL="{url}" userId="${username}" password="${password}">
    <!-- 针对mysql数据库 -->
    <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访问数据库也是通过JDBC进行,而通过JDBC连接Oracle、Mysql(其它数据库暂不清楚)时,想获取到表及字段注释是需要额外设置一些连接属性的.一般大体上都是如下的代码(以Oracle为例):

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中找到:

//以下代码来自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;
}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Mybatis Generator 获取不到字段注释的解决

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址