ibator插件教程

安装ibatis:

Apache iBATIS Ibator for Eclipse Update Site

This is the Apache iBATIS Ibator for Eclipse update site. If you're not sure what Ibator is, then see this page http://ibatis.apache.org/ibator.html

If you've already installed a prior version of Apache iBATIS Ibator for Eclipse, then simply run the Eclipse Install/Update tool and any new version will be found automatically.

If you've not already installed Apache iBATIS Ibator, then you can use the built in Eclipse install support by following these steps:

  1. Take the "Help>Software Updates..." Menu Option
  2. Select the "Available Software" Tab
  3. Press the "Add Site" button
  4. Enter the following information:Location:http://ibatis.apache.org/tools/ibator
  5. Press OK
  6. Check the box next to "Apache iBATIS Ibator Feature"
  7. Press the "Install" button
  8. Follow the remainder of the install wizard

 

 现在我们着手来写一个ibatis的简单例子.
  如果你是使用eclipse开发项目的话,那么,有一个eclipse的插件ibator,可以通过配置自动生成java代码sqlmap等,蛮好用。下面先做一个使用ibator插件的例子。
  1.使用link方式在线安装ibator。
    eclipse菜单  help-->
                 software updates-->
                 add site-->(填入在线安装地址:http://ibatis.apache.org/tools/ibator
                 -->一直下一步安装
  2.新建一个web工程,导入相应的包,我使用的数据库是ORACLE,所以我需要导入的包是:
    ojdbc14.jar或classes12.jar,把ibatis需要的jar包都导进去
    此例我导入的jar包如下:

    

  3.由于你安装了ibator插件,那么你在你的工程某个目录下单击右键-->new-->other里将会看到
    Apache iBatis Ibator文件夹,下面只有一个选项,如下图。
    

    点击以后要求你输入文件名(这个就是ibator的配置文件):
    

    填入配置文件名称,可以随便设置配置文件名字。一般为“ibatorConfig.xml”,点确定后就创建好了一个ibator配置文件的模板。
    代码如下:
    
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE ibatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN" "http:                 //ibatis.apache.org/dtd/ibator-config_1_0.dtd" >
<ibatorConfiguration >
  <ibatorContext id="context1" >
    <jdbcConnection driverClass="???" connectionURL="???" userId="???" password="???" />
    <javaModelGenerator targetPackage="???" targetProject="???" />
    <sqlMapGenerator targetPackage="???" targetProject="???" />
    <daoGenerator targetPackage="???" targetProject="???" type="GENERIC-CI" />
    <table schema="???" tableName="???" >
      <columnOverride column="???" property="???" />
    </table>
  </ibatorContext>
</ibatorConfiguration>
    
    4.将对应配置参数替换掉上面的“?”号。我自己的替换文件是这样的:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE ibatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN" "http://ibatis.apache.org/dtd/ibator-config_1_0.dtd" >
<ibatorConfiguration >
  <classPathEntry location="D:\Program Files\work_soft\apache-maven-2.0.9\repository\com\oracle\ojdbc14\10.2.0.1.0\ojdbc14-10.2.0.1.0.jar"/>
  <ibatorContext id="content1">
    <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin: @localhost :1522:zju" userId="ly" password="ly">
        <property name="" value=""/>
    </jdbcConnection>
    <javaModelGenerator targetPackage="com.model" targetProject="ibatistest">
    </javaModelGenerator>
    <sqlMapGenerator targetPackage="com.xml" targetProject="ibatistest">
    </sqlMapGenerator>
    <daoGenerator targetPackage="com.dao" targetProject="ibatistest" type="IBATIS">
    </daoGenerator>
    <table tableName="TB_USER" domainObjectName="user">
    </table>
  </ibatorContext>
</ibatorConfiguration>
     其中<table tableName="TB_USER"...> 这个"TB_USER"是在数据库中事先创建好的表,就是我们要通过ibatis操作的表。
     <table..> 这里如果什么也不写的话,默认会使用数据库中的字段名产生pojo类</table>

     关于ibatorConfig.xml中配置参数具体内容可以参考一下apache文档
     文档地址: http://ibatis.apache.org/docs/tools/abator/

     5.之后我们只需要在这个配置文件上点击右键-->点击Generate ibatis artifacts,这样就应该能生成对应的package和类了.我的生成如下图:

     
     
     6.在我使用ibator的时候发生了以下几处错误:
       1)Exception :getting jdbc Driver   由于我开始的时候将
<classPathEntry location="D:"Program Files"work_soft"apache-maven-2.0.9"repository"com"oracle"ojdbc14"10.2.0.1.0"ojdbc14-10.2.0.1.0.jar"/>
         这句放到了jdbcConnection里,而新版本是放到外面的,所以报此错误。
       2)Cannot find source folder ibatistest/src
         因为端口号没有配置正确1522配置成1521了,所以报这个问题,网上还有人说是端口号配置正确了防火墙拦截也有可能导致这个问题,那么只需要去把防火墙里的“例外”里添加你数据库使用的端口号就可以了,如果安装了防火墙软件也是一样道理,添加一个例外的端口。
         做完以上的修改以后先用sqlplus试一下,如果能够登录那么就对了,如果不能够登录,那么你需要开启数据库的监听程序,这个比较多内容就不再这里说了,可以去网上查一下如何开启数据库的监听程序。
       3)Cannot find source folder ibatistest/src,由于我开始的时候将<javaModelGenerator targetPackage="com.model" targetProject="ibatistest/src">里的targetProject的值设置为ibatistest/src,但是我没有创建这个文件夹,所以就报这个错误了,如果你没有创建任何源文件夹那么就是用你的工程名字就好了。
       4)Invalid name specified: com/dao  由于我把com.dao写成com/dao所以说是无效的包名.

转载于:https://my.oschina.net/liting/blog/511784

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值