ibatis 代码生成工具 Abator 使用步骤

下载Abator插件,可以使用link方式将其安装到Eclipse中,也可以进行Find And Install来安装;
 
如果使用link方式进行安装,其Abator下载地址为: http://people.apache.org/builds/ibatis/alpha/AbatorForEclipse1.0.0.zip
(关于link方式如何安装不在本文范畴,要参见:http://blog.csdn.net/apicescn/archive/2005/03/19/323478.aspx).
 
采用Find And Install安装Eclipse插件
1. 要求eclipse3.3,jdk1.4以上
2. 在eclipse上安装abator插件
    eclipse菜单栏 --> help --> Software Updates --> Find And Install 
    在弹出的对话框中选择 “Search for new features to install” 然后点击 “next”
    在对话框中点击按钮 New Remort Site  在弹出的对话框中的URL输入框中填写 http://ibatis.apache.org/tools/abator ,Name输入框就随便写abator就行了。
    点击Finish,安装插件,然后重新启动eclipse
 
下面将介绍如何使用该插件完成自动生成ibatis代码的功能:
1. 新建一个java工程(如何建立java工程以及相关基础知识不在本范畴).
2. 在eclipse菜单栏中新建一个 abator 配置文件   File -->  Abator  for  iBatis Configration File
 
好的,我们完成了创建配置文件,下面打开abator生成的配置文件:
 

Abator配置文件(abatorConfig.xml)




<?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>    <!-- 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="???">
      <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:表名
  columnOverride:表的字段名.
修改后的abatorConfig.xml配置文件内容如下:

  


<?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/ibatis" userId="Allen"
            password="Allen">
            <classPathEntry
                location="lib/mysql-connector-java-3.1.5-gamma-bin.jar" />
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.etong.test.daomain"
            targetProject=".. estsrcmainjava">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="."
            targetProject=".. estsrcmain esources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <daoGenerator type="SPRING" targetPackage="com.etong.test.dao"
            targetProject=".. estsrcmainjava">
            <property name="enableSubPackages" value="true" />
        </daoGenerator>

        <table schema="ibatis" tableName="ibatis">        
            <property name="useActualColumnNames" value="true"/>
            <generatedKey column="ID" sqlStatement="ORACLE"
                identity="true" />
            <columnOverride column="id" property="id" />
            <columnOverride column="name" property="name" />
        </table>
    </abatorContext>
</abatorConfiguration>

    配置完这个xml文件以后,下面就可以开始自动自成iBatis的相关文件了,有三种方法可供选择,具体做法如下:
一、       在配置文件上点击右键,选择 “Generate iBatis Artifacts”,即可生成所有的java文件以及sqlmap配置文件等等,但这种方式在Eclipse中需要先运行abator这个程序,所以不太好用,不建议使用。
二、       使用Ant来构建,Ant配置文件如下所示:




<?xml version="1.0" encoding="GBK"?>
<!--
   作者:Allen
   create date: 2007-08-20
-->
<project name="test" default="compile" basedir=".">
    <!-- properies 源文件夹 -->
    <property name="src.dir" value="src/main/java" />
    <property name="main.resource.dir" value="src/main/resources" />
    <property name="junitsrc.dir" value="src/test/java" />
    <property name="junitsrc.resource.dir" value="src/test/resources" />
    <property name="generated.source.dir" value="${basedir}" />
    
    <!--temporary build directory names 编译及引用类库文件夹 -->
    <property name="classes.dir" value="target/classes" />
    <property name="lib.dir" value="lib" />
    <property name="dist.dir" value="dist" />
    <property name="doc.dir" value="doc" />

    <!-- 单元测试和Todo报告文件夹 -->
    <property name="junit.dir" value="junit" />
    <property name="report.main.dir" value="report/main" />
    <property name="report.test.dir" value="report/test" />

    <!-- 定义classpath -->
    <path id="master-classpath">
        <!--  替换如下方式        <fileset file="${lib.dir}/*.jar" />        -->
        <!-- 
        也可以写成如下方式,此方式同上面方式基本一致,这种方式可以通过include来限定
        导入的类库,如果不限定即可使用通配符**/*来替代!        
        -->
        <fileset dir="${lib.dir}">
            <include name="**/*.jar" />
        </fileset>
        <pathelement path="${classes.dir}" />
    </path>

    <!-- 初始化开始任务 -->

    <target name="init">
        <mkdir dir="${src.dir}" />
        <mkdir dir="${main.resource.dir}" />
        <mkdir dir="${junitsrc.dir}" />
        <mkdir dir="${junitsrc.resource.dir}" />
        <mkdir dir="${classes.dir}" />
        <mkdir dir="${lib.dir}" />
        <mkdir dir="${dist.dir}" />
    </target>

    <!-- 清除任务 -->
    <target name="clean">
        <delete dir="${classes.dir}" />
    </target>

    <!-- 清除所有任务 -->
    <target name="cleanAll" depends="clean">
        <delete dir="${src.dir}" />
        <delete dir="${main.resource.dir}" />
        <delete dir="${junitsrc.dir}" />
        <delete dir="${junitsrc.resource.dir}" />
        <delete dir="${dist.dir}" />
        <delete dir="${doc.dir}" />
        <delete dir="${junit.dir}" />
        <delete dir="${report.main.dir}" />
        <delete dir="${report.test.dir}" />
    </target>

    <!-- ================================= 
          target: prepart 记录构建时间              
         ================================= -->
    <target name="prepart" depends="init" description="create the build dirdctories prior to a compile">
        <tstamp>
            <format property="now" pattern="MMMM d yyyy hh:mm aa" />
            <format property="hour_earlier" pattern="MMMM d yyyy hh:mm aa" offset="-1" unit="hour" />
            <format property="minute_later" pattern="MMMM d yyyy hh:mm aa" offset="1" unit="minute" />
        </tstamp>
        <echo>now=${now}</echo>
    </target>

    <!-- 编译 -->
    <target name="compile" depends="prepart" description="compile the source files">
        <javac srcdir="${src.dir}:${junitsrc.dir}" destdir="${classes.dir}" target="1.4">
            <classpath refid="master-classpath" />
        </javac>
        <copy todir="${classes.dir}">
            <fileset dir="${main.resource.dir}" includes="**/*.properties" />
            <fileset dir="${main.resource.dir}" includes="**/*.xml" />
            <!-- copy the primary program and modules -->
            <fileset dir="${junitsrc.resource.dir}" includes="*.properties" />
            <fileset dir="${junitsrc.resource.dir}" includes="**/*.xml" />
        </copy>
    </target>

    <!-- 生成ToDo报告,这个target依赖的库有
         commons-collections-2.1.jar
         commons-logging.jar
     -->
    <target name="todo" depends="init">
        <mkdir dir="${report.main.dir}" />
        <mkdir dir="${report.test.dir}" />
        <docdoclet destdir="${report.main.dir}">
            <fileset dir="${src.dir}">
                <include name="**/*.java" />
            </fileset>
            <info />
        </docdoclet>

        <docdoclet destdir="${report.test.dir}">
            <fileset dir="${junitsrc.dir}">
                <include name="**/*.java" />
            </fileset>
            <info />
        </docdoclet>
    </target>


    <!-- 单元测试 -->
    <target name="junit" depends="compile" description="run junit test">
        <mkdir dir="${junit.dir}" />
        <junit printsummary="on" haltonfailure="false" failureproperty="tests.failed" showoutput="true">
            <classpath refid="master-classpath" />
            <formatter type="plain" />
            <batchtest todir="${junit.dir}">
                <fileset dir="${classes.dir}">
                    <include name="**/*Test.*" />
                </fileset>
            </batchtest>
        </junit>
        <fail if="tests.failed">
        ***********************************************************
        ****  One or more tests failed!  Check the output   ****
        ***********************************************************
        </fail>
    </target>


    <!-- 打包成jar -->
    <target name="jar" depends="compile" description="make .jar file">
        <delete file="${dist.dir}/${ant.project.name}.jar">
        </delete>
        <jar jarfile="${dist.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <exclude name="**/*Test.*" />
            <exclude name="**/Test*.*" />
        </jar>
    </target>

    <target name="rebuild" depends="clean,init,compile,jar">
    </target>

    <!-- 输出api文档 -->
    <target name="doc" depends="jar" description="create api doc">
        <mkdir dir="${doc.dir}" />
        <javadoc destdir="${doc.dir}" author="true" version="true" use="true" windowtitle="Test API">
            <packageset dir="${src.dir}" defaultexcludes="yes">
                <include name="*/**" />
            </packageset>
            <doctitle>
                <![CDATA[<h1>Hello, test</h1>]]></doctitle>
        <bottom>
            <![CDATA[<i>All Rights Reserved.</i>]]></bottom>
    <tag name="todo" scope="all" description="To do:" />
</javadoc>
</target>


<!-- 自动生成iBatis相关代码和sqlMap配置文件-->
<target name="genfiles" description="Generate the files">
<taskdef  name="abator" classname="org.apache.ibatis.abator.ant.AbatorAntTask" classpath="${lib.dir}/abator.jar" />
<abator overwrite="true" configfile="${main.resource.dir}/abatorConfig.xml" verbose="false">
    <propertyset>
        <propertyref name="${generated.source.dir}" />
    </propertyset>
</abator>
</target>


</project>


然后运行“genfiles”此任务即可看到生成的java代码以及sqlMap的配置文件(此方法推荐)。
三、       可以使用java类来自动生成,类代码内容如下:


package com.etong.test.test;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.abator.api.Abator;
import org.apache.ibatis.abator.config.AbatorConfiguration;
import org.apache.ibatis.abator.config.xml.AbatorConfigurationParser;
import org.apache.ibatis.abator.exception.InvalidConfigurationException;
import org.apache.ibatis.abator.exception.XMLParserException;
import org.apache.ibatis.abator.internal.DefaultShellCallback;

public class TestClient {
    public static void main(String[] args) {
        List warnings = new ArrayList(); // Abator will add Strings to this
                                            // list
        boolean overwrite = true;
        File configFile = new File("abatorConfig.xml");
        AbatorConfigurationParser cp = new AbatorConfigurationParser(warnings);
        AbatorConfiguration config = null;
        try {
            config = cp.parseAbatorConfiguration(configFile);
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (XMLParserException e1) {
            e1.printStackTrace();
        }
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        Abator abator = new Abator(config, callback, warnings);
        try {
            abator.generate(null);
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

最后运行此类即可看到生成的文件!
文章出处:飞诺网(www.firnow.com):http://dev.firnow.com/course/3_program/java/javashl/2007126/90733.html



------------------my---------------------------------------
<abatorConfiguration >
<abatorContext >
<jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@18.0.0.177:1521:xrhis" userId="xrhis" password="xrhis" >
<classPathEntry location="D:/ojdbc14.jar" />
</jdbcConnection>
<javaModelGenerator targetPackage="com.test.model" targetProject="testAbator" />
<sqlMapGenerator targetPackage="com.test.map" targetProject="testAbator" />
<daoGenerator targetPackage="com.test.dao" targetProject="testAbator" type="GENERIC-CI" />
<table tableName="dept">
    
</table>
</abatorContext>
</abatorConfiguration>

你可能感兴趣的:(java,apache,eclipse,mysql,ibatis)