spring框架第四天

spring框架第四天之 SSH三大框架整合

整合步骤

1、导包

导入整合三大框架的jar包(略)

单独配置spring容器

<1>创建配置文件,并导入约束(4个) beans|context|aop|tx
<2>配置spring随项目启动
<!-- web.xml -->
<!-- 让spring随web启动而创建的监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置spring配置文件位置参数 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
<3>启动项目测试配置,无异常则成功

单独配置struts2

<1>创建struts.xml配置文件
<!-- struts.xml -->
<struts>
	<package name="ssh" namespace="/" extends="struts-default">
		<action name="UserAction_*" class="userAction" method="{1}">
			<result name="success">/success.jsp</result>
		</action>
	</package>
</struts>
<2>配置struts2核心过滤器到web.xml
<!-- web.xml -->
<!-- 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>
<3>启动项目测试配置,无异常则成功

struts2与spring整合

<1>配置常量
<!-- struts.xml -->
<constant name="struts.objectFactory" value="spring"></constant>
<2>spring负责创建action以及组装 注意:通过spring和struts整合之后,处理用户请求的Action对象由spring容器创建,因此在struts.xml配置文件中配置Action对象时,指定class属性值不再是相应的全类名,而是spring配置文件中的bean中的id值,spring根据此id值,从spring中获取相应的Action实例
<!-- applicationContext.xml -->
<bean name="userAction" class="com.geek.web.action.UserAction" scope="prototype">
	<property name="userService" ref="userService"></property>
</bean>
<!-- struts.xml -->
<struts>
	<package name="ssh" namespace="/" extends="struts-default">
		<action name="UserAction_*" class="userAction" method="{1}">
			<result name="success">/success.jsp</result>
		</action>
	</package>
</struts>

spring整合hibernate(整合c3p0连接池)

<1>导入实体类&orm元数据
<2>配置主配置文件注意:因为整合spring和hibernate以及连接池,所以不用使用hibernate.cfg.xml配置文件配置数据库的连接
<!-- db.properties -->
jdbc.jdbcUrl=jdbc:mysql:///ssh
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=****
<!-- applicationContext.xml -->
<!-- 读取db.properties文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
	<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
	<property name="driverClass" value="${jdbc.driverClass}" ></property>
	<property name="user" value="${jdbc.user}" ></property>
	<property name="password" value="${jdbc.password}" ></property>
</bean>
<!-- applicationContext.xml -->
<!-- 在spring配置中放置hibernate配置信息 -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
	<!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 -->
	<property name="dataSource" ref="dataSource" ></property>
	<!-- 配置hibernate基本信息 -->
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>				
			<!--  可选配置 -->
			<prop key="hibernate.show_sql" >true</prop>
			<prop key="hibernate.format_sql" >true</prop>
			<prop key="hibernate.hbm2ddl.auto" >update</prop>
		</props>
	</property>
	<!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 -->
	<property name="mappingDirectoryLocations" value="classpath:com/geek/domain" ></property>
</bean>

<!-- 配置Action -->
<bean name="userAction" class="com.geek.web.action.UserAction" scope="prototype">
	<property name="userService" ref="userService"></property>
</bean>
<!-- 配置Service -->
<bean name="userService" class="com.geek.service.impl.UserServiceImpl">
	<property name="userDao" ref="userDao"></property>
</bean>
<!-- 配置Dao -->
<bean name="userDao" class="com.geek.dao.impl.UserDaoImpl">
	<!-- 注入sessionFactory -->
	<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
<3>单独书写测试类测试整合
//测试hibernate框架
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class HibernateTest {
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	@Test 
	public void fun1() {
		Configuration conf = new Configuration().configure();

		SessionFactory sessionFactory = conf.buildSessionFactory();

		Session session = sessionFactory.openSession();

		Transaction tx = session.beginTransaction();
		// -------------------------------------------------
		User u = new User();
		u.setUser_code("rose");
		u.setUser_name("肉丝");
		u.setUser_password("1234");

		session.save(u);
		// -------------------------------------------------
		tx.commit();
		session.close();

		sessionFactory.close();
	}
}

spring的aop事务(注解方式)

<!-- applicationContext.xml -->
<!-- 核心事务管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
	<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
<!-- UserServiceImpl.java测试类 -->
public class UserServiceImpl implements UserService {
	private UserDao userDao;
	
    //添加事务
	@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public void saveUser(User u) {
		// TODO Auto-generated method stub
		userDao.save(u);
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
}

扩大session作用范围

<!-- 扩大session作用范围。注意: 任何filter一定要在struts的filter之前调用 -->
   <filter>
  	<filter-name>openSessionInView</filter-name>
  	<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <!-- struts2核心过滤器 -->
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>openSessionInView</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值