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>