总结:Spring2.5.6 与 Hibernate3.x整合(connpool)

环境:JDK1.6+Spring2.5.6+hibernate3

 

IDE:MyEclipse6

DB:SQLSERVER2000

具体lib如下:

 

c3p0-0.9.1.2.jar

jtds-1.2.5.jar

commons-logging.jar

log4j-1.2.15.jar

spring.jar

hibernate3.jar

dom4j-1.6.1.jar

jaxen-1.1-beta-7.jar

slf4j-api-1.5.0.jar

slf4j-log4j12-1.5.0.jar

jta.jar

commons-collections.jar

antlr-2.7.6.jar

以下为其配置文件:

===========hibernate-daos.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"

xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 

<bean id="productDao"

class="com.me.dao.hibernate.HibernateProductDao">

<property name="sessionFactory" ref="mySessionFactory" />

</bean>

 

<bean

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<value>classpath:conn/jdbc.properties</value>

</property>

</bean>

<bean id="mySessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="mappingResources">

<list>

<value>product.hbm.xml</value>

</list>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.HSQLDialect

</prop>

<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.generate_statistics">true</prop>

</props>

 

 

</property>

</bean>

<!--连接池的配置-->

<bean id="dataSource"

class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">

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

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

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

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

 

<property name="minPoolSize" value="1" />

<property name="initialPoolSize" value="2" />

<property name="maxPoolSize" value="20" />

 

<property name="maxIdleTime" value="1800" />

<property name="acquireIncrement" value="2" />

<property name="maxStatements" value="3" />

 

<property name="idleConnectionTestPeriod" value="1800" />

<property name="acquireRetryAttempts" value="3" />

</bean>

</beans>

=============conn/jdbc.properties============

 

 

jdbc.driverClassName=net.sourceforge.jtds.jdbc.Driver

jdbc.url=jdbc:jtds:sqlserver://localhost:1433/jpetstore

jdbc.username=sa

jdbc.password=sa

==========service.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"

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">

<import resource="hibernate-daos.xml" />

<bean id="productService" class="com.me.service.ProductService">

<property name="productDao">

<ref bean="productDao" />

</property>

</bean>

....

当然还有product.hbm.xml,如下:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

<!--

- Mapping file for the Hibernate implementation of the Clinic interface.

-->

<hibernate-mapping auto-import="true" default-lazy="false">

 

<class name="com.me.domain.Product" table="product">

<id name="productId" column="productId">

<generator class="assigned" />

</id>

<property name="name" column="name" />

<property name="category" column="category" />

<property name="description" column="descn" />

</class>

 

</hibernate-mapping>

====  Product 文件如下:=======

 

public class Product implements Serializable {

 

  /* Private Fields */

 

  private String productId;

  private String categoryId;

  private String name;

  private String description;

  private String category;

....setter/getter方法

=================测试如下=========

 

public class HibernateDBTest {

 

/**

* @param args

*/

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext(

new String[] { "services.xml", "hibernate-daos.xml" });

ProductService productService = (ProductService) context

.getBean("productService");

String productId="AV-CB-01";

Product product=productService.getProductByID(productId);

System.out.println(product);

}

 

}

测试结果OK。

 

 

你可能感兴趣的:(hibernate3)