Spring Framework(7)-SSH整合过程总结

点击查看更多Spring文章

目录

各框架的作用

1、需要导入的jar包

2、单独配置spring配置文件,再src下创建applicationContext.xml配置文件并导入约束


各框架的作用

 

1、需要导入的jar包

1)hibernate所需:

9个基础核心jar包:

jpa包:

 

 

2)struts2所需:

13个基础核心jar包:

导入后发现有两个重复jar包,删去版本低的一个即可

再导入一个整合spring包,如果不整合spring就不要导入,如果导入后,找不到spring 容器就会抛出异常

 

3)spring所需基础jar包:

太复杂,20个全导入即可

再导入一个c3p0包,方便使用c3p0连接池

 

2、单独配置spring配置文件,再src下创建applicationContext.xml配置文件并导入约束

然后在web.xml中书写监听器和配置参数

 <!-- 配置spirng随项目的启动而启动 -->
  <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,导入web.xml的核心约束,书写过滤器

<!-- 配置strur2核心过滤器 -->
  <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>  

创建struts.xml,并配置以下常量,将struts2交给spring管理生命周期

注意,applicationContext.xml中的bean配置成多例对象,因为每次请求都会创建一个action

4、配置hibernate

配置hibernate.cfg.xml主配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
	
		 <!-- 数据库驱动 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		 <!-- 数据库url -->
		<property name="hibernate.connection.url">jdbc:mysql:///ssh_crm</property>
		 <!-- 数据库连接用户名 -->
		<property name="hibernate.connection.username">root</property>
		 <!-- 数据库连接密码 -->
		<property name="hibernate.connection.password">123</property>
		<!-- 数据库方言
			不同的数据库中,sql语法略有区别. 指定方言可以让hibernate框架在生成sql语句时.针对数据库的方言生成.
			sql99标准: DDL 定义语言  库表的增删改查
					  DCL 控制语言  事务 权限
					  DML 操纵语言  增删改查
			注意: MYSQL在选择方言时,请选择最短的方言.
		 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	
		<!-- 将hibernate生成的sql语句打印到控制台 -->
		<property name="hibernate.show_sql">true</property>
		<!-- 将hibernate生成的sql语句格式化(语法缩进) -->
		<property name="hibernate.format_sql">true</property>
	
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!-- 引入orm元数据
			路径书写: 填写src下的路径
		 -->
		<mapping resource="com/iteason/domain/User.hbm.xml" />
		
	</session-factory>
</hibernate-configuration>

这是单配置hibernate,配置hibernate与spring整合时,需要把sessionFactroy对象交给spring管理

方案一,在applicationContext.xml中配置,仍使用外部的hibernate.xml配置文件

<!-- 加载配置 方案1、使用外部的hibernatr.cfg.xml配置信息-->
		 <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
			<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
		</bean> 

方案二:在applicationContext.xml中配置,不使用外部配置

<!-- 加载配置 方案2、在spring容器中配置 -->
		<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
			<!-- 配置基本信息 -->
			<property name="hibernateProperties">
				<props>
					<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
					<prop key="hibernate.connection.url">jdbc:mysql:///ssh_crm</prop>
					<prop key="hibernate.connection.username">root</prop>
					<prop key="hibernate.connection.password">123</prop>
					<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元数据所在的包路径即可-->
			<property name="mappingDirectoryLocations" value="classpath:com/iteason/domain"></property>
		</bean>

引入c3p0连接池,注意要在src下配置好c3p0-config.xml

 	
		 <!-- 配置c3p0连接池 -->
		 <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"></bean>
		 

在sessionFactory中,多加一行配置,并删除下面四个数据库连接配置,由连接池管理

<!-- 将连接池注入到sessionFactory中 -->
			<property name="dataSource" ref="dataSource"></property>
			

书写dao实现类,基础HibernateDaoSupport

在applicationContext.xml中配置好UserDaoImp

 

 

配置aop事务

<!-- 配置通知 -->
		<tx:advice id="txAdvice" transaction-manager="transactionManager">
			<tx:attributes>
				<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
				<tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
				<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
				<tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
				<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
				<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
				<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
				<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
				</tx:attributes>
		</tx:advice>
		 <!-- 织入切点 -->
		 <aop:config>
		 	<aop:pointcut expression="execution(* com.iteason.serviceimp.*ServiceImp.*(..))" id="txPc"/>
		 	<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/>
		 </aop:config>

注解方式配置aop事务

 <!-- 开启注解事务 -->
		 <tx:annotation-driven transaction-manager="transactionManager"/>

在serviceimp层的配置为:

@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class UserServiceImp implements UserService {

	private UserDao ud;
	
	
	@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public void saveUser(User u) {
		 ud.saveUser(u);
	}


	public void setUd(UserDao ud) {
		this.ud = ud;
	}

 

在web.xml中配置扩大hibernate的session作用范围,注意要配置在struts2的filter之前

<!-- 扩大session作用范围 -->
  <filter>
  	<filter-name>openSessionInView</filter-name>
  	<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>openSessionInView</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping> 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BoringRong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值