ssh整合的xml基本配置

web.xml基本配置
<!-- 由于session 会在执行完数据库操作后会关闭 当有些对象会需要延迟加载 这个时候就需要session 而session被已经被关闭 所有需要开启这个过滤器 代表着需要session的时候就会重新打开session -->
<!-- Spring开启Hibernate的OpenSessionView模式 --> 
	<filter>
	    <filter-name>OpenSessionInViewFilter</filter-name>
	    <filter-class>
	      org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
	    </filter-class>
	</filter>
	<filter-mapping>
	    <filter-name>OpenSessionInViewFilter</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>*.action</url-pattern>
	</filter-mapping>
	<!-- Spring的配置 -->
	<context-param>
	    <param-name>contextConfigLocation</param-name>
	    <param-value>classpath:config/spring.xml,classpath:config/springDao.xml</param-value>
	</context-param>
	<listener>
	    <listener-class>
	        org.springframework.web.context.ContextLoaderListener
	    </listener-class>
	</listener>
spring.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: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.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.2.xsd" default-autowire="byType">
	
	<!-- 未加注解前需要这么写 -->
	<!-- entity -->
	<!-- <bean id="student" class="com.znsd.ssh.entitys.Student" scope="prototype"></bean>
	<bean id="course" class="com.znsd.ssh.entitys.Course" scope="prototype"></bean> -->
	
	<!-- <bean id="item" class="com.znsd.vote.entity.Item"></bean> 
	<bean id="option" class="com.znsd.vote.entity.Option"></bean> 
	<bean id="subject" class="com.znsd.vote.entity.Subject"></bean> 
	<bean id="user" class="com.znsd.vote.entity.User"></bean> 
	
	
	service
	<bean id="voteServiceImpl" class="com.znsd.vote.biz.impl.VoteServiceImpl"></bean>
	
	dao
	<bean id="itemDaoImpl" class="com.znsd.vote.dao.impl.ItemDaoImpl"></bean>
	<bean id="subjectDaoImpl" class="com.znsd.vote.dao.impl.SubjectDaoImpl"></bean>
	<bean id="userDaoImpl" class="com.znsd.vote.dao.impl.UserDaoImpl"></bean>
	
	action
	<bean id="loginAction" class="com.znsd.vote.action.LoginAction" scope="prototype"></bean>
	<bean id="subjectAction" class="com.znsd.vote.action.SubjectAction" scope="prototype"></bean>
	<bean id="updateAction" class="com.znsd.vote.action.UpdateAction" scope="prototype"></bean>
	<bean id="voteAction" class="com.znsd.vote.action.VoteAction" scope="prototype"></bean>
 -->
 
 	<!-- 使用注解写法 只需要配置一个注解扫描器即可 -->
 	<context:component-scan base-package="com.spring.vote"/>
 
 
</beans>
hibernate.cfg.xml配置
<session-factory>
		<!-- 数据库链接配置 -->
		<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">157953</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
		<!-- c3p0连接池配置 -->
		<!-- 最大连接数 -->
		<property name="hibernate.c3p0.max_size">20</property>
		<!-- 最小连接数 -->
		<property name="hibernate.c3p0.min_size">5</property>
		<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
		<property name="hibernate.c3p0.timeout">120</property>
		<!-- 最大的PreparedStatement的数量 -->
		<property name="hibernate.c3p0.max_statements">100</property>
		<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
		<property name="hibernate.c3p0.idle_test_period">120</property>
		<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
		<property name="hibernate.c3p0.acquire_increment">2</property>
		<!-- 每次都验证连接是否可用 -->
		<property name="hibernate.c3p0.validate">true</property>
		
		
		<property name="show_sql">true</property>
		<!-- 格式化sql语句 -->
		<property name="format_sql">true</property>
		<!-- 数据库生成策略 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<property name="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</property>
	</session-factory>
增加springdao.xml配置
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		
	<!-- 加载数据源对象,spring的配置方式 --><!-- 对于数据源如果写在本xml内便要写上该标签 -->
	<!-- <property name="dataSource" ref="dataSource" /> -->
	
	<!-- 读取hibernate config文件  name值固定-->
	<property name="configLocation" value="classpath:config/hibernate.cfg.xml"/>
	
	<!-- 读取映射文件 name值固定-->							
	<property name="mappingLocations" value="classpath:com/znsd/vote/entity/hbm/*.hbm.xml"></property>
	</bean>
	
	<!-- 配置事务 -->
	<bean id="txManager" 
	    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
	    <property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<!-- 事务的通知方式 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
	<tx:attributes>
	<tx:method name="find*" propagation="REQUIRED" read-only="true" />
	<tx:method name="search*" propagation="REQUIRED" read-only="true" />
	<tx:method name="query*" propagation="REQUIRED" read-only="true" />
	
	<tx:method name="create*" propagation="REQUIRED" />
	<tx:method name="add*" propagation="REQUIRED" />
	<tx:method name="submit*" propagation="REQUIRED" />
	<tx:method name="save*" propagation="REQUIRED" />
	<tx:method name="insert*" propagation="REQUIRED" />
	
	<tx:method name="del*" propagation="REQUIRED" />
	<tx:method name="remove*" propagation="REQUIRED" />
	
	<tx:method name="update*" propagation="REQUIRED" />
	<tx:method name="modify*" propagation="REQUIRED" />
	
	<tx:method name="*" propagation="REQUIRED" read-only="true" />
	</tx:attributes>
	</tx:advice>
	
	<!-- AOP切面拦截事务 --><!-- 选择拦截的包路径 -->
	<aop:config>
	    <aop:pointcut id="serviceMethod" 
	        expression="execution(* com.znsd.vote.biz.*.*.*(..))" />
	    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
	</aop:config> 

注意的是数据源在hibernate或者springdaoxml中配置一个就行了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值