A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 罗忠文 中级黑马   /  2012-11-25 11:07  /  1590 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

在Myeclipse中配置Spring和Hibernate的怎么样做,使用吃c3p0.

3 个回复

倒序浏览
首先加入相关jar;
再次就是几个重要的xml配置文件了。
下面提供一个模板,你可以再次基础上修改:
  1. <!--1.首先是web.xml配置文件: -->
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7.         <!--配置在struts2过滤器前面,且必须配合着spring的事务才能起到,sesion延迟的效果 -->
  8.         <filter>
  9.                 <filter-name>OpenSessionView</filter-name>
  10.                 <filter-class>
  11.                         org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  12.                 <init-param>
  13.                         <param-name>flushMode</param-name>
  14.                         <param-value>AUTO</param-value>
  15.                 </init-param>
  16.                 <init-param>
  17.                         <param-name>singleSession</param-name>
  18.                         <param-value>true</param-value>
  19.                 </init-param>
  20.         </filter>

  21.         <filter-mapping>
  22.                 <filter-name>OpenSessionView</filter-name>
  23.                 <url-pattern>/*</url-pattern>
  24.         </filter-mapping>

  25.         <!--struts2配置 2.1.3版本起推荐使用StrutsPrepareAndExecuteFilter -->
  26.         <filter>
  27.                 <filter-name>struts2</filter-name>
  28.                 <filter-class>
  29.                         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  30.         </filter>
  31.         <filter-mapping>
  32.                 <filter-name>struts2</filter-name>
  33.                 <url-pattern>/*</url-pattern>
  34.         </filter-mapping>


  35.         <!-- spring监听器 -->
  36.         <listener>
  37.                 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  38.         </listener>

  39.         <!-- spring上下文参数配置 -->
  40.         <context-param>
  41.                 <param-name>contextConfigLocation</param-name>

  42.                 <param-value>classpath:applicationContext.xml</param-value>
  43.         </context-param>

  44.         <!-- <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:properties/log4j.properties</param-value>
  45.                 </context-param> <context-param> <param-name>webAppRootKey</param-name> <param-value>webRoot</param-value>
  46.                 </context-param> -->

  47. </web-app>


  48. <!-- 2.接下来是struts.xml配置文件: -->
  49. <?xml version="1.0" encoding="UTF-8"?>
  50. <!DOCTYPE struts PUBLIC
  51.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  52.     "http://struts.apache.org/dtds/struts-2.0.dtd">
  53. <struts>
  54.         <constant name="struts.objectFactory" value="spring" />
  55.         <constant name="struts.i18n.encoding" value="GBK" />
  56.         <constant name="struts.devMode" value="false" />
  57.         <constant name="struts.serve.static.browserCache" value="false" />
  58.         <package name="struts2" extends="struts-default">
  59.                 <action name="studentAction" class="studentAction">
  60.                         <result>/show.jsp</result>
  61.                         <result name="error">/error.jsp</result>
  62.                 </action>
  63.         </package>
  64.         <include file="otherxml.xml"></include>
  65. </struts>

  66. <!--接下来是applicationContext.xml配置文件  -->
  67. <?xml version="1.0" encoding="UTF-8"?>
  68. <beans xmlns="http://www.springframework.org/schema/beans"
  69.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  70.         xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
  71.         xmlns:context="http://www.springframework.org/schema/context"
  72.         xsi:schemaLocation="http://www.springframework.org/schema/beans
  73.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  74.     http://www.springframework.org/schema/tx  
  75.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  76.     http://www.springframework.org/schema/aop  
  77.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  78.     http://www.springframework.org/schema/context
  79.     http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  80.         <bean id="data"
  81.                 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  82.                 <property name="location" value="classpath:dataSource.properties"></property>
  83.         </bean>
  84.         <!-- 使用C3P0连接池的配置 -->
  85.         <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  86.                 <!--这些数据库的连接参数(name值)与原来的不大一样 -->
  87.                 <property name="driverClass" value="${driverClassName}" />
  88.                 <property name="jdbcUrl" value="${url}" />
  89.                 <property name="user" value="${username}" />
  90.                 <property name="password" value="${password}" />

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

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

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

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

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

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

  106.         </bean>
  107.         <bean id="sessionFactory"
  108.                 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  109.                 <property name="dataSource">
  110.                         <ref bean="dataSource"></ref>
  111.                 </property>
  112.                 <property name="hibernateProperties">
  113.                         <props>
  114.                                 <prop key="hibernate.dialect">
  115.                                         org.hibernate.dialect.MySQLDialect
  116.                                 </prop>
  117.                                 <!-- 设置二级缓冲 -->
  118.                                 <prop key="hibernate.cache.provider_class">
  119.                                         org.hibernate.cache.EhCacheProvider
  120.                                 </prop>

  121.                                 <!-- 设置二级缓冲,打开查询缓冲 -->
  122.                                 <prop key="hbm2ddl.auto">create</prop>
  123.                                 <!-- <prop key="hibernate.format_sql">true</prop> -->
  124.                                 <prop key="hibernate.cache.use_query_cache">true</prop>
  125.                                 <prop key="hibernate.show_sql">true</prop>

  126.                         </props>
  127.                 </property>
  128.                 <property name="mappingResources">
  129.                         <list>
  130.                                 <!-- ******* 将vo导入到applicationContext中 ****** -->
  131.                                 <value>com/itcast/hibernate/po/Student.hbm.xml</value>
  132.                                 <value>com/itcast/hibernate/po/Sgroup.hbm.xml</value>

  133.                         </list>
  134.                 </property>
  135.         </bean>
  136.         <!-- 配置事务管理 -->
  137.         <bean id="transactionManager"
  138.                 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  139.                 <property name="sessionFactory" ref="sessionFactory" />
  140.         </bean>
  141.         <aop:config>
  142.                 <aop:pointcut id="serviceOperation" expression="execution(* com.itcast.dao..*(..))" />
  143.                 <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice" />
  144.         </aop:config>
  145.         <tx:advice id="txAdvice">
  146.                 <tx:attributes>
  147.                         <tx:method name="get*" read-only="true" />
  148.                         <tx:method name="find*" read-only="true" />
  149.                         <tx:method name="save*" />
  150.                         <tx:method name="update*" />
  151.                         <tx:method name="*delete" />
  152.                         <tx:method name="*" />
  153.                 </tx:attributes>
  154.         </tx:advice>

  155.         <bean id="StudentDAO" class="com.itcast.dao.impl.StudentDAOImpl">
  156.                 <property name="sessionFactory">
  157.                         <ref bean="sessionFactory" />
  158.                 </property>
  159.         </bean>
  160.         <bean id="studentService" class="com.itcast.service.impl.StudentServiceImpl">
  161.                 <property name="studentDao" ref="StudentDAO"></property>
  162.         </bean>
  163.         <bean id="SgroupDAO" class="com.itcast.dao.impl.SgroupDAOImpl">
  164.                 <property name="sessionFactory">
  165.                         <ref bean="sessionFactory" />
  166.                 </property>
  167.         </bean>
  168.         <bean id="sgroupService" class="com.itcast.service.impl.SgroupServiceImpl">
  169.                 <property name="sgroupDao" ref="SgroupDAO"></property>
  170.         </bean>

  171.         <bean id="studentAction" class="com.itcast.struts2.action.StudentAction">
  172.                 <property name="studentService" ref="studentService"></property>
  173.                 <property name="sgroupService" ref="sgroupService"></property>
  174.         </bean>
  175. </beans>

复制代码

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
谢谢问下应该用哪个版本啊Spring和Hibernate
回复 使用道具 举报
罗忠文 发表于 2012-11-25 14:38
谢谢问下应该用哪个版本啊Spring和Hibernate


建议用struts2.1.8+,hibernate3.3+,spring2.5+。现在hibernate4,spring3都出来了,如果时间多的话可以到官网去了解些。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马