mybatis反轉數據庫的配置文件:
generatorConfig.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> <!-- 配置mysql驅動包,使用的絕對路徑 --> <classPathEntry location="G:\javalib\jdbc\mysql-connector-java-5.0.8-bin.jar"/> <context id="westward_mysql_tables" targetRuntime="MyBatis3"> <!-- 控制生成的代碼中的注釋 --> <commentGenerator> <property name="suppressAllComments" value="true"/> <property name="suppressDate" value="true"/> </commentGenerator> <!-- 數據庫連接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="yao" password="y123" /> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 數據表對應的model 層 --> <javaModelGenerator targetPackage="com.westward.bean" targetProject="src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- sql mapper 映射配置文件 --> <sqlMapGenerator targetPackage="com.westward.mapper" targetProject="src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 在ibatis2 中是dao層,但在mybatis3中,其實就是mapper接口 --> <javaClientGenerator targetPackage="com.westward.inter" type="XMLMAPPER" targetProject="src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要對那些數據表進行生成操作,必須要有一個. --> <table tableName="category" schema="mybatis" domainObjectName="Category" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" > </table> </context> </generatorConfiguration>
根據配置文件,生成對應的bean,接口,mapper的方法:
mybatis官網:http://www.mybatis.org/generator/running/running.html
給了好幾種方法:我就摘出兩種最常用的吧:
1.命令行的方式:
java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml -overwrite

注意jar包和xml文件的路徑
我的命令行是在項目根目錄下
2.java代碼的形式:
public static void main(String[] args) { List<String> warnings= new ArrayList<String>(); boolean overwrite= true; String genCfg= "G:/workspace10/mybatisgenerator/src/generatorConfig.xml"; File configFile= new File(genCfg); ConfigurationParser cp= new ConfigurationParser(warnings); Configuration config= null; try { config= cp.parseConfiguration(configFile); DefaultShellCallback callback= new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator= null; myBatisGenerator= new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); System.out.println(warnings); } catch (IOException e) { e.printStackTrace(); } catch (XMLParserException e) { e.printStackTrace(); } catch (InvalidConfigurationException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
其中集合warning,會存儲執行的錯誤信息,若無錯誤,則集合中無元素。
可能出現的錯誤:
[There are no statements enabled for table mybatis.category, this table will be ignored.]
原因:generatorConfig.xml中,<table>標簽配成了這樣,
把標紅的去掉就行了,標紅的默認是true,不需要顯示配成false.上邊的帶Example的配成false就行,帶Example的是指示例,這個基本不需要。
附上maven結構的web項目,一次構建多個表的配置:
<?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> <!-- 配置mysql驅動包,使用的絕對路徑 --> <classPathEntry location="G:\javalib\jdbc\mysql-connector-java-5.0.8-bin.jar"/> <context id="westward_mysql_tables" targetRuntime="MyBatis3"> <!-- 控制生成的代碼中的注釋 --> <commentGenerator> <property name="suppressAllComments" value="true"/> <property name="suppressDate" value="true"/> </commentGenerator> <!-- 數據庫連接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/bookestore" userId="yao" password="y123" /> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 數據表對應的model 層 --> <javaModelGenerator targetPackage="com.blue.bean" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- sql mapper 映射配置文件 --> <sqlMapGenerator targetPackage="com.blue.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 在ibatis2 中是dao層,但在mybatis3中,其實就是mapper接口 --> <javaClientGenerator targetPackage="com.blue.dao" type="XMLMAPPER" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要對那些數據表進行生成操作,必須要有一個. --> <table tableName="orders" schema="mybatis" domainObjectName="Order" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" > </table> <table tableName="orderitem" schema="mybatis" domainObjectName="OrderItem" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" > </table> <table tableName="users" schema="mybatis" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" > </table> <table tableName="products" schema="mybatis" domainObjectName="Product" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" > </table> </context> </generatorConfiguration>

