整合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的配置
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
6 <filter>
7 <filter-name>struts2</filter-name>
8 <filter-class>
9 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
10 </filter-class>
11 </filter>
12 <listener>
13 <listener-class>
14 org.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的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="WEB-INF/sql-map-config.xml" />
</bean>
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<!-- 其他配置,如DAO,Action--/>
</beans>
sql-map-config.xml的配置
<?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>
<settings useStatementNamespaces="true" />
<sqlMap resource="ssi/persistance/sqlmap/user_SqlMap.xml" />
</sqlMapConfig>
具體sql-map文件的配置,這里就不寫了,我也是剛剛學習ibatis,呵呵,由于最近馬上就要開發了,就找了工具來加快速度,ibatis官方提供的ibator(原來叫abator)這個工具相當猛啊,還在學習中...
在配置過程當中,特別需要注意各個配置文件的存放位置,比如對于applicationContext.xml來說,默認應該存放在WEB-INF文件夾中,如果想放到類路徑上去,需要在web.xml里面配置,一般配置在web.xml的開頭部分:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>