webwork+spring+ibatis

流程:web服务器启动,读取web.xml,web.xml配置servlet启动webwork,同时管理启动spring并解析加载applicationContext*.xml,applicationContext*.xml管理ibatis的数据源,SqlMap工厂以及相应的声明式事务。
1.web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<!--编码过滤器-->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>
			org.springframework.web.filter.CharacterEncodingFilter
		</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>

	<!--Spring服务部署配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/spring/applicationContext*.xml
		</param-value>
	</context-param>
	<!--启动Spring-->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

	<!--启动WebWork-->
	<servlet>
		<servlet-name>webwork</servlet-name>
		<servlet-class>
			com.opensymphony.webwork.dispatcher.ServletDispatcher
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>


	<servlet-mapping>
		<servlet-name>webwork</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
</web-app>


2.Spring加载ibatis数据源 ApplicationContext_dataSource.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>/WEB-INF/spring/mysqljdbc.properties</value>
			</list>
		</property>
		<property name="fileEncoding">
			<value>GBK</value>
		</property>
	</bean>
	<!--数据源设置-->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName">
			<value>${jdbc.driver}</value>
		</property>
		<property name="url">
			<value>${jdbc.url}</value>
		</property>
		<property name="username">
			<value>${jdbc.username}</value>
		</property>
		<property name="password">
			<value>${jdbc.password}</value>
		</property>
	</bean>
</beans>

数据源的properties:
# Properties file with JDBC-related settings.
# Applied by PropertyPlaceholderConfigurer from "dataAccessContext-local.xml".
# Targeted at system administrators, to avoid touching the context XML files.

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/jlict?useUnicode=true&amp;characterEncoding=GBK
jdbc.username=root
jdbc.password=root


3.ApplicationContext_ibatis.xml管理sqlMap工厂,方言
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<!--ibatis设置-->
	<bean id="sqlMapClient"
		class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation">
			<value>
				classpath:cn\edu\jlict\common\sql\sql-map-config.xml
			</value>
		</property>
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
	</bean>
	<bean id="sqlExecutor"
		class="cn.edu.jlict.common.ibatis.LimitSqlExecutor">
		<property name="dialect">
			<bean class="cn.edu.jlict.common.ibatis.MySQLDialect" />
		</property>
	</bean>
	<bean id="txBaseSqlMapDaoSpring"
		class="cn.edu.jlict.common.ibatis.BaseDaoiBatis"
		init-method="initialize">

		<property name="sqlMapClient">
			<ref bean="sqlMapClient" />
		</property>
		<property name="sqlExecutor">
			<ref bean="sqlExecutor" />
		</property>
	</bean>
</beans>

4.声明式事务

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<!--Ibatis事务处理-->
	<bean id="transactionManagerIbatis"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
	</bean>
	
    <!-- 利用spring的TransactionProxyFactoryBean去对事务进行自动管理 -->  
	<bean id="txProxyTemplateIbatis" abstract="true"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager">
			<ref local="transactionManagerIbatis" />
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="update*">
					PROPAGATION_REQUIRED,-Exception
				</prop>
				<prop key="insert*">
					PROPAGATION_REQUIRED,-Exception
				</prop>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="delete*">
					PROPAGATION_REQUIRED,-Exception
				</prop>
				<prop key="remove*">
					PROPAGATION_REQUIRED,-Exception
				</prop>
			</props>
		</property>
	</bean>
</beans>

5.webwork.properties没有这个文件spring不能管理action
webwork.locale=zh_CN
webwork.i18n.encoding=UTF-8
#webwork.multipart.parser=pell
#webwork.multipart.saveDir=E:\\hdkwOA\\exploded\\admin\\temp
#webwork.multipart.saveDir=/usr/local/jakarta-tomcat-4.0.6/webapps/hdkwOA/admin/temp
#webwork.multipart.maxSize=12345
webwork.configuration.xml.reload=true
webwork.objectFactory = spring

6.IOC dao层
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	<!-- 会员Dao定义 -->
	<bean id="jmemberDao" class="cn.edu.jlict.member.dao.impl.JmemberDaoImpl">
		<property name="sqlMapClient">
			<ref bean="sqlMapClient"/>
		</property>
	</bean>
</beans>

总结:调试了很多BUG之后才出来了这篇文章,要自己牢记:1.切要细心 2.编译器提示错误问题后要耐心调试。

你可能感兴趣的:(spring,xml,ibatis,jdbc,Webwork)