服务器:tomcat 6.0
Jdk: jdk1.5
开发包:Ibatis开发包(从网上下载)
iBATIS和hibernate一样也是ORMapping框架,可以实现数据库的CRUD功能,不同的是ibatis使用的还是sql语句直接与数据库进行交互,因此性能比hibernate高。iBATIS结合spring和struts进行开发时的先后顺序和hibernate一样,先加spring和iBATIS最后加struts
1、首先建立一个web项目,为项目加spring和ibatis如图
选择next
选择finish
2、手工将数据库的jar包拷贝到项目web-inf的lib下(我用的是oracle10g数据库的jar包是classes12.jar)
3、将ibatis开发包中的SqlMapConfig.xml拷贝到项目的src下(此文件为ibatis的核心配置文件)
4、在spring的配置文件applicationContext-ibatis.xml里加入datasource和sqlMapcClient以及sqlMapcClientTemplate
5、编写vo以及映射文件(vo正常编写,在vo类的路径下要有vo类的一个xml映射文件,此文件从ibatis的开发包中将Account.xml拷贝到vo类所在的路径下,然后把Account改成对应的vo类的名字)
例如:如下图我编写了一个News的vo类,那么就将拷贝过来的Account.xml配置文件改成News.xml
5.1、vo类映射文件的配置(我的vo类是News
下面就是我对News.xml映射文件的配置)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="News"> <typeAlias alias="News" type="cn.gcb.vo.News" /> <resultMap id="NewsResult" class="News"> <result property="id" column="id" /> <result property="title" column="title" /> <result property="content" column="content" /> <result property="postDate" column="post_date" /> <result property="type" column="type" /> </resultMap> <select id="findAllNews" resultMap="NewsResult"> select * from news </select> <select id="findNewsById" parameterClass="int" resultClass="News"> select id,title,content,post_date AS postDate ,type from news where id = #id# </select> <insert id="doCreateNews" parameterClass="News"> insert into news ( id, title, content, post_date,type ) values ( #id#, #title#, #content#, #postDate#, #type# ) </insert> </sqlMap> |
5.2、加入映射后需要在核心配置文件SqlMapConfig.xml中加入vo映射文件的名字和路径
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <sqlMap resource="cn/gcb/vo/News.xml" /> </sqlMapConfig> |
6、编写dao接口和实现类(其中编写实现类的时候要继承SqlMapClientDaoSupport在实现接口)
7、编写service接口和实现类
8、给项目加struts支持
点击Next
点击finish
8.1、在src下建立struts.properties,并加入struts结合spring开发的默认配置。
点击finish
8.2、在struts.properties文件配置里加入struts与spring开发的默认配置
点击finish并进行保存
8.3、为了让struts2可以读取到spring配置文件所在的位置,在web.xml里定义 spring的监听器。
8.4、在src下加入log4j.properties文件
启动服务器