现在我们着手来写一个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配置文件的模板。
代码如下:
<! 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.将对应配置参数替换掉上面的“?”号。我自己的替换文件是这样的:
<! 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所以说是无效的包名.