Spring3.0.5+Mybatis3.0.5+Struts2.0.6整合

废话不多说,直接上配置文件
Mybatis配置文件,这个配置文件主要是为了加载Mybatis分页拦截器,可以使用基于Dilect的方言进行分页
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
 <plugins>
   <plugin interceptor="com.ncu.app.PageInterceptor">
   	<property name="dbtype" value="mysql"/>
   </plugin>
 </plugins>   
    
</configuration>

Spring的配置文件,事务由Spring管理,Mapper的装配也由Spring管理,实体的别名也由sessionFactory自动扫描,由于Spring没有提供对Mybatis的支持,这里使用Mybatis团队开发的Spring整合包
<?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" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
	 <!--加载 JDBC 配置文件 -->
	<context:property-placeholder location="classpath:config/jdbc.properties" />
	 
	 <!-- 数据源以及proxool连接池配置 -->
	<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
		<property name="alias" value="proxoolpool"></property>
		<property name="delegateProperties">
			<value>user=${jdbc.username},password=${jdbc.password}</value>
		</property>
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="driver" value="${jdbc.driverClassName}" />
		<property name="driverUrl" value="${jdbc.url}" />
		<property name="houseKeepingSleepTime">
			<value>90000</value>
		</property>
		<property name="trace">
			<value>true</value>
		</property>
		<property name="maximumConnectionCount">
			<value>100</value>
		</property>
		<property name="prototypeCount">
			<value>5</value>
		</property>
		<property name="houseKeepingTestSql" value="select CURRENT_DATE" />
	</bean>

	<!-- 定义事务管理,数据源为Session工厂的数据源 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 定义Spring扫描包的路径 -->
	<context:component-scan base-package="com.ncu.app" />

	<!-- 启用自动装配 -->
	<context:annotation-config />

	<!-- 启用事务注解 -->
	<tx:annotation-driven />
	
	 <!-- 定义Mybatis的Session工厂,typeAliasesPackage为entity包全名,多个用逗号或者分号隔开 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />  
        <property name="typeAliasesPackage" value="com.ncu.app.entity.example" /> 
        <property name="configLocation" value="classpath:config/mybatis.xml" />   
    </bean>
 
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory" />
	</bean>

	 <!-- 定义所要扫描的Mapper配置文件包路径,多个包用逗号或者分号隔开 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ncu.app.dao.example" />
    </bean>

</beans>

Struts配置文件,这个没什么特别的,只是定义了需要包含的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<constant name="struts.objectFactory" value="spring" />	

	<!-- 需要包含的struts配置文件添加到这 -->
	<include file="struts_config/example.xml"/>
			
</struts>

这里可以看到Action已经是由Spring管理的
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="example" namespace="/test" extends="struts-default">       
        <action name="*_*" method="{2}" class="{1}Action">          
            <result name="list">/WEB-INF/jsp/{1}/{1}_list.jsp</result>      
        </action>
        <!-- Add actions here -->
    </package>
</struts>

最后是整合的Web配置文件,比较常用的,不再啰嗦
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SSIFramework</display-name>
  
    
  <!-- 系统过滤器配置-->
  <filter>
    <filter-name>SystemFilter</filter-name>
    <filter-class>com.ncu.app.filter.SystemFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>SystemFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 配置登陆过滤器-->
  <filter>
    <filter-name>LoginFilter</filter-name>
    <filter-class>com.ncu.app.filter.LoginFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>LoginFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 查询页码过滤器-->
  <filter>
    <filter-name>PageNumberFilter</filter-name>
    <filter-class>com.ncu.app.filter.PageNumberFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>PageNumberFilter</filter-name>
    <url-pattern>*.do</url-pattern>
    <url-pattern>*.action</url-pattern>
    <url-pattern>*.htm</url-pattern>
  </filter-mapping>
  
    <!-- Spring log4j配置 --> 
  <context-param>
  	<param-name>webAppRootKey</param-name>
  	<param-value>ssi.root</param-value>
  </context-param>
  <context-param>
      <param-name>log4jRefreshInterval</param-name>
      <param-value>3000</param-value>
 </context-param>
  <context-param>
       <param-name>log4jConfigLocation</param-name>
       <param-value>WEB-INF/classes/config/log4j.properties</param-value>
  </context-param>
  <listener>
       <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  
  
  <!-- Struts配置,必须在所有过滤器后面,否则前面过滤器无法生效 -->   
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class> 
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 
        </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- Spring配置 --> 
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value> 
        classpath*:config/applicationContext*.xml 
    </param-value>
  </context-param>
  <listener>
    <display-name>SpringListenner</display-name>
    <listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
  </listener>
  

  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

由于只是测试,所以在分页方面都没有去处理,可以自行引入dialect实现方言的分页
已附上源码

猜你喜欢

转载自31841814.iteye.com/blog/1124111