黑马程序员技术交流社区
标题:
SSH怎么整合
[打印本页]
作者:
罗忠文
时间:
2012-11-25 11:07
标题:
SSH怎么整合
在Myeclipse中配置Spring和Hibernate的怎么样做,使用吃c3p0.
作者:
yzqiong5566
时间:
2012-11-25 14:33
首先加入相关jar;
再次就是几个重要的xml配置文件了。
下面提供一个模板,你可以再次基础上修改:
<!--1.首先是web.xml配置文件: -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!--配置在struts2过滤器前面,且必须配合着spring的事务才能起到,sesion延迟的效果 -->
<filter>
<filter-name>OpenSessionView</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--struts2配置 2.1.3版本起推荐使用StrutsPrepareAndExecuteFilter -->
<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>
<!-- spring监听器 -->
<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>
<!-- <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:properties/log4j.properties</param-value>
</context-param> <context-param> <param-name>webAppRootKey</param-name> <param-value>webRoot</param-value>
</context-param> -->
</web-app>
<!-- 2.接下来是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.objectFactory" value="spring" />
<constant name="struts.i18n.encoding" value="GBK" />
<constant name="struts.devMode" value="false" />
<constant name="struts.serve.static.browserCache" value="false" />
<package name="struts2" extends="struts-default">
<action name="studentAction" class="studentAction">
<result>/show.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
<include file="otherxml.xml"></include>
</struts>
<!--接下来是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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="data"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:dataSource.properties"></property>
</bean>
<!-- 使用C3P0连接池的配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--这些数据库的连接参数(name值)与原来的不大一样 -->
<property name="driverClass" value="${driverClassName}" />
<property name="jdbcUrl" value="${url}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
<!--连接池中保留的最小连接数。 -->
<property name="minPoolSize">
<value>5</value>
</property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="30" />
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="10" />
<!--最大空闲时间,600秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="600" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="0" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- 设置二级缓冲 -->
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<!-- 设置二级缓冲,打开查询缓冲 -->
<prop key="hbm2ddl.auto">create</prop>
<!-- <prop key="hibernate.format_sql">true</prop> -->
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<!-- ******* 将vo导入到applicationContext中 ****** -->
<value>com/itcast/hibernate/po/Student.hbm.xml</value>
<value>com/itcast/hibernate/po/Sgroup.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config>
<aop:pointcut id="serviceOperation" expression="execution(* com.itcast.dao..*(..))" />
<aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="save*" />
<tx:method name="update*" />
<tx:method name="*delete" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<bean id="StudentDAO" class="com.itcast.dao.impl.StudentDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="studentService" class="com.itcast.service.impl.StudentServiceImpl">
<property name="studentDao" ref="StudentDAO"></property>
</bean>
<bean id="SgroupDAO" class="com.itcast.dao.impl.SgroupDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="sgroupService" class="com.itcast.service.impl.SgroupServiceImpl">
<property name="sgroupDao" ref="SgroupDAO"></property>
</bean>
<bean id="studentAction" class="com.itcast.struts2.action.StudentAction">
<property name="studentService" ref="studentService"></property>
<property name="sgroupService" ref="sgroupService"></property>
</bean>
</beans>
复制代码
作者:
罗忠文
时间:
2012-11-25 14:38
谢谢问下应该用哪个版本啊Spring和Hibernate
作者:
yzqiong5566
时间:
2012-11-25 15:09
罗忠文 发表于 2012-11-25 14:38
谢谢问下应该用哪个版本啊Spring和Hibernate
建议用struts2.1.8+,hibernate3.3+,spring2.5+。现在hibernate4,spring3都出来了,如果时间多的话可以到官网去了解些。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2