`

spring + struts + hibernate 数据源的几种配置方法(原创)

阅读更多

spring + struts + hibernate 数据源的几种配置方法


环境:JDK 1.6 + tomcat 6.0 + spring2.5 + struts2.18 + hibernate3.x

 

1. c3p0数据连接池 (需要将mysql-connector-java-5.0.8-bin.jar文件copy到tomcat 的lib下)
 <?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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 - <!--  使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入
   -->
   <context:component-scan base-package="cn.com.zqk" />
 - <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
   <property name="driverClass" value="com.mysql.jdbc.Driver" />

   <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=gbk" />
 - <property name="properties">
 - <props>
   <prop key="c3p0.minPoolSize">2</prop>
   <prop key="c3p0.maxPoolSize">100</prop>
   <prop key="c3p0.timeout">5000</prop>
   <prop key="c3p0.max_statement">100</prop>
   <prop key="c3p0.testConnectionOnCheckout">true</prop>
 - <!--  获取connnection时测试是否有效 
   -->
   <prop key="c3p0.testConnectionOnCheckin">true</prop>
 - <!--  自动测试的table名称 
   -->
   <prop key="c3p0.automaticTestTable">t_wb_zqk</prop>
 - <!--  set to something much less than wait_timeout, prevents connections from going stale 
   -->
   <prop key="c3p0.idleConnectionTestPeriod">18000</prop>
 - <!--  set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out 
   -->
   <prop key="c3p0.maxIdleTime">25000</prop>
   <prop key="user">root</prop>
   <prop key="password">123456</prop>
 - <!--  set to 'SELECT 1' 
   -->
   <prop key="validationQuery">SELECT 1</prop>
 - <!--  set to 'true' 
   -->
   <prop key="testWhileIdle">true</prop>
 - <!--  some positive integer 
   -->
   <prop key="timeBetweenEvictionRunsMillis">3600000</prop>
 - <!--  set to something smaller than 'wait_timeout' 
   -->
   <prop key="minEvictableIdleTimeMillis">18000000</prop>
 - <!--  if you don't mind a hit for every getConnection(), set to "true"
   -->
   <prop key="testOnBorrow">true</prop>
   </props>
   </property>
   </bean>
 - <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource" ref="dataSource" />
 - <property name="mappingDirectoryLocations">
 - <list>
   <value>classpath:/cn/com/zqk/model/</value>
   </list>
   </property>
 - <property name="hibernateProperties">
 - <value>
   hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
 - <!--  hibernate.hbm2ddl.auto=update
   -->
   hibernate.show_sql=false hibernate.format_sql=true
   </value>
   </property>
   </bean>
 - <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
   </bean>
 - <!-- 使用基于注解方式配置事务
   -->
   <tx:annotation-driven transaction-manager="txManager" />
   </beans>

2. JNDI + proxoolDB 连接池(需要将mysql-connector-java-5.0.8-bin.jar,proxool-0.9.1.jar,proxool-cglib.jar文件copy到tomcat 的lib下)

 第一步:在tomcat/conf/context.xml中配置JDNI
  <Resource  name="jdbc/jndiname"
      auth="Container"
      type="javax.sql.DataSource"
   factory="org.logicalcobwebs.proxool.ProxoolDataSource"
      proxool.alias="proxoolDB"
      user="root"
      password="123456"
      delegateProperties="foo=1,bar=true"
      proxool.jndi-name="jndiname"   
      proxool.driver-url="jdbc:mysql://localhost:3306/dbname?useUnicode=true&amp;characterEncoding=GBK"    
      proxool.driver-class="com.mysql.jdbc.Driver"  
      proxool.house-keeping-sleep-time="900000"  
      proxool.maximum-active-time="5"
      proxool.prototype-count="3"
      proxool.statistics="1m,15m,1d"
      proxool.simultaneous-build-throttle="10"
      proxool.minimum-connection-count="5"
      proxool.maximum-connection-count="100"
      proxool.house-keeping-test-sql="select * from t_wb_zqk" 
      proxool.test-before-use="true"/>
 
 第二步: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:aop="http://www.springframework.org/schema/aop"
        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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  
  <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
  <context:component-scan base-package="cn.com.zqk" />
        
  <bean id="dataSource"  class="org.springframework.jndi.JndiObjectFactoryBean">
   <property name="jndiName">
   <value>java:comp/env/jdbc/jndiname</value>
   </property>
  </bean>
  

  
  <bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource" ref="dataSource" />

   <property name="mappingDirectoryLocations">
    <list>
        <value>classpath:/cn/com/zqk/model/</value>            
    </list>
   </property>
   
   <property name="hibernateProperties">
    <value>
     hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
     <!-- hibernate.hbm2ddl.auto=update -->
     hibernate.show_sql=true
     hibernate.format_sql=true    
     hibernate.cache.use_query_cache = true
     hibernate.cache.provider_class = org.hibernate.cache.EhCacheProvider
     hibernate.cache.use_second_level_cache = true
     <!-- hibernate.connection.release_mode = after_statement --> 
    </value>
   </property>
  </bean>
  
  
  
  <bean id="txManager"
   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
  </bean>
  <!--使用基于注解方式配置事务 -->
  <tx:annotation-driven transaction-manager="txManager" />

 </beans>

 

 

个人签名

-------------------------------------

 

图盾 淘宝保护 保护图片 图片防盗

0
2
分享到:
评论

相关推荐

    Spring+Struts2+Hibernate三大框架开发校园精品课程网源代码

    本项目以"Spring+Struts2+Hibernate"三大框架为核心,旨在实现一个校园精品课程网的后台系统,提供丰富的功能和良好的用户体验。下面将详细介绍这三个框架以及它们在该项目中的应用。 1. Spring框架:Spring是Java...

    图书管理系统spring+struts+hibernate

    《图书管理系统spring+struts+hibernate》是一款基于Java技术栈开发的图书管理软件,其核心框架包括Spring、Struts和Hibernate。该系统利用MySQL作为数据库存储数据,提供了完整的数据库备份,确保了数据的安全性与...

    学生选课管理系统(java开发,框架是spring+struts1.2+hibernate)

    该系统采用经典的Java开发技术,结合Spring、Struts1.2和Hibernate三大框架,构建了一个功能完备、稳定可靠的选课平台。 首先,Spring框架作为核心的依赖注入(DI)和面向切面编程(AOP)容器,负责管理应用中的...

    ssh2整合(spring+struts2+hibernate)-附

    SSH2整合指的是Spring、Struts2和Hibernate三个开源框架的集成应用。这三大框架分别负责不同的职责:Spring作为基础架构框架,提供依赖注入(DI)和面向切面编程(AOP),Struts2则用于MVC(Model-View-Controller)...

    Spring+struts2+hibernate框架整合

    "Spring+Struts2+Hibernate框架整合"是Java Web开发领域中的一种常见架构模式,它结合了Spring的依赖注入、AOP(面向切面编程)、事务管理,Struts2的MVC(模型-视图-控制器)设计模式,以及Hibernate的对象关系映射...

    Spring + Struts +Hibernate简单学习源码

    Spring、Struts和Hibernate是Java开发中非常经典的三大框架,它们各自负责应用程序的不同层面:Spring作为全面的容器和依赖注入框架,负责管理对象和提供事务处理;Struts则是一个MVC(模型-视图-控制器)框架,主要...

    整合spring4+struts2+hibernate4项目源码

    `hibernate.cfg.xml`,用于配置Hibernate的数据源和实体映射。 2. Action类:继承自Struts2的ActionSupport,实现业务逻辑。 3. DAO层:使用Hibernate的Session接口进行数据库操作。 4. Service层:封装业务逻辑...

    MyEclipse7.5+flex4+spring3.0.5+struts2.2.1+hibernate3.6.0+blazeds4.0.0.14931完美整合方案

    本方案提供了一种集成化的开发环境,即"MyEclipse7.5+flex4+spring3.0.5+struts2.2.1+hibernate3.6.0+blazeds4.0.0.14931完美整合方案",它将多个流行的技术框架整合在一起,为Web应用程序开发提供了一个强大的平台...

    spring+struts2+hibernate完整项目初学者进阶练习

    【标题】"spring+struts2+hibernate完整项目初学者进阶练习"是一个针对Java初学者设计的进阶项目,旨在帮助他们熟练掌握三大主流Java Web开发框架——Spring、Struts2和Hibernate的集成应用。这个项目是基于MySQL...

    spring3+struts2+hibernate3+spring security3 权限管理

    (1)该项目是基于spring3+struts2+hibernate3+spring security3的权限管理项目 (2)后台我已经实现了权限管理,包括用户,角色和资源的分配。前台实现了spring security3的管理 (3)网上案例普遍是后台单一登陆。...

    spring+struts2+hibernate+json+dtree+mysql实现的无限级联树(全)

    3. 在Spring中配置数据源、事务管理器以及服务和DAO层。 4. 使用Struts2配置Action,处理请求并调用服务层方法。 5. 实现无限级联树的查询逻辑,可能使用Hibernate的Criteria API或HQL语言。 6. 将查询结果转化为...

    Struts2+Hibernate+Spring基于单表的增删改查code

    Struts2、Hibernate和Spring是Java Web开发中的三大框架,它们的组合,通常被称为SSH(Struts2+Spring+Hibernate)或SSH2。这个“Struts2+Hibernate+Spring基于单表的增删改查code”项目是一个典型的Java Web应用...

    Spring+Struts+Hibernate 实例源代码

    通过`说明.txt`文件,开发者可以了解到具体如何配置和运行这个集成示例,包括数据库连接设置、Struts配置、Spring Bean的定义以及Hibernate的实体映射等。 为了更好地理解和学习这个实例,你需要: 1. 阅读`说明....

    Spring+Struts2+Hibernate三大框架开发企业人力资源管理系统源代码

    本资源提供的"Spring+Struts2+Hibernate三大框架开发企业人力资源管理系统源代码"是实现这一目标的具体实例,帮助开发者了解如何将这三个强大的工具集成功能强大的应用。 Spring框架作为核心的依赖注入(DI)和面向...

    Spring+Struts2+Hibernate实现的OA项目

    【Spring+Struts2+Hibernate实现的OA项目】 在企业级应用开发中,Spring、Struts2和Hibernate是Java EE领域常用的三大框架,它们分别在不同的层面上解决了应用程序的复杂性。Spring作为全面的轻量级框架,提供了...

    struts1.2 + spring2.5 + hibernate3.2框架demo

    2. **配置文件**:struts-config.xml定义Struts的配置,spring-beans.xml管理Spring的bean,hibernate.cfg.xml配置Hibernate的数据库连接,可能还有实体类的映射文件(hbm.xml或使用注解)。 3. **JSP页面**:展示...

    struts+spring+hibernate三大框架整合

    Spring管理SessionFactory,通常在ApplicationContext.xml中配置,通过`&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"&gt;`指定数据源、映射文件、hibernate配置等。...

    spring+struts2+hibernate 整合例子

    2. 配置Spring:定义bean,包括数据源、SessionFactory、事务管理器以及应用中的业务类和服务类。 3. 配置Struts2:配置struts.xml,指定全局结果类型、拦截器栈,并声明Action类。 4. 配置Hibernate:创建实体类,...

Global site tag (gtag.js) - Google Analytics