现在的位置: 首页 > 综合 > 正文

Abator代码生成工具 – -Eclipse下Ibatis|Mybatis代码生成工具 利器

2013年10月20日 ⁄ 综合 ⁄ 共 14312字 ⁄ 字号 评论关闭

使用Abator工具来自动生成iBatis代码:

1.安装

  1.1.下载安装 

       首先,下载Abator插件,其Abator下载地址为:

http://people.apache.org/builds/ibatis/alpha/AbatorForEclipse1.0.0.zip

       其次,将插件使用link方式将其安装到Eclipse中,也可以进行Find And Install来本地安装.

  2.1.Eclipse在线安装

       2.1.1. 要求eclipse3.3,jdk1.4以上

       2.1.2.eclipse菜单栏 --> help --> Software Updates --> Find And Install... ,

               在弹出的对话框中选择 “Search for new features to install” 然后点击 “next”,

               在对话框中点击按钮 New Remort Site... ,Name:Ibatis_Abator(任意),URL:http://ibatis.apache.org/tools/abator

               点击Finish,安装插件,然后重新启动eclipse.

               安装完成后,File-->new菜单下出现"Abator for iBATIS Configuration File"选项.

2.使用

 2.1.新建一个java工程.

   2.2.在eclipse菜单栏中新建一个abator 配置文件: File -->new--> Abator for iBATIS Configuration File

   2.3.打开abator生成的配置文件(abatorConfig.xml)并修改

 2.4.右键菜单选择Generate iBATIS Artifacts,生成文件


配置文件说明:

<?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="???">    <!-- TODO: Add Database Connection Information -->
    <jdbcConnection driverClass="???"
        connectionURL="???"
        userId="???"
        password="???">
      <classPathEntry location="???" />
    </jdbcConnection>

    <javaModelGenerator targetPackage="???" targetProject="???" />
    <sqlMapGenerator targetPackage="???" targetProject="???" />
    <daoGenerator type="IBATIS" targetPackage="???" targetProject="???" />

    <table schema="???" tableName="???">

      <generatedKey column="???" sqlStatement="???" identity="???">
      <columnOverride column="???" property="???" />

    </table>

</abatorContext>
</abatorConfiguration>

我们需要做的是替换一些???:
1. 填写driverClass(jdbc驱动,例如MySql的就是com.mysql.jdbc.Driver)
2. 填写connectionURL(连接字符串,例如MySql的就是jdbc:mysql://localhost:3306/ibatis)
3. 填写classPathEntry的location(jdbc驱动jar包的位置,例如:lib/mysql-connector-java-3.1.5-gamma-bin.jar)
4. 填写javaModelGenerator,生成的DTO(java model 类)
    targetPackage:目标包的位置,如 com.etong.test.daomain
    targetProject:目标源文件夹位置,如:../test/src/main/java/
5. 填写sqlMapGenerator ,生成的xml sqlmap的相关配置
    targetPackage:目标位置,如 .表示放在该目标源文件夹的根目录下.
    targetProject:目标源文件夹位置,如:../test/src/main/resources/
6. 填写daoGenerator ,生成的DAO的相关配置
    type:生成的dao实现的类型,如果你使用spring的话写SPRING,否则写IBATIS
    targetPackage:目标位置,如 com.etong.test.dao
    targetProject:目标源文件夹位置,如:../test/src/main/java/
7. 配置相关数据库的表
    schema:数据库schema (如果是oracle就是填写数据库的用户名,可以不填)
    tableName:表名

  generatedKey:来告诉abator那些列需要自动返回值(当插入的时候可以返回插入记录的主键,这对有外键的数据库极其有用)identity默认为false
    columnOverride:重写的列,列名重写.

【注意事项】:
     1.Abator工具生成的DAO实现中,引用SqlMap Statements时默认使用namespace,即namespace.statements;
     2.Statements ID以abatorgenerated_开头时,默认为是自动生成的elements,下次会重新生成(覆盖)该elements;
     3.在 jdbcConnection 中设置要mapping的数据库的jdbc连接classPathEntry 是你的jdbc driver类路径
     4.javaModelGenerator,sqlMapGenerator,daoGenerator 分别设置 java dataObject、sql mapping文件和DAO 接口、实现类的生成位置:targetPackage 目标包,targetProject:eclipse项目
     5.daoGenerator 中可以设置属性  type: ibatis 或 spring 指定生成的dao实现类是使用com.ibatis.dao.client.template.SqlMapDaoTemplate还是org.springframework.orm.ibatis.support.SqlMapClientDaoSupport
     6.table 中 tableName 指定要处理的表名
  可以有多个table
   7.table中可以包含子元素 generatedKey: 使Insert方法可以返回值--由指定的column mapping
   8.generatedKey中的sqlStatement属性可以是获取sequence的SQL,也可以是获取自增值的SQL
    比如:Oracle的 select theSequence.nextVal from dual
    SQLServer的 SELECT @@IDENTITY as column_name

  


例:

<?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 >
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/rms" 
        userId="root" password="11111111" >
      <classPathEntry location="D:/hurry/Workspaces/MyEclipse10/rms/WebRoot/WEB-INF/testLib/mysql-connector-java-5.1.18-bin.jar" />
    </jdbcConnection>
    <javaModelGenerator targetPackage="com.zltd" targetProject="rms" />
    <sqlMapGenerator targetPackage="com.zltd" targetProject="rms" />
    <daoGenerator targetPackage="com.zltd" targetProject="rms" type="SPRING" />
    <!-- <table schema="" tableName="T_AFN_0C_F1" >
        <columnOverride column="???" property="???" />
    </table> -->
    <table schema="" tableName="tb_log" ></table>
    <table schema="" tableName="tb_error" ></table>
    <table schema="" tableName="tb_user" ></table>
    <table schema="" tableName="tb_user_role" ></table>
    <table schema="" tableName="tb_user_orgnazation" ></table>
    <table schema="" tableName="tb_role_resouce_operation" ></table>
    <table schema="" tableName="tb_role" ></table>
    <table schema="" tableName="tb_orgnazation" ></table>
    <table schema="" tableName="tb_role_resouce" ></table>
    <table schema="" tableName="tb_resouce" ></table>
    <table schema="" tableName="tb_resouce_operation" ></table>
    <table schema="" tableName="tb_operation" ></table>
    <table schema="" tableName="tb_dictionary" ></table>
    <table schema="" tableName="tb_asset_change" ></table>
    <table schema="" tableName="tb_asset" ></table>
    <table schema="" tableName="tb_app" ></table>
    
  </abatorContext>
</abatorConfiguration>

 

 

使用Abator工具来自动生成iBatis代码:

1.安装

  1.1.下载安装 

       首先,下载Abator插件,其Abator下载地址为:

http://people.apache.org/builds/ibatis/alpha/AbatorForEclipse1.0.0.zip

       其次,将插件使用link方式将其安装到Eclipse中,也可以进行Find And Install来本地安装.

  2.1.Eclipse在线安装

       2.1.1. 要求eclipse3.3,jdk1.4以上

       2.1.2.eclipse菜单栏 --> help --> Software Updates --> Find And Install... ,

               在弹出的对话框中选择 “Search for new features to install” 然后点击 “next”,

               在对话框中点击按钮 New Remort Site... ,Name:Ibatis_Abator(任意),URL:http://ibatis.apache.org/tools/abator

               点击Finish,安装插件,然后重新启动eclipse.

               安装完成后,File-->new菜单下出现"Abator for iBATIS Configuration File"选项.

2.使用

 2.1.新建一个java工程.

   2.2.在eclipse菜单栏中新建一个abator 配置文件: File -->new--> Abator for iBATIS Configuration File

   2.3.打开abator生成的配置文件(abatorConfig.xml)并修改

 2.4.右键菜单选择Generate iBATIS Artifacts,生成文件


配置文件说明:

<?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="???">    <!-- TODO: Add Database Connection Information -->
    <jdbcConnection driverClass="???"
        connectionURL="???"
        userId="???"
        password="???">
      <classPathEntry location="???" />
    </jdbcConnection>

    <javaModelGenerator targetPackage="???" targetProject="???" />
    <sqlMapGenerator targetPackage="???" targetProject="???" />
    <daoGenerator type="IBATIS" targetPackage="???" targetProject="???" />

    <table schema="???" tableName="???">

      <generatedKey column="???" sqlStatement="???" identity="???">
      <columnOverride column="???" property="???" />

    </table>

</abatorContext>
</abatorConfiguration>

我们需要做的是替换一些???:
1. 填写driverClass(jdbc驱动,例如MySql的就是com.mysql.jdbc.Driver)
2. 填写connectionURL(连接字符串,例如MySql的就是jdbc:mysql://localhost:3306/ibatis)
3. 填写classPathEntry的location(jdbc驱动jar包的位置,例如:lib/mysql-connector-java-3.1.5-gamma-bin.jar)
4. 填写javaModelGenerator,生成的DTO(java model 类)
    targetPackage:目标包的位置,如 com.etong.test.daomain
    targetProject:目标源文件夹位置,如:../test/src/main/java/
5. 填写sqlMapGenerator ,生成的xml sqlmap的相关配置
    targetPackage:目标位置,如 .表示放在该目标源文件夹的根目录下.
    targetProject:目标源文件夹位置,如:../test/src/main/resources/
6. 填写daoGenerator ,生成的DAO的相关配置
    type:生成的dao实现的类型,如果你使用spring的话写SPRING,否则写IBATIS
    targetPackage:目标位置,如 com.etong.test.dao
    targetProject:目标源文件夹位置,如:../test/src/main/java/
7. 配置相关数据库的表
    schema:数据库schema (如果是oracle就是填写数据库的用户名,可以不填)
    tableName:表名

  generatedKey:来告诉abator那些列需要自动返回值(当插入的时候可以返回插入记录的主键,这对有外键的数据库极其有用)identity默认为false
    columnOverride:重写的列,列名重写.

【注意事项】:
     1.Abator工具生成的DAO实现中,引用SqlMap Statements时默认使用namespace,即namespace.statements;
     2.Statements ID以abatorgenerated_开头时,默认为是自动生成的elements,下次会重新生成(覆盖)该elements;
     3.在 jdbcConnection 中设置要mapping的数据库的jdbc连接classPathEntry 是你的jdbc driver类路径
     4.javaModelGenerator,sqlMapGenerator,daoGenerator 分别设置 java dataObject、sql mapping文件和DAO 接口、实现类的生成位置:targetPackage 目标包,targetProject:eclipse项目
     5.daoGenerator 中可以设置属性  type: ibatis 或 spring 指定生成的dao实现类是使用com.ibatis.dao.client.template.SqlMapDaoTemplate还是org.springframework.orm.ibatis.support.SqlMapClientDaoSupport
     6.table 中 tableName 指定要处理的表名
  可以有多个table
   7.table中可以包含子元素 generatedKey: 使Insert方法可以返回值--由指定的column mapping
   8.generatedKey中的sqlStatement属性可以是获取sequence的SQL,也可以是获取自增值的SQL
    比如:Oracle的 select theSequence.nextVal from dual
    SQLServer的 SELECT @@IDENTITY as column_name

  


例:

<?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 >
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/rms" 
        userId="root" password="11111111" >
      <classPathEntry location="D:/hurry/Workspaces/MyEclipse10/rms/WebRoot/WEB-INF/testLib/mysql-connector-java-5.1.18-bin.jar" />
    </jdbcConnection>
    <javaModelGenerator targetPackage="com.zltd" targetProject="rms" />
    <sqlMapGenerator targetPackage="com.zltd" targetProject="rms" />
    <daoGenerator targetPackage="com.zltd" targetProject="rms" type="SPRING" />
    <!-- <table schema="" tableName="T_AFN_0C_F1" >
        <columnOverride column="???" property="???" />
    </table> -->
    <table schema="" tableName="tb_log" ></table>
    <table schema="" tableName="tb_error" ></table>
    <table schema="" tableName="tb_user" ></table>
    <table schema="" tableName="tb_user_role" ></table>
    <table schema="" tableName="tb_user_orgnazation" ></table>
    <table schema="" tableName="tb_role_resouce_operation" ></table>
    <table schema="" tableName="tb_role" ></table>
    <table schema="" tableName="tb_orgnazation" ></table>
    <table schema="" tableName="tb_role_resouce" ></table>
    <table schema="" tableName="tb_resouce" ></table>
    <table schema="" tableName="tb_resouce_operation" ></table>
    <table schema="" tableName="tb_operation" ></table>
    <table schema="" tableName="tb_dictionary" ></table>
    <table schema="" tableName="tb_asset_change" ></table>
    <table schema="" tableName="tb_asset" ></table>
    <table schema="" tableName="tb_app" ></table>
    
  </abatorContext>
</abatorConfiguration>

 

 

抱歉!评论已关闭.