MyBatis中,可以使用Generator自动生成代码,包括DAO层、 MODEL层 、MAPPING SQL映射文件。 第一步:下载MyBatis的Generator工具 下载地址:http://code.google.com/p/mybatis/downloads/detail?name=mybatis-generator-core-1.3.1-bundle.zip&can=3&q=Product%3DGenerator 第二步:配置自动生成代码所需的XML配置文件,例如(generator.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:数据库的JDBC驱动,换成你自己的驱动位置 -->
<classPathEntry location="D:\libs\ojdbc14.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 去除自动生成的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@172.16.88.10:1521:mydb" userId="abc" password="abc">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:自动生成代码的位置 -->
<javaModelGenerator targetPackage="com.test.model" targetProject="E:\eclipse 3.5.2\workspace\gao\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.test.mapping" targetProject="E:\eclipse 3.5.2\workspace\gao\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao" targetProject="E:\eclipse 3.5.2\workspace\gao\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
<table tableName="pds_system_item" domainObjectName="PdsSystemItem" />
<table tableName="pds_system_level" domainObjectName="PdsSystemLevel" />
</context>
</generatorConfiguration>
将这个文件保存至你下载的mybatis-generator-core-1.3.1文件夹下 第三步:用命令行运行(记得选择自己的文件地址) java -jar E:\soft\mybatis-generator-core-1.3.1\lib\mybatis-generator-core-1.3.1.jar -configfile E:\soft\mybatis-generator-core-1.3.1\genrator.xml -overwrite
我自己写的如下:
<?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="D:\abin\Java\MybatisTool\mybatis-generator-core-1.3.1\lib\mysql-connector-java-5.1.20-bin.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 去除自动生成的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mycrm" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:自动生成代码的位置 -->
<javaModelGenerator targetPackage="com.test.model" targetProject="F:\nb\mp\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.test.mapping" targetProject="F:\nb\mp\src\main\java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao" targetProject="F:\nb\mp\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
<table tableName="testcrm" domainObjectName="testcrmone" />
<table tableName="testmy" domainObjectName="testmyone" />
</context>
</generatorConfiguration>