安装ibatis: html
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 java
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. web
If you've not already installed Apache iBATIS Ibator, then you can use the built in Eclipse install support by following these steps: sql
- Take the "Help>Software Updates..." Menu Option
- Select the "Available Software" Tab
- Press the "Add Site" button
- Enter the following information:Location:http://ibatis.apache.org/tools/ibator
- Press OK
- Check the box next to "Apache iBATIS Ibator Feature"
- Press the "Install" button
- 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因此说是无效的包名.