`
ynp
  • 浏览: 428575 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring学习笔记------Spring和Hibernate的整合配置

阅读更多
Spring和Hibernate整合配置有两种方式:
   方式一:保留hibernate.cfg.xml,整合Spring
   Spring配置文件--->
<?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:aop="http://www.springframework.org/schema/aop"
		xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<!-- 数据源配置 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql:///test"/>
		<property name="username" value="root"/>
		<property name="password" value="root"/>
		<!-- 连接池启动时的初始值 -->
		<property name="initialSize" value="1"/>
		<property name="maxActive" value="200"/>
	    <property name="maxIdle" value="5"/>
	    <property name="minIdle" value="2"/>
	</bean>
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<!-- Hibernate配置信息 -->
		<property name="configLocation">
 			<value>classpath:hibernate.cfg.xml</value>
 		</property>
	</bean>
	<!-- 配置事务信息 -->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	<!-- 采用注解方式进行事务配置 -->
	<tx:annotation-driven transaction-manager="txManager"/>
	<!-- 采用注解注入对象 -->
	<context:annotation-config/>
	<bean id="personService" class="com.xx.service.impl.PersonServiceBean"></bean>
</beans>

   hibernate配置文件--->
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.url">jdbc:mysql:///test</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		<mapping resource="com/xx/dao/Person.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

   方式二: 把Hibernate的配置整合到Spring的配置文件中
   Spring配置文件--->
<?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:aop="http://www.springframework.org/schema/aop"
		xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<!-- 数据源配置 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql:///test"/>
		<property name="username" value="root"/>
		<property name="password" value="root"/>
		<!-- 连接池启动时的初始值 -->
		<property name="initialSize" value="1"/>
		<property name="maxActive" value="200"/>
	    <property name="maxIdle" value="5"/>
	    <property name="minIdle" value="2"/>
	</bean>
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<!-- hbm.xml文件 -->
		<property name="mappingResources">
			<list>
				<value>com/xx/dao/Person.hbm.xml</value>
			</list>
		</property>
		<!-- Hibernate配置信息 -->
	    <property name="hibernateProperties">
		    <value>
		        hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
		        hibernate.hbm2ddl.auto=update
		        hibernate.show_sql=true
		        hibernate.format_sql=false
		      </value>
        </property>
	</bean>
	<!-- 配置事务信息 -->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	<!-- 采用注解方式进行事务配置 -->
	<tx:annotation-driven transaction-manager="txManager"/>
	<!-- 采用注解注入对象 -->
	<context:annotation-config/>
	<bean id="personService" class="com.xx.service.impl.PersonServiceBean"></bean>
</beans>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics