1. 介绍
Abator 是一款iBATIS代码生成工具。它从数据库获取信息,然后产生如下文件。
1) 数据库表的SqlMap.xml file 文件,如UserSQL.xml;
2) 数据库表的Java Bean Model,如User.java;
3) 包含一个综合主键的数据库表的主键model,如UserKey.java;
4) 包含DAO层 selectByExample方法的example文件, UserExample.java;
5) 提供插入、更新、查询、删除方法的DAO接口 UserDAO.java;
6) DAO接口的实现类 UserDAOImpl.java。
2. 安装Abator到MyEclipse(Eclipse)中
1) 点击MyEclipse(Eclipse)工具栏中的Help-->software update -->find and install --> search for new features to install --> new remote site。
2) 在name处填写abator ,在url处填写http://ibatis.apache.org/tools/abator。
3) 点击OK, 然后点击finish,系统开始安装Abator到MyEclipse(Eclipse)中。
4) Abator插件安装成功后会在file -->new 菜单中看到Abator for iBATIS Configuration File。
3. 使用Abator生成程序文件
1) 在MyEclipse(Eclipse)中,点击File --> new 选择Abator for iBATIS Configuration File,创建一个名叫做abatorConfig.xml的配置文件。
2) 修改配置文件。下面给出一个示例。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN" "http://ibatis.apache.org/dtd/abator-config_1_0.dtd" > <abatorConfiguration > <abatorContext generatorSet=”java5”> <!--JDBC驱动程序、连接字符串、用户名以及密码--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/evaluation" userId="root" password="138246s" > <!--连接数据库所用的jar包地址--> <classPathEntry location=".\mysql-connector-java\5.1.18\mysql-connector-java-5.1.18.jar" /> </jdbcConnection> <!--数据库表的Java Bean Model 存放的目录--> <javaModelGenerator targetPackage="com.sogou.mobile.tc.ev.report.db" targetProject="tcevaluation" /> <!--sqlmap的存放目录--> <sqlMapGenerator targetPackage="com.sogou.mobile.tc.ev.report.db" targetProject="tcevaluation" /> <!--DAO的存放目录, IBATIS DAO在最新版中已经不再更新,可以用spring框架DAO管理代替,或者使用GENERIC-CI不使用框架,直接使用SQL client map--> <daoGenerator targetPackage="com.sogou.mobile.tc.ev.report.db" targetProject="tcevaluation" type="GENERIC-CI" /> <!--设置需要生成的数据库表,schema:数据库名称 tableName表名--> <table tableName="Page" domainObjectName="Page"> <generatedKey column="id" identity="true" sqlStatement="select p.nextval from dual"/> <columnOverride column="id" property="id" javaType="int" jdbcType="int"/> <columnOverride column="typeid" property="typeid" javaType="int" jdbcType="int" /> </table> </abatorContext> </abatorConfiguration>
abatorContext generatorSet="Java5" 这里选择得生成器是JAVA2的虽然不是推荐的,但个人觉得还停好用的,其他可以选择Legacy,Java2,Java5,这两个生成器生成的代码在后面WHERE查询条件的时候会有细微不同
<generatedKey>来告诉abator那些列需要自动返回值(当插入的时候可以返回插入记录的主键,这对有外键的数据库极其有用)identity默认为false,则在sqlMap配置文件中<selectKey>的位置在sql命令前面,所以identity应该改为true。sqlStatement属性则填入各类数据库的返回值函数:
Cloudscape This will translate to VALUES IDENTITY_VAL_LOCAL()
DB2 This will translate to VALUES IDENTITY_VAL_LOCAL()
Derby This will translate to VALUES IDENTITY_VAL_LOCAL()
HSQLDB This will translate to CALL IDENTITY()
MySql This will translate to SELECT LAST_INSERT_ID()
SqlServer This will translate to SELECT SCOPE_IDENTITY()
<columnOverride>告诉abator那些列是需要特殊处理的,对于date这种比较复杂的类型,可以用它来指定映射(用jdbctype和javatype属性来指定)。
3) 配置完毕,右键点击abatorConfig.xml文件,点击generate iBatis artfacts就会在相应的配置目。
结构
<abatorConfiguration> <!-- 普通的java資源文件 properties resource引用 resource="com/myproject/abatorConfig.properties" 必須在com.myproject包中 url引用 例如:url="file://C:/myfolder/abatorConfig.properties" 以${property}的方式引用,属性资源文件中的属性值 --> <properties resource=""? url=""? />* <!-- defaultModelType 生成代码的类型 defaultModelType="conditional(默认值)|flat|hierarchical" conditional:类似hierarchical,但是只有一个主键的时候,不会拆分类 flat:一个table生成一个类 hierarchical:如果有主键,生成一个类,BLOB生成一个类,其它的在生成一个类 --> <!-- generatorSet generatorSet="Legacy(默认值)|Java2|Java5" Legacy 兼容所有的iBatis版本和 Java2,会丢失一些特性,不建议使用 Java2 支持iBatis2.2.0及以上,所有版本的Java2 Java5 支持iBatis2.2.0及以上,所有版本的JSE5 --> <abatorContext defaultModelType=””? generatorSet=””?>+ <!-- 支持的属性 suppressTypeWarnings=false, Java5不起早作用 beginningDelimiter="\"" 分隔符 endingDelimiter ="\"" 分隔符 --> <propter name=”” value=”” />* <commentGenerator type=""?>? <propter name=”” value=”” />* </commentGenerator> <jdbcConnection driverClass=”” connectionURL=”” userId=””? password=””?> <!-- location 指的是jdbc连接器的位置,如果在classpath中就不用添加这项了 --> <classPathEntry location=””/>* <propter name=”” value=”” />* </jdbcConnection> <javaTypeResolver type="" ?>? <propter name=”” value=”” />* </javaTypeResolver> <javaModelGenerator type="" ? targetPackage="" targetProject=""> <propter name=”” value=”” />* </javaModelGenerator> <sqlMapGenerator type="" ? targetPackage="" targetProject=""> <propter name=”” value=”” />* </sqlMapGenerator> <daoGenerator type="" ? targetPackage="" targetProject="">? <propter name=”” value=”” />* </daoGenerator> <table catalog="" ? schema="" ? tableName="" alias="" ? domainObjectName="" ? enableInsert="" ? enableSelectByPrimaryKey="" ? enableSelectByExample="" ? enableUpdateByPrimaryKey="" ? enableDeleteByPrimaryKey="" ? enableDeleteByExample="" ? enableCountByExample="" ? enableUpdateByExample="" ? selectByPrimaryKeyQueryId="" ? selectByExampleQueryId="" ? modelType="" ? escapeWildcards="" ? delimitIdentifiers="" ?>+ <propter name=”” value=”” />* <generatedKey column="" sqlStatement="" identity="" ? type="" ? />? <columnRenamingRule searchString="" replaceString="" ? />? <!-- columnOverride ignoreColumn不能同时出现 --> <columnOverride column="" property="" ? javaType="" ? jdbcType="" ? typeHandler="" ? delimitedColumnName="" ? />* <ignoreColumn column="" delimitedColumnName="" ? /> * </table> </abatorContext> </abatorConfiguration>
请参考eclipse/plugins/org.apache.ibatis.abator.doc_1.1.0/ doc.zip/core/htmldoc/index.html 文档