您的位置:首页 > 其它

s2sh整合

2016-06-28 10:14 281 查看
1.导入相关的jar包



2. 整合Spring与Hibernate
目的:
让Spring管理单例的SessionFactory
让Spring管理事务(采用基于注解的方式)
步骤:
(1.配置applicationContext.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: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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
<!-- 自动扫描与装配bean -->
<context:component-scan base-package="com.zzc.oa"></context:component-scan>

<!-- 导入外部的properties配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- =========== 数据库连接信息 =========== -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- =========== 连接池的管理配置 =========== -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>

<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

<!-- 配置声明式的事务管理(采用基于注解的方式) -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>

</beans>
               ( 2.配置hibernate.cfg.xml

<!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.url">jdbc:mysql:///itcastoa</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property> <property
name="hibernate.connection.password">1234</property> -->

<property name="hibernate.dialect">
org.hibernate.dialect.MySQL5InnoDBDialect
</property>

<!-- 其他配置信息 -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>

<!-- 声明映射文件 -->
<mapping resource="com/zzc/oa/domain/User.hbm.xml" />

</session-factory>
</hibernate-configuration>

(3.配置数据库jdbc.properties (为了便于修改)

jdbcUrl		= jdbc:mysql:///oa
driverClass = com.mysql.jdbc.Driver
username	= root
password	= root


3.整合Spring与Struts2
目的:
让Spring管理Action的实例(多例)。
这样就可以方便的注入依赖的对象了。
步骤:
(1,Spring在Web应用中使用时要配置一个监听器
为了创建ApplicationContext对象

<!-- 配置Spring的用于初始化ApplicationContext对象的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>

<!-- 配置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>


(2,添加struts2-spring-plugin.jar
为了让Struts2从Spring的容器中拿Action对象

  (3,配置struts.xml文件

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

<struts>
<!-- 配置为开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- 把action扩展名改为.do -->
<constant name="struts.action.extension" value="do" />
<!-- 把主题设为simple -->
<constant name="struts.ui.theme" value="simple" />

<package name="default" namespace="/" extends="struts-default">

<!-- 配置测试用的action -->
<action name="Test" class="com.zzc.oa.test.TestAction">
<result name="success">/test.jsp</result>
</action>

</package>

</struts>


简单配置完毕···只是做一个简单记录
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  s2sh