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>
你的能量超乎你想象_________