这里使用的是Spring2.5+Hibernate3.3+Struts1.3(SSH)框架,进行整合开发,通过该案例可以了解使用SHH框架进行开发的基本操作和配置过程,虽然SSH版本比较老,但是还是有参考价值,以后在整理出更高版本的框架开发博客。
PS:这里给出所需要的整合JAR包,SSH整合包(S1.3+S2.5+H3.3)。
10、使用事务管理器统一管理事务(在applicationContext.xml中配置)。
pplicationContext.xml文件,把该文件放在src目录下。<!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:SWITCH" /> <property name="username" value="scott" /> <property name="password" value="123456" /> <!-- 连接池启动时初始值 --> <property name="initialSize" value="3" /> <!-- 连接池的最大值 --> <property name="maxActive" value="500" /> <!-- 最大空闲值,当经历一个高峰时间后,连接池可以慢慢将已经不用的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2" /> <!-- 最小空闲值,当空闲的连接数少于阈值时,连接池会预申请一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1" /> </bean>
<!-- 配置SessionFactory(Spring数据源) --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 设置数据源 --> <property name="dataSource" ref="dataSource" /> <!-- 接管了Hibernate对象映射文件 --> <property name="mappingResources"> <list> <value>com/pc/domain/Employee.hbm.xml</value> <value>com/pc/domain/Department.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.OracleDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=true </value> </property> </bean>
<!-- 配置事务管理器,统一管理sessionFactory的事务 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 启用事务注解 --> <tx:annotation-driven transaction-manager="transactionManager" />
PS:在需要进行事务管理的类添加@Transactional
hibernate.cache.use_second_level_cache=true hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.generate_statistics=true
<cache usage="read-write"/>
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <!-- 配置登录action表单 --> <form-bean name="employeeForm" type="com.pc.web.forms.EmployeeForm" /> </form-beans> <!-- 配置全局跳转 --> <global-forwards> <forward name="opererr" path="/WEB-INF/err.jsp"></forward> <forward name="operok" path="/WEB-INF/ok.jsp"></forward> </global-forwards> <action-mappings> <!-- 配置登录action --> <action path="/login" name="employeeForm" scope="request" parameter="type"> <forward name="ok" path="/WEB-INF/mainFrame.jsp" /> <forward name="error" path="/WEB-INF/login.jsp" /> </action> <!-- 配置雇员Action --> <action path="/employee" name="employeeForm" scope="request" parameter="type"> <forward name="goToAddEmployeeUI" path="/WEB-INF/addEmployeeUI.jsp" /> <forward name="goToShowEmployeeUI" path="/WEB-INF/showEmployeeUI.jsp" /> <forward name="goToUpdateEmployeeUI" path="/WEB-INF/updateEmployeeUI.jsp" /> </action> </action-mappings> <!-- 配置代理请求处理DelegatingRequestProcessor --> <controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" /> </controller> </struts-config>
<!-- 配置struts --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml/</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
<!-- 指定Spring配置文件,默认从web根目录下寻找配置文件,可以通过Spring提供的classpath:前缀指定从类路径下寻找 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 对Spring容器进行实例化 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<!-- 配置代理请求处理DelegatingRequestProcessor --> <controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" /> </controller>
<!-- 配置action --> <!-- 配置登录action --> <bean name="/login" class="com.pc.web.action.LoginAction" scope="prototype"> <property name="employeeServiceInter" ref="employeeService" /> </bean> <!-- 配置雇员action --> <bean name="/employee" class="com.pc.web.action.EmployeeAction" scope="prototype"> <property name="departmentServiceInter" ref="departmentService" /> <property name="employeeServiceInter" ref="employeeService" /> </bean>
package com.pc.web.filter; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author Switch * @function 过滤中文乱码 * @description * */ public class MyFilter extends HttpServlet implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 设置编码 request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
<!-- 配置自定义编码过滤器 --> <filter> <filter-name>MyFilter</filter-name> <filter-class>com.pc.web.filter.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<!-- 配置Spring提供的编码过滤器 --> <filter> <filter-name>encoding</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> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<!-- 配置EmployeeService对象,通过注解的方式来注入属性值,spring会启用byName的方式注入属性值 --> <bean id="employeeService" class="com.pc.service.imp.EmployeeService" />
// 当给某个属性增加了@Resource后,spring会启用byName的方式注入属性值 @Resource private SessionFactory sessionFactory;
<!-- 启用注解扫描 --> <context:annotation-config />
<!-- 配置Spring的OpenSessionInView过滤器解决懒加载 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
PS:该方法可以有效的减少对数据库的查询,缺点是和用户对session持有时间延长了。
SpringMVC实战-雇员薪资管理系统