整合SSH框架 (Struts2.x+Spring3.x+hibernate3.x+oracle)
1.整合ssh框架思路
1.1 首先需要导入ssh的jar包,包括Struts2,Spring,Hibernate所需jar包和关联包,最后就是oracle的jar包
1.2 然后配置web.xml文件,配置spring的监听器信息,struts2过滤器信息和hibernate的延迟加载问题
1.3 配置Spring的配置文件,首先就是数据源的配置,然后sessionFactory配置,hibernate配置,如果spring使用的是注解,则需要设置spring bean组件扫面,最后配置 实体向SessionFactory的注册映射关系包的路径
1.4 创建pojo,使用spring测试框架的jar里的注解,进行单元测试
2.具体步骤
2.1需要导入的jar包
struts2 :
struts2-core-2.3.16.jar
xwork-core-2.3.16.jar
ognl-3.0.6.jar
Struts2与Spring整合插件包
struts2-spring-plugin-2.3.16.jar
Struts2注解包
struts2-convention-plugin-2.3.16.jar
spring:
spring-aop-3.2.4.RELEASE.jar
spring-aspects-3.2.4.RELEASE.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
spring-jdbc-3.2.4.RELEASE.jar
spring-orm-3.2.4.RELEASE.jar
开始启用Spring的测试框架
spring-test-3.2.4.RELEASE.jar
spring-tx-3.2.4.RELEASE.jar
spring-web-3.2.4.RELEASE.jar
其它对于aspect支持包 (AOP)
hibernate:
hibernate3.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
其它jar包
ojdbc14.jar (oracle的jar)
数据连接池(配置数据源)jar包 :
commons-collections.jar
commons-dbcp.jar
commons-pool-1.6.jar
2.2配置web.xml文件
<?xml version="1.0" encoding= "UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
<!-- 处理Hibernate延迟加载问题 -->
<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 >
<!-- 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 >
<!-- spring的配置监听器信息 -->
<context-param >
<param-name> contextConfigLocation</param-name >
<param-value> classpath:beans.xml</param-value >
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<welcome-file-list >
<welcome-file >index.jsp</ welcome-file>
</welcome-file-list >
</web-app>
2.3配置spring配置文件(bean.xml)本次使用的是dbcp配置数据源,数据库为oracle,数据库地址为本地地址
<?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: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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 开启spring bean组件扫面 -->
<context:component-scan base-package= "test"/>
<!-- 配置数据源 -->
<bean id="dateSource" class="org.apache.commons.dbcp.BasicDataSource" >
<!-- 数据库连接配置信息 -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="test"/>
<property name="password" value="123456"/>
<!-- 数据源相关配置信息 -->
<property name="initialSize" value="10"/>
<property name="maxActive" value="8"/>
<property name="maxIdle" value="6"/>
</bean >
<!--
使用 hbm映射文件进行配置
org.springframework.orm.hibernate3.LocalSessionFactoryBean
使用注解进行映射配置
org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<!-- 把配置好的数据源注入到SessionFactory -->
<property name="dataSource" ref="dateSource"/>
<!-- 配置hibernate相关配置信息 -->
<property name="hibernateProperties" >
<props>
<prop key="hibernate.show_sql" >true</ prop>
<prop key="hibernate.formate_sql" >true</ prop>
<prop key="hibernate.hbm2ddl.auto" >update</ prop>
<prop key="hibernate.dialect" >org.hibernate.dialect.Oracle10gDialect</prop >
<!-- 指定使用哪种方式来管理当前线程创建session -->
<prop key="hibernate.current_session_context_class" >thread</ prop>
</props>
</property>
<!-- 向SessionFactory注册映射关系 -->
<property name="packagesToScan" >
<list>
<value> test</value >
</list>
</property>
</bean >
</beans>
2.4创建测试实体类和测试类,测试sessionFactory是否创建成功
//测试实体类
package test;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = "TEST")
public class TestPo {
private int id;
private String name;
@Id
@Column(name = "ID", unique = true, nullable = false, precision = 10, scale = 0)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ")
@SequenceGenerator(name="SEQ",allocationSize=1,initialValue=1, sequenceName="SEQ")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package test1;
import static org.junit.Assert .*;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import test.TestPo;
//测试类
@RunWith(SpringJUnit4ClassRunner.class) //使用spring测试框架,不需要启动web项目,加上注解就可以直接进行测试
@ContextConfiguration ("/beans.xml" )
public class testC{
@Resource(name="sessionFactory" )
private SessionFactory sessionFactory ;
@Test
public void createSessionFactory() {
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
TestPo user = new TestPo();
user.setId(1);
user.setName( "name1");
session.save(user);
tx.commit();
}
}
创建sessionFactory就可以了,接下来会继续更新整合。。。