20170316_ssh(struts2+spring3+hiberanate3)整合--注意jar
目的:简单的ssh框架搭建以及用junit4测试。
1、新建web project工程
2、更改工程编码格式
3、导入junit4.jar
4、导入struts2.jar
web.xml中配置struts2 的主过滤器
1 <!-- 配置Struts2的主过滤器 --> 2 <filter> 3 <filter-name>struts2</filter-name> 4 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 5 </filter> 6 <filter-mapping> 7 <filter-name>struts2</filter-name> 8 <url-pattern>/*</url-pattern> 9 </filter-mapping>
struts2配置文件:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <!-- 配置为开发模式 --> 8 <constant name="struts.devMode" value="true" /> 9 <!-- 配置扩展名为action --> 10 <constant name="struts.action.extension" value="action" /> 11 12 13 <package name="default" namespace="/" extends="struts-default"> 14 15 16 </package> 17 18 </struts>
5、导入hibernate.jar
hibernate3的配置文件:
hibernate.cfg.xml映射文件:
6、spring.jar
a.核心包--spring.jar
b.日志包
c.aop面向切面
d.生成代理
web.xml中配置spring的监听器:
1 <!-- 配置Spring的监听器,用于初始化ApplicationContext对象 --> 2 <listener> 3 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 4 </listener> 5 <context-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:applicationContext*.xml</param-value> 8 </context-param>
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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 自动扫描与装配bean --> <context:component-scan base-package="cn.itcast.oa"></context:component-scan> <!-- 加载外部的properties配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据库连接池(c3p0) --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 基本信息 --> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="driverClass" value="${driverClass}"></property> <property name="user" value="${username}"></property> <property name="password" value="${password}"></property> <!-- 其他配置 --> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="3"></property> <!--连接池中保留的最小连接数。Default: 3 --> <property name="minPoolSize" value="3"></property> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="5"></property> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="3"></property> <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --> <property name="maxStatements" value="8"></property> <!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --> <property name="maxStatementsPerConnection" value="5"></property> <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="1800"></property> </bean> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <!-- 配置声明式的事务管理(采用基于注解的方式) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
============================================================================
整合:
1、整合spring+hibernate:
就是让spring管理SessionFactory和事务。而SessionFactory需要用到数据源。所以applicationContext.xml中配置:数据源c3p0、SessionFactory、事务三个内容。
如何测试整合spring+hibernate是否正确?
测试两个:第一个测试SessionFactory管理是否正确,第二个测试事务管理是否正确
1 package cn.itcast.oa.test; 2 3 import org.hibernate.SessionFactory; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 public class SpringTest { 9 10 private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 11 12 // 测试SessionFactory管理是否正确,拿到容器获取SessionFactory,能得到SessionFactory就行 13 // 容器创建是单例的,就马上创建,创建不报错就行 14 @Test 15 public void testSessionFactory() throws Exception { 16 SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory"); 17 System.out.println(sessionFactory); 18 } 19 20 // 测试事务管理是否正确, 21 // 测试事务管理是否正确需要借助另外一个service层,因为它要生成service的代理。 22 @Test 23 public void testTransaction() throws Exception { 24 25 // TestService testService = new TestService(); 26 TestService testService = (TestService) ac.getBean("testService"); 27 testService.saveTwoUsers(); 28 29 } 30 31 }
1 package cn.itcast.oa.test; 2 3 import javax.annotation.Resource; 4 5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import org.springframework.stereotype.Service; 8 import org.springframework.transaction.annotation.Transactional; 9 10 import cn.itcast.oa.domain.User; 11 12 @Service("testService") 13 public class TestService { 14 15 @Resource 16 private SessionFactory sessionFactory; 17 18 @Transactional 19 public void saveTwoUsers() { 20 Session session = sessionFactory.getCurrentSession(); 21 session.save(new User()); 22 int a = 1 / 0; // 这行会抛异常 23 session.save(new User()); 24 } 25 }
2、struts2单独测试:
public class TestAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("------->TestAction.execute()");
return "success";
}
}
<package name="default" namespace="/" extends="struts-default">
<action name="test" class="cn.itcast.oa.test.TestAction">
<result name="success">/test.jsp</result>
</action>
</package>
3、struts2+spring 整合:
首先导入一个jar
然后在action中添加注解@Controller、@Scope("prototype"),
最后struts.xml文件中当与Spring整合后,class属性写的就是Spring中bean的名称。
@Controller
@Scope("prototype")
public class TestAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("------->TestAction.execute()");
return "success";
}
}
ps:scope
可以利用容器的scope="prototype"来保证每一个请求有一个单独的Action来处理, 避免struts中Action的线程安全问题
spring 默认scope 是单例模式
这样只会创建一个Action对象
每次访问都是同一个Action对象,数据不安全
struts2 是要求 每次次访问 都对应不同的Action
scope="prototype" 可以保证 当有请求的时候 都创建一个Action对象
<package name="default" namespace="/" extends="struts-default">
<action name="test" class="testAction">
<result name="success">/test.jsp</result>
</action>
</package>
4、struts2+spring+hibernate整合:就是在action 中调用service,service调用数据库
@Controller
@Scope("prototype")
public class TestAction extends ActionSupport {
@Resource
private TestService testService;
@Override
public String execute() throws Exception {
testService.saveTwoUsers();
System.out.println("------->TestAction.execute()");
return "success";
}
}
ps:在改方法内容不需要重新启动。改方法,改类的结构要重新启动。
重新启动:
重新发布:
END!!!