SSH 整合

一、Spring + Hibernate

1.创建数据源配置文件 resources.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/tapwater?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.user=root
jdbc.password=root

2.创建Sprint核心文件 applicationContext-core.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: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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<!-- 1. 载入 resources.properties -->
	<context:property-placeholder location="classpath:resources.properties" />

	<!-- 2. 配置数据源(需引入C3P0,同时删除hibernate.hbm.xml中的数据源配置) -->
	<!-- 要引用C3PO中的ComboPooledDataSource类 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 读取resources.properties中参数值 -->

		<!-- 数据源信息 -->
		<property name="user" value="${jdbc.user}"></property>

		<property name="password" value="${jdbc.password}"></property>

		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

		<property name="driverClass" value="${jdbc.driverClass}"></property>

		<!--连接池中保留的最小连接数。 -->
		<property name="minPoolSize" value="5" />

		<!--连接池中保留的最大连接数。Default: 30 -->
		<property name="maxPoolSize" value="30" />

		<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize" value="10" />

		<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="300" />

		<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement" value="5" />

		<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 
			如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
		<property name="maxStatements" value="0" />

		<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
		<property name="idleConnectionTestPeriod" value="300" />

		<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
		<property name="acquireRetryAttempts" value="30" />

		<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 
			获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
		<property name="breakAfterAcquireFailure" value="true" />

		<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 
			等方法来提升连接测试的性能。Default: false -->
		<property name="testConnectionOnCheckout" value="false" />

		<!-- 注意:resources.properties中的key尽量不要与这里name的值一致,会引起冲突 -->
	</bean>

	<!-- 3. 配置SessionFactory -->
	<!-- 实例化LocalSessionFactoryBean类,需要在pom.xml中引入spring-orm支持依赖 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 1.引用数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 2.配置hibernate其他参数 -->
		<property name="hibernateProperties">
			<props>
				<!-- 格式化sql -->
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<!-- 开启二级缓存 -->
				<prop key="hibernate.cache.use_second_level_cache">true</prop>
				<!-- 开启查询缓存 -->
				<prop key="hibernate.cache.use_query_cache">true</prop>
				<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
				<!-- 
					配置线程Session 
					1.openSession()【每次都会创建新的Session】 
					2.getCurrentSession()【获取当前线程的Session,如果没有则创建】
				-->
				<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
			</props>
		</property>
		<!-- 3.加载hibernate映射文件 -->
		<property name="mappingLocations" value="classpath:cn/kaxlm6/tapwater/pojo/*.hbm.xml" />
	</bean>

	<!-- 4. 配置事务 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!-- 5. 配置事务的属性 -->
	<tx:advice id="myAdvice" transaction-manager="transactionManager">
		<!-- 事务的属性 -->
		<tx:attributes>
			<!-- REQUIRED - 无事务会自动增加一个事务 -->
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="edit*" propagation="REQUIRED" />
			<tx:method name="query*" propagation="REQUIRED" />
			<tx:method name="*" />
		</tx:attributes>
	</tx:advice>

	<!-- 6. 配置事务的切点 -->
	<!-- 指明哪些包里的哪些类,哪些类中的哪些方法要事务 -->
	<aop:config>
		<!-- * cn.kaxlm6.tapwater.dao.impl.*.*(..)表示cn.kaxlm6.tapwater.dao.impl包下所有的类中的所有方法,这是一种固定写法 -->
		<aop:pointcut id="myCut"
			expression="execution(* cn.kaxlm6.tapwater.dao.impl.*.*(..))" />
		<!--注意:需要再次引入一个依赖:spring-aspects,用于解析上面这个表达式 -->
		<!-- 通知关联,事务的属性 -->
		<aop:advisor advice-ref="myAdvice" pointcut-ref="myCut" />
	</aop:config>

	<!-- 引用bean -->                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
	<import resource="classpath:applicationContext-bean.xml" />

	<!-- 引用自定义bean -->
	<import resource="classpath:mySpringBean/applicationContext-*.xml" />

</beans>

二、Struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

	<!-- 设置字符集为UTF-8 -->
	<constant name="struts.i18n.encoding" value="UTF-8"/>
	
	<!-- 设置struts2允许的后缀名 -->
	<constant name="struts.action.extension" value="do,action,xlm"/>
	
	<!-- 是否允许浏览器缓存静态内容 -->
	<constant name="struts.serve.static.browserCache" value="false"/>
	
	<!-- 配置文件变化时是否重新加载 -->
	<constant name="struts.configuration.xml.reload" value="true"/>
	
	<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认为false,开发阶段应打开 -->
	<constant name="struts.configuration.xml.reload" value="true"></constant>
	
	<!-- 开发模式下使用,可以打印出更详细的错误信息 -->
	<constant name="struts.devMode" value="true"/>
	
	<!-- 是否允许动态方法调用 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
	
	<!-- 由spring担任工程bean -->
	<constant name="struts.ObjectFactory" value="spring"/>

	<!-- dispather/redirect 转发/重定向 -->
	
	<!-- <package name="myUser" namespace="/" extends="struts-default"> -->

		<!-- 配置404页面 -->
		<!-- <global-results><result>404.jsp</result></global-results> -->

		<!-- 使用通配符所需配置 -->
		<!-- <global-allowed-methods>regex:.*</global-allowed-methods> -->

		<!-- 配置调用的动作类与方法 -->
		<!-- <action name="*User" class="userServlet" method="{1}User" /> -->

	<!-- </package> -->

	<!-- 包含struts-*.xml文件 -->
	<include file="myStruts/struts-*.xml"/>

</struts>

三、Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">

	<display-name>Archetype Created Web Application</display-name>

	<!--添加监听 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!--加载Spring -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext-core.xml</param-value>
	</context-param>

	<!-- 配置字符集拦截器,由spring提供 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>	

	<!-- 配置Struts2核心过滤器 -->
	<filter>
		<filter-name>Struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>Struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 配置404页面 -->
	<error-page>
		<error-code>404</error-code>
		<location>/404.jsp</location>
	</error-page>

</web-app>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值