Struts2.x、Spring3.x、Hibernate3.x整合开发

SSH整合开发的步骤:

 

一、首先我们先导入jar包:

Struts2.x的jar包、Spring3.x的jar包、Hibernate3.x的jar包、Struts-spring plugin的jar包

二、接下来我们配置web.xml:

 

 

1>添加openSessionInView过滤器,这个配置的作用是:延迟session周期到响应结束,避免延迟加载引起错误,这个过滤器应该放到Struts过滤器的前面,组成过滤器链

 

 

<!-- openSessionInView -->
	<filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>
			org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 

 

2>添加Struts2.x Filter配置,这个是使用Struts开发必须要配置的,大家应该知道

 

<!-- Struts2.x Filter -->
	<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>
 

 

 

 

 3>添加Spring上下文监听器,这个监听器的作用是在web容器启动的时候自动创建applicationContext对象

 

<!-- Spring上下文监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>  
	    	classpath:applicationContext*.xml  
		</param-value>
	</context-param> 
   

 

 4>添加Spring 字符集过滤器,避免中文乱码,你懂得,不想配置的话,也可以自己写一个过滤器

 

<!-- Spring 字符集过滤器 -->
	<filter>
		<filter-name>springEncoding</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>springEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
 

 

三、在源文件夹中添加配置文件:

1>添加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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
 >
 	<!-- 启动自动扫描,便于使用注解把类交给Spring管理-->
	<context:component-scan base-package="com.kaishengit"/> 
 
 	<!-- 开启事务的Annotation支持 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean> 
	
	
	<!-- 配置数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql:///ssh" />
		<property name="maxIdleTime" value="25000" />
		<property name="properties">
			<props>
				<prop key="user">root</prop>
				<prop key="password">root</prop>
				<prop key="c3p0.acquire_increment">2</prop>
				<prop key="c3p0.max_size">20</prop>
				<prop key="c3p0.min_size">1</prop>
			</props>
		</property>  
  </bean>
<!--配置SessionFactory-->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		
		<property name="packagesToScan" value="com.kaishengit.pojo"/>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect 
				</prop>
				<prop key="hibernate.cache.provider_class">
					org.hibernate.cache.EhCacheProvider 
				</prop>
				<prop key="hibernate.cache.provider_configuration_file_resource_path">
					ehcache.xml 
				</prop>
			</props>
		</property>
	</bean>

 
 </beans>

 

 2>添加Struts配置文件struts.xml

 

<?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>
	<package name="myPackage" extends="struts-default">
		<action name="user" class="userAction">
			<result>index.jsp</result>
		</action>
	</package>
</struts>

 3>添加ehcache.xml配置文件,是第三方二级缓存,这个在上面applicationContext.xml中使用到,里面都是默认值

 

<ehcache>
    <diskStore path="java.io.tmpdir"/>
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />
</ehcache>
 

 

 

四、在类中使用注解

1>pojo类使用注解,如User.java

 

package com.kaishengit.pojo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name="t_user")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class User {
	private int id;
	private String username;
	private String password;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}

 

2>Spring类使用注解,类交给Spring管理,一种是在applicationContext.xml中配置,但是使用注解非常简便,把需要交给Spring类管理的类上面加上@Service、@Controller、@Component、@Repository任意一种都行,在需要依赖注入的属性的set方法上面加上@Autowired即可,如:

 

package com.kaishengit.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaishengit.dao.UserDao;
import com.kaishengit.pojo.User;

@Transactional
@Service
public class UserService {
	private UserDao userDao;

	public void saveOrUpdate(User user){
		userDao.saveOrUpdate(user);
	}
	
	public void del(int id){
		userDao.del(id);
	}
	
	@Transactional(readOnly=true)
	public User findById(int id){
		return (User) userDao.findById(id);
	}
	
	@Transactional(readOnly=true)
	public List<User> findAll(){
		return userDao.findAll();
	}
	
	
	//set
	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	
}
 

3>使用事务,在需要事务的类(一般都是Service类)上面加上@Transactional即可,在查询的方法上面加上@Transactional(readOnly=true),代表只读事务,更利于性能,例子就是上面的java类

4>Action类,暂且使用struts.xml配置

 

 

--end--

 

 

你可能感兴趣的:(java,spring,Hibernate,struts,ssh)