sping struts2 ibatis 整合

本文介绍如何整合Struts2、Spring和IBATIS框架,包括所需依赖包、配置文件详解及注意事项。

整合ssi虽然原理比较简单,但在实际操作的时候还是容易出错的,在这里也记录一下...
各个组件的版本号:struts2.1 spring2.5 ibatis2.3
struts2.1需要的包
首先是struts2.1必须的包:


然后是要与spring集成需要的包:struts2-spring-plugin-2.1.6.jar

spring2.5需要的包
这里用的是集成了spring所有模块的包:spring.jar

ibatis2.3需要的包
ibatis-2.3.*.*.jar

web.xml的配置

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->1<?xmlversion="1.0"encoding="UTF-8"?>
2<web-appversion="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"
3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
5http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
6<filter>
7<filter-name>struts2</filter-name>
8<filter-class>
9org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
10</filter-class>
11</filter>
12<listener>
13<listener-class>
14org.springframework.web.context.ContextLoaderListener
15</listener-class>
16</listener>
17<filter-mapping>
18<filter-name>struts2</filter-name>
19<url-pattern>/*</url-pattern>
20</filter-mapping>
21<welcome-file-list>
22<welcome-file>login.jsp</welcome-file>
23</welcome-file-list>
24</web-app>
25


applicationContext.xml的配置

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop
="http://www.springframework.org/schema/aop"
xmlns:tx
="http://www.springframework.org/schema/tx"
xsi:schemaLocation
="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>

<beanid="dataSource"
class
="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<propertyname="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<propertyname="username">
<value>root</value>
</property>
<propertyname="password">
<value></value>
</property>
</bean>

<beanid="sqlMapClient"
class
="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="configLocation"value="WEB-INF/sql-map-config.xml"/>
</bean>

<beanid="sqlMapClientTemplate"class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<propertyname="sqlMapClient"ref="sqlMapClient"/>
</bean>

<!--其他配置,如DAO,Action--/>

</beans>


sql-map-config.xml的配置

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsqlMapConfig
PUBLIC"-//ibatis.apache.org//DTDSQLMapConfig2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd"
>
<sqlMapConfig>
<settingsuseStatementNamespaces="true"/>
<sqlMapresource="ssi/persistance/sqlmap/user_SqlMap.xml"/>
</sqlMapConfig>


具体sql-map文件的配置,这里就不写了,我也是刚刚学习ibatis,呵呵,由于最近马上就要开发了,就找了工具来加快速度,ibatis官方提供的ibator(原来叫abator)这个工具相当猛啊,还在学习中...

在配置过程当中,特别需要注意各个配置文件的存放位置,比如对于applicationContext.xml来说,默认应该存放在WEB-INF文件夹中,如果想放到类路径上去,需要在web.xml里面配置,一般配置在web.xml的开头部分:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值