`

Spring整合Hibernate之HibernateTemplate

阅读更多

要得到HibernateTemplate就得先获得SessionFactory或者DataSource,前面已经介绍了SessionFactory和DataSource的获得。

 

beans.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/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<context:annotation-config />
	<context:component-scan base-package="com.lbx" />
	<tx:annotation-driven transaction-manager="txManager" />

	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>
	
	<bean id="dataSource" destroy-method="close"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		
		<!-- 
		<property name="annotatedClasses">
			<list>
				<value>com.lbx.model.User</value>
				<value>com.lbx.model.Log</value>
			</list>
		</property>
		 -->
		 <property name="packagesToScan">
			<list>
				<value>com.lbx.model</value>
			</list>
		</property>
		
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="add*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>


	<aop:config>
		<aop:pointcut id="fooServiceOperation"
			expression="execution(public * com.lbx.service..*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation" />
	</aop:config>


	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<bean id="logDAO" class="com.lbx.dao.impl.LogDAOImpl">
		<property name="hibernateTemplate" ref="hibernateTemplate"></property>
	</bean>
	<bean id="u" class="com.lbx.dao.impl.UserDAOImpl">
		<property name="hibernateTemplate" ref="hibernateTemplate"></property>
	</bean>
	<bean id="userService" class="com.lbx.service.UserService">
		<property name="userDAO" ref="u"></property>
		<property name="logDAO" ref="logDAO"></property>
	</bean>

</beans>

 

 

 

这样得到HibernateTemplate之后就可以在dao层通过HibernateTemplate来增删改查了,如下代码:

package com.lbx.dao.impl;


import org.springframework.orm.hibernate3.HibernateTemplate;

import com.lbx.dao.LogDAO;
import com.lbx.model.Log;

public class LogDAOImpl implements LogDAO {

	private HibernateTemplate hibernateTemplate;
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void save(Log log) {

		hibernateTemplate.save(log);
		//用于测试回滚,默认的时候是RuntimeException
		//throw new RuntimeException("运行的时候出错了");
	}

}

 

 

分享到:
评论

相关推荐

    Spring整合hibernate(2)之基于HibernateTemplate的整合

    Spring整合hibernate(2)之基于HibernateTemplate的整合,jar需要自己添加!

    Spring+hibernate整合源代码

    Spring+hibernate整合源代码 结束Sping+hibernate 的使用方法 包括演示示例

    Spring整合Hibernate 详解.doc

    6.5 Spring整合Hibernate 6.6 Spring提供的DAO支持 6.5.2 管理Hibernate的SessionFactory 6.5.3 使用HibernateTemplate 6.5.4 使用HibernateCallBack 6.5.6 使用IoC容器组装各种组件 6.5.7启动web容器读取xml配置...

    第24次课-1 Spring与Hibernate的整合

    Spring提供了org.springframework.orm.hibernate3.HibernateTemplate类和org.springframework.orm.hibernate3.HibernateCallback接口来方便和Hibernate整合。 HibernateTemplate类封装了Hibernate的主要类,它提供了...

    Spring4整合Hibernate5详细步骤

    本篇文章主要介绍了Spring4整合Hibernate5详细步骤,具有一定的参考价值,有兴趣的同学可以了解一下

    spring和hibernate整合示例

    spring和hibernate整合示例,以一张表的业务操作,来简要说明hibernate和spring的整合,说明spring对HibernateTemplate和调用

    Spring 整合 Hibernate 时启用二级缓存实例详解

    Spring 整合 Hibernate 时启用二级缓存实例详解 写在前面:  1. 本例使用 Hibernate3 + Spring3;  2. 本例的查询使用了 HibernateTemplate; 1. 导入 ehcache-x.x.x.jar 包; 2. 在 applicationContext.xml ...

    AnyFo - Util - AnyFoDao :Spring + Hibernate整合通用的DAO层类

    本类封装了Spring提供的HibernateTemplate,从提供了对数据的各种操作,因此,本类尤其适合用Spring + Hibernate整合后进行系统开 发时使用。 AnyFoAction功能概述 AnyFoDao中的那个类,提供多个方法来对...

    spring2.5.6与hibernate3.3整合示例

    spring2.5.6与hibernate3.3整合示例,导入即可运行。运用注解、hibernateTemplate、声明式事务管理。包括所有需要jar包。

    spring集成Hibernate

    本文主要讲解如何在Spring4.0.4下整合Hibernate4.3.6; 主要介绍了如下内容: 项目结构的规划; Spring MVC的配置和使用; Spring下整合Hibernate的具体过程; 数据源的配置; jdbcTemplate和HibernateTemplate两种...

    struts1.2+spring2.0+hibernate3.2 整合源码

    1、struts1.2+spring 2.0+hibernate3.2 2、struts的动作交由spring来管理,hibernate的配置集中在spring中配置。 3、增加了声明式事务处理,加强了hibernateTemplate的简单事务处理。 4、完整的Myeclipse的工程文件...

    Spring相关测试4

    主要对Spring 整合Hibernate (基于HibernateTemplate、HibernateDaoSupport)的不同版本做了测试

    Spring的学习笔记

    三、 Spring整合hibernate3事务 31 (一) Annotation注解方式配置事务管理 31 (二) Spring事务选项 35 (三) XML文件形式配置Spring事务管理 37 四、 HibernateTemplate 38 (一) HibernateTemplate 38 (二) ...

    struts2.3.x+spring3.1.x+hibernate3.6 demo

    十分抱歉,上次整合的是一个半成品,spring3.1和hibernate4.1目前为止我测试了,整合过程中有很多问题!关键问题有几个,第一个HibernateDaoSupport这个没有了,在使用hibernateTemplate的时候,报错误:java.lang....

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    application.xml配置 ... &lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt; &lt;property name="sessionFactory" ref="sessionFactory"&gt;&lt;/property&gt; &lt;/beans&gt;

    spring2.5 学习笔记

    三、 Spring整合hibernate3事务 31 (一) Annotation注解方式配置事务管理 31 (二) Spring事务选项 35 (三) XML文件形式配置Spring事务管理 37 四、 HibernateTemplate 38 (一) HibernateTemplate 38 (二) ...

    SpringMVC+Hibernate全注解整合

    &lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt; class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;!-- Spring AOP ...

    SSH整合中 hibernate托管给Spring得到SessionFactory

    Spring文件中的 SessionFactory中 加入为了能得到同一个Session

Global site tag (gtag.js) - Google Analytics