Struts2_Spring_Hibernate整合及测试_1(标准)

48 篇文章 0 订阅
47 篇文章 0 订阅
===================================================== 搭建环镜

一、数据库
	mysql> create database itcastoa default character set utf8;
	Query OK, 1 row affected (0.00 sec)
	mysql> show create database itcastoa;
	+----------+-------------------------------------------------------------------+
	| Database | Create Database                                                   |
	+----------+-------------------------------------------------------------------+
	| itcastoa | CREATE DATABASE `itcastoa` /*!40100 DEFAULT CHARACTER SET utf8 */ |
	+----------+-------------------------------------------------------------------+
	1 row in set (0.00 sec)	
	
二、MyEclipse工程
	1,新建Web工程,并把编码设为utf-8
		右键项目 --> Properties --> Resource --> (右边)Other:UTF-8
	2,添加框架环境
		Junit --> 右键项目 --> Build Path --> Add Library --> JUnit 
		Struts2 --> 加jar包 --> 拷配置文件 --> 写配置文件内容
		Hibernate
		Spring
	3,整合SSH
		Struts2与Spring整合
		Hibernate与Spring整合
	4,资源分类
	5,配置日志
		
Struts2
	jar包
	struts.xml,web.xml  
Hibernate	
	jar包:核心包,必须包,jpa,c3p0,jdbc
	hibernate.cfg.xml,*.hbm.xml
Spring
	jar包
	application.xml/beans.xml

声明一个bean	
	@Component("beanName") --> 
	@Controller("beanName") --> 控制层
	@Service("beanName") --> 业务层
	@Repository("beanName") --> 持久层

配置bean的scope(生命)周期
	@Scope("prototype")

注入bean
	@Resource

Spring与Struts2整合
	1,在web.xml中配置Spring的监听器
	2,加一个jar包

Hibernate与Spring整合
	1,管理SessionFactory实例(只需要一个)
	2,声明式事务管理

Spring
	IOC	管理对象...
	AOP	事务管理...


<?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: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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- 配置HibernateTemplate或SessionFactory -->
	<bean id="employeeDaoImpl" class="cn.itcast.dao.impl.EmployeeDaoImpl">
		<property name="hibernateTemplate" ref="hibernateTemplate" />
	</bean>

	<!-- 配置EmployeeService -->
	<bean id="employeeService" class="cn.itcast.service.EmployeeService">
		<property name="employeeDao" ref="employeeDaoImpl" />
	</bean>

	<!-- 配置数据库连接池 -->
	<context:property-placeholder location="jdbc.properties" />
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	<!-- 配置SessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingDirectoryLocations">
			<list>
				<value>/cn/itcast/model/</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>
	<!-- 配置HibernateTemplate -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 配置txManager -->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 配置AOP -->
	<aop:config>
		<aop:pointcut expression="execution(public * cn.itcast.service..*.*(..))"
			id="pointcut" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
	</aop:config>
	
	<!-- 配置事务范围 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
</beans>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值