好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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

查看更多关于Mybatis Generator 获取不到字段注释的解决的详细内容...

  阅读:36次