虽然MyBatis很方便,可是想要手写所有的mapper仍是很累人的,好在MyBatis官方推出了自动化工具,能够根据数据库和定义好的配置直接生成DAO层及如下的所有代码,很是方便.java
须要注意的是,虽然自动化工具须要一个配置文件,可是MyBatis的配置文件仍然不能少,自动化工具的配置文件用于对生成的代码的选项进行配置,MyBatis的配置文件才是运行时的主要配置文件.web
下面我要说的是针对cmd下操做的方法sql
Mybatis-Generator 使用的是 mybatis-generator-core-1.3.1.jar数据库
本身用的是DB2数据库,因此用的是 db2jcc-1.4.2.jarmybatis
一份配置文件: generator.xmlapp
<?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:数据库的JDBC驱动--> <classPathEntry location="db2jcc-1.4.2.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 去除自动生成的注释 --> <commentGenerator> <property name="suppressAllComments" value="false" /> </commentGenerator> <jdbcConnection driverClass="com.ibm.db2.jcc.DB2Driver" connectionURL="jdbc:db2://128.232.9.214:50000/esnew:traceLevel=3;driverType=4;" userId="eser" password="eservice123"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:自动生成代码的位置 --> <javaModelGenerator targetPackage="cn.com.sinosoft.domain" targetProject="D:/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="cn.com.sinosoft.dao" targetProject="D:/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.com.sinosoft.dao" targetProject="D:/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="GE_PRODUCT_SALE_AREA" domainObjectName="GEProductSaleArea" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" > <property name="useActualColumnNames" value="true"/> </table> </context> </generatorConfiguration>
具体操做:dom
1.在 D:\SVN\dev\站点\iwebsite\src\test\resources 路径下有以下文件:工具
2. CMD 命令进入到 D:\SVN\dev\站点\iwebsite\src\test\resources.net
3. 点击命令code
java -jar mybatis-generator-core-1.3.1.jar -configfile generator.xml -overwrite
4.而后,你能够在mapper.xml中做出一些修改,使适合你的项目。
本人是参考:http://blog.csdn.net/wyc_cs/article/details/9023117