Mybatis Generator 获取不到字段注释 html
环境限制,暂时只提供Oracle和Mysql的解决方法,其它数据库若是遇到一样问题,原理是同样的,具体就看该数据库应当去配置哪一个属性.java
下面的配置均指的是Mybatis Generator 的配置文件(通常是叫generatorConfig.xml)的配置:mysql
<jdbcConnection driverClass="${driver}" connectionURL="{url}" userId="${username}" password="${password}"> <!-- 针对oracle数据库 --> <property name="remarksReporting" value="true"></property> </jdbcConnection>
<jdbcConnection driverClass="${driver}" connectionURL="{url}" userId="${username}" password="${password}"> <!-- 针对mysql数据库 --> <property name="useInformationSchema" value="true"></property> </jdbcConnection>
mysql的connectionURL中添加 useInformationSchema=true
.大致上就是:spring
connectionURL="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&useInformationSchema=true"
两种方法任选其一.sql
MBG访问数据库也是经过JDBC进行,而经过JDBC链接Oracle、Mysql(其它数据库暂不清楚)时,想获取到表及字段注释是须要额外设置一些链接属性的.通常大致上都是以下的代码(以Oracle为例):数据库
Properties props =newProperties(); props.put("remarksReporting","true");//Oracle dbConn = DriverManager.getConnection(url, props); DatabaseMetaData dbmd = dbConn.getMetaData();
这样经过JDBC就能获取到表或者字段的注释了.bash
那么在MBG中怎么设置呢?总不能去改源码吧.其实MBG自身已经提供了解决方法.网络
咱们先来看下MBG链接数据库的代码,能够在org.mybatis.generator.internal.JDBCConnectionFactory
中找到:mybatis
//如下代码来自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就好了.oracle
JDBCConnectionConfiguration
对应到XML配置里就是jdbcConnection
节点.
再来看看官方的使用文档,官方文档关于jdbcConnection (点击查看)
一节中 <property>
子元素的说明:
<property> (0..N) Note: any properties specified here will be added to the properties of the JDBC driver.
那么在配置文件中咱们以下改动便可:
<jdbcConnection driverClass="${driver}" connectionURL="{url}" userId="${username}" password="${password}"> <!-- 针对oracle数据库 --> <property name="remarksReporting" value="true"></property> </jdbcConnection>
1 目录
2 generate.bat
java -jar mybatis-generator-core-1.3.7.jar -configfile generator_oracle.xml -overwrite
3 generator_oracle.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration> <!-- 在项目中会自动找到 --> <classPathEntry location="ojdbc14-10.2.0.4.0.jar" /> <!-- <properties resource="D:\\workspace-demo\\mybatis-generator-core\\bin\\config_oracle.properties" /> --> <context id="context1"> <!--oracle.jdbc.driver.OracleDriver or com.mysql.jdbc.Driver --> <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@192.168.5.11:1521:ORCL" userId="USER" password="PASS"> </jdbcConnection> <!-- model --> <javaModelGenerator targetPackage="com.spinach.business.test.entity" targetProject="."> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- mapper --> <sqlMapGenerator targetPackage="com.spinach.business.test.dao" targetProject="."> <property name="enableSubPackages" value="true" /> <property name="methodNameCalculator" value="extended" /> </sqlMapGenerator> <!-- dao --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.spinach.business.test.dao" targetProject="."> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- schema="dbname" 网络上说:schema是数据名,加了反而不能生成。 --> <table tableName="t_blxx_tbl" enableCountByExample="false" enableUpdateByExample="false" enableUpdateByPrimaryKey="false" enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableSelectByPrimaryKey="false" enableSelectByExample="false" selectByExampleQueryId="false" enableInsert="true" > <property name="rootClass" value="com.spinach.support.spring.mybatis.entity.MybatisEntity" /> </table> </context> </generatorConfiguration>