ibatis代码生成器使用:
1.下载MyBatis的Generator工具
下载地址:http://code.google.com/p/mybatis/downloads/detail?name=mybatis-generator-core-1.3.1-bundle.zip&can=3&q=Product%3DGenerator
2.配置自动生成代码所需的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="123">
</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>
3.用命令行运行(记得选择自己的文件地址)
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
4.部分属性说明
:driverClass:数据库连接驱动类
connectionURL:数据库连接地址
userId:数据库用户
passwor:数据库密码
location:数据库驱动jar包地址(本地)
targetPachage:自动生成的包的地址,(可以是已建好的,也可以是未建的)
targetProject:自动生成的项目名
type:生成dao文件的类型,可选择IBATIS、SPRING、GENERIC-CI、GENERIC-SI。默认使用GENERIC-CI
connectionURL:数据库连接地址
userId:数据库用户
passwor:数据库密码
location:数据库驱动jar包地址(本地)
targetPachage:自动生成的包的地址,(可以是已建好的,也可以是未建的)
targetProject:自动生成的项目名
type:生成dao文件的类型,可选择IBATIS、SPRING、GENERIC-CI、GENERIC-SI。默认使用GENERIC-CI
PS:targetRuntime="MyBatis3"时javaClientGenerator不会生成实现类,ibatis3自动装配,不需要实现类