struts、hibernate与spring整合

框架的搭建

1搭建Spring3

1.1 导入springjar

1.2 spring.xml放入src/main/resources

<?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:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

	<!-- 引入项目配置文件 -->
	<!-- <context:property-placeholder location="classpath:config.properties" /> -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:config.properties</value>
			</list>
		</property>
	</bean>

	<!-- 自动扫描dao和service包(自动注入) -->
	<context:component-scan base-package="sy.dao,sy.service" />
</beans>
 

1.3 web.xml中配置spring监听器和配置文件位置  

<!-- spring配置文件位置 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
</context-param>
<!--spring监听器-->
<listener>
   <listener-class>
       org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>


1.4 使用annotation方式把类加入spring容器中:

如果是为service包中类添加annotation

@Service("userService")
public class UserServiceImpl implements UserServiceI {}
如果是为 dao 包中类添加 annotation     
@Repository("userDao")
public class UserDaoImpl implements UserDaoI {}

1.5 建立测试类测试spring

public class TestSpring {
    @Test
    public void test() {
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"classpath:spring.xml"});
        UserServiceI userService = (UserServiceI) ac.getBean("userService");
        userService.test();
    }
}
2  搭建 Struts2 并整合 spring3 

2.1 导入struts2jar

2.2 struts.xml放入src/main/resources

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

	<!-- 指定由spring负责action对象的创建 -->
	<constant name="struts.objectFactory" value="spring" />
	<!-- 所有匹配*.action的请求都由struts2处理 -->
	<constant name="struts.action.extension" value="action" />
	<!-- 是否启用开发模式 -->
	<constant name="struts.devMode" value="true" />
	<!-- struts配置文件改动后,是否重新加载 -->
	<constant name="struts.configuration.xml.reload" value="false" />
	<!-- 设置浏览器是否缓存静态内容 -->
	<constant name="struts.serve.static.browserCache" value="false" />
	<!-- 请求参数的编码方式 -->
	<constant name="struts.i18n.encoding" value="utf-8" />
	<!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->
	<constant name="struts.i18n.reload" value="false" />
	<!-- 文件上传最大值 -->
	<constant name="struts.multipart.maxSize" value="104857600" />
	<!-- 让struts2支持动态方法调用 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<!-- Action名称中是否可使用斜线 -->
	<constant name="struts.enable.SlashesInActionNames" value="false" />
	<!-- 允许标签中使用表达式语法 -->
	<constant name="struts.tag.altSyntax" value="true" />
	<!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
	<constant name="struts.dispatcher.parametersWorkaround" value="false" />

	<package name="basePackage" extends="struts-default">
		
	</package>

</struts>
 

2.3 web.xml中配置struts2filter

<!-- Struts2配置 -->
<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>

2.4 使用注解的方式配置action

@ParentPackage("basePackage")
@Action(“userAction”)
public class UserAction {
    public void test() {
        System.out.println("struts successful");
    }
}

3 搭建Hibernate4并整合spring3 
3.1导入Hibernate4jar包

3.2spring-hibernate.xml放入src/main/resources

<?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:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

	<!-- JNDI方式配置数据源 -->
	<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->

	<!-- 配置数据源 -->
	<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="url" value="${jdbc_url}" />
		<property name="username" value="${jdbc_username}" />
		<property name="password" value="${jdbc_password}" />

		<!-- 初始化连接大小 -->
		<property name="initialSize" value="0" />
		<!-- 连接池最大使用连接数量 -->
		<property name="maxActive" value="20" />
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="0" />
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="60000" />

		<!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->

		<property name="validationQuery" value="${validationQuery}" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="testWhileIdle" value="true" />

		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="25200000" />

		<!-- 打开removeAbandoned功能 -->
		<property name="removeAbandoned" value="true" />
		<!-- 1800秒,也就是30分钟 -->
		<property name="removeAbandonedTimeout" value="1800" />
		<!-- 关闭abanded连接时输出错误日志 -->
		<property name="logAbandoned" value="true" />

		<!-- 监控数据库 -->
		<!-- <property name="filters" value="mergeStat" /> -->
		<property name="filters" value="stat" />
	</bean>

	<!-- 配置hibernate session工厂 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
	   <property name="dataSource" ref="dataSource" />
	      <property name="hibernateProperties">
		 <props>
		   <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
		   <prop key="hibernate.dialect">${hibernate.dialect}</prop>
		   <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
		   <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
		   <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
		 </props>
	      </property>

	      <!-- 自动扫描注解方式配置的hibernate类文件 -->
	      <property name="packagesToScan">
		 <list>
		    <value>zhisuo.model</value>
		 </list>
	      </property>

	      <!-- 自动扫描hbm方式配置的hibernate文件和.hbm文件 -->
	      <!-- <property name="mappingDirectoryLocations"> <list> <value>classpath:sy/hbm</value> </list> </property> -->
	</bean>

	<!-- 配置事务管理器 -->
	<bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
	     <property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!-- 注解方式配置事物 -->
	<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->

	<!-- 拦截器方式配置事物 -->
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="saveOrUpdate*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="grant*" propagation="REQUIRED" />

			<tx:method name="init*" propagation="REQUIRED" />
			
			<tx:method name="get*" propagation="SUPPORTS" />
			<tx:method name="find*" propagation="SUPPORTS" />
			<tx:method name="load*" propagation="SUPPORTS" />
			<tx:method name="search*" propagation="SUPPORTS" />
			<tx:method name="datagrid*" propagation="SUPPORTS" />

			<tx:method name="*" propagation="SUPPORTS" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
	<!-- 第一个*代表所有的返回值类型;第二个*代表所有的类;第三个*代表类所有方法;..代表子或者孙子包;最后一个..代表所有的参数 -->
	    <aop:pointcut id="transactionPointcut" expression="(execution(* zhisuo.service..*Impl.*(..)))" />
	  <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
	</aop:config>

</beans> 

3.3     web.xml中添加Hibernate的配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:spring.xml,classpath:spring-hibernate.xml
    </param-value>
</context-param>
<!-- openSessionInView配置 -->
<filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
    </filter-class>
	<init-param>
	    <param-name>singleSession</param-name>
	    <param-value>true</param-value>
	</init-param>
</filter>
<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

3.4 UserDao中使用SessionFactory来操作数据库

@Repository("userDao")
public class UserDaoImpl implements UserDaoI {
	
    private SessionFactory sessionFactory;
	
    public SessionFactory getSessionFactory() {
	return sessionFactory;
    }

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
	this.sessionFactory = sessionFactory;
    }
    @Override
    public Serializable save(Tuser tuser) {
	return this.sessionFactory.getCurrentSession().save(tuser);
    }
}

 

你可能感兴趣的:(web前端,SSH整合)