Spring结合Hibernate的配置方法之一:直接引用hibernate文件
Spring结合Hibernate主要有集中配置方式,分别是
1、直接引用hibernate的*.cfg.xml配置文件
2、
3、
一、直接饮用hibernate的*.cfg.xml配置文件
项目的包结构如下:

此时Spring配置文件applicationContext.xml配置如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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 3 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 4 http://www.springframework.org/schema/context 5 http://www.springframework.org/schema/context/spring-context-2.5.xsd 6 http://www.springframework.org/schema/aop 7 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 8 http://www.springframework.org/schema/tx 9 http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 10 11 <!-- Spring整合Hibernate:直接饮用hibernate配置文件--> 12 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 13 <property name="configLocation" value="classpath:chanjiayi.cfg.xml"></property> 14 </bean> 15 16 <!-- 使用HibernateTemplate必须将sessionFactory注入DAO类中 --> 17 <bean id="userDAO" class="com.chanjiayi.dao.impl.UserDAOImpl"> 18 <property name="sessionFactory" ref="sessionFactory"/> 19 </bean> 20 21 <bean id="userService" class="com.chanjiayi.service.impl.UserServiceImpl"> 22 <property name="dao" ref="userDAO"/> 23 </bean> 24 25 </beans>
hibenate的配置文件chanjiayi.cfg.xml配置:
1 <?xml version='1.0' encoding='UTF-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 6 <!-- Generated by MyEclipse Hibernate Tools. --> 7 <hibernate-configuration> 8 9 <session-factory> 10 <!-- 配置JDBC连接属性 --> 11 <property name="myeclipse.connection.profile"> 12 com.mysql.jdbc.Driver 13 </property> 14 <property name="connection.url"> 15 jdbc:mysql://localhost:3306/chanjiayi 16 </property> 17 <property name="connection.username">root</property> 18 <property name="connection.password">sa</property> 19 <property name="connection.driver_class"> 20 com.mysql.jdbc.Driver 21 </property> 22 <property name="dialect"> 23 org.hibernate.dialect.MySQLDialect 24 </property> 25 26 <!-- 配置C3P0连接池及连接池属性 --> 27 <property name="hibernate.connection.provider_class"> 28 org.hibernate.connection.C3P0ConnectionProvider 29 </property> 30 <property name="hibernate.c3p0.timeout">120</property> 31 <!--连接池中保留的最小连接数。--> 32 <property name="minPoolSize">5</property> 33 <!--连接池中保留的最大连接数。Default: 15 --> 34 <property name="maxPoolSize">15</property> 35 <!-- 最大连接数 --> 36 <property name="hibernate.c3p0.max_size">10</property> 37 <!-- 最小连接数 --> 38 <property name="hibernate.c3p0.min_size">5</property> 39 40 <!-- 自动建表 --> 41 <property name="hbm2ddl.auto">update</property> 42 43 <property name="show_sql">true</property> 44 45 <property name="connection.autocommit">true</property> 46 <mapping class="com.chanjiayi.pojo.Order" /> 47 <mapping class="com.chanjiayi.pojo.Product" /> 48 <mapping class="com.chanjiayi.pojo.User" /> 49 <mapping class="com.chanjiayi.pojo.UserInfo" /> 50 51 </session-factory> 53 </hibernate-configuration>
使用了C3P0连接池,我使用的Jar包是:c3p0-0.9.0.2.jar。点击下载。

浙公网安备 33010602011771号