当前位置: 首页 > 编程日记 > 正文

Spring整合Hibernate的步骤

为什么要整合Hibernate?
1、使用Spring的IOC功能管理SessionFactory对象
 LocalSessionFactoryBean
2、使用Spring管理Session对象
 HibernateTemplate
3、使用Spring的功能实现声明式的事务管理

整合Hibernate的步骤:
1、配置SessionFactory(能够自己主动完毕)
 <bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation"
   value="classpath:hibernate.cfg.xml">
  </property>
 </bean>

2、配置HibernateTemplate,用于完毕数据操作
 <bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>

3、让Common继承HibernateDaoSupport类,该类提供了HibernateTemplate的getter和setter方法。

4、将hibernateTemplete注入到Common中
 <bean id="common" class="com.aptech.common.Common">
  <property name="hibernateTemplate" ref="hibernateTemplete"></property>
 </bean>

5、将Common的方法改动成hibernateTemplete的操作。
package com.aptech.common;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.aptech.exception.CommonException;
import com.aptech.htm.HibernateSessionFactory;
/**
 * 通用类,不再负责事务处理
 * 目标对象
 * @author 李赞红
 *
 * @param <POJO>
 */
public class Common<POJO> extends HibernateDaoSupport implements ICommon<POJO> {
 public void insertObject(POJO pojo) throws CommonException {
  try {
   this.getHibernateTemplate().save(pojo);
  } catch (DataAccessException e) {
   e.printStackTrace();
   throw new CommonException(e);
  }
 }

public void updateObject(POJO pojo) throws CommonException {
  try {
   this.getHibernateTemplate().update(pojo);
  } catch (DataAccessException e) {
   e.printStackTrace();
   throw new CommonException(e);
  }
 }

public void deleteObject(Class theClass, long id) throws CommonException {
  try {
   Object obj = this.getHibernateTemplate().load(theClass, id);
   this.getHibernateTemplate().delete(obj);
  } catch (DataAccessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   throw new CommonException(e);
  }
 }

public POJO loadObject(Class theClass, long id) throws CommonException {
  try {
   Object obj = this.getHibernateTemplate().load(theClass, id);
   return (POJO) obj;
  } catch (DataAccessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   throw new CommonException(e);
  }
 }

public List queryObjects(final String hql) throws CommonException {
  class Hc implements HibernateCallback{
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
    return session.createQuery(hql).list();
   }
  }
  
  try {
   return this.getHibernateTemplate().executeFind(new Hc());
  } catch (DataAccessException e) {
   e.printStackTrace();
   throw new CommonException(e);
  }
 }

public List queryObjects(Class theClazz) throws CommonException {
  return this.queryObjects("from " + theClazz.getSimpleName());
 }
}


6、配置事务
<!-- 事务管理器,相当于TransactionProxy,定义事务的开启、提交、回滚 -->
 <bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref bean="sessionFactory"/>
  </property>
 </bean>
 <bean id="ti" class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager">
   <ref bean="htm"/>
  </property>
  <property name="transactionAttributes">
   <props>
    <!-- key:方法名称 -->
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>
 
 <!-- 自己主动代理,代理业务对象 -->
 <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="beanNames">
   <list>
    <value>common</value>
   </list>
  </property>
  <property name="interceptorNames">
   <list>
    <value>ti</value>
   </list>
  </property>
 </bean>

7、将common注入Dao
 <bean id="baseDao" abstract="true">
  <property name="common">
   <ref bean="common"/>
  </property>
 </bean>
 
 <bean id="udao" class="com.aptech.dao.impl.UserDao" parent="baseDao"></bean>
 <bean id="rdao" class="com.aptech.dao.impl.RoleDao" parent="baseDao"></bean>


8、将Dao注入Service
 <bean id="grantService" class="com.aptech.service.impl.GrantService">
  <property name="rdao" ref="rdao"></property>
  <property name="udao" ref="udao"></property>
 </bean>

转载于:https://www.cnblogs.com/gcczhongduan/p/4305516.html

相关文章:

初创企业购买企业邮箱_支持#NetNeutrality =支持设计师及其创建的初创企业

初创企业购买企业邮箱by Lukasz Lysakowski卢卡斯吕萨科夫斯基(Lukasz Lysakowski) 支持#NetNeutrality 支持设计师及其创建的初创企业 (Supporting #NetNeutrality Supporting Designers and the Startups They Create) I believe in Net Neutrality, and I wrote a brief e…

【转】Android source build/envsetup.sh学习笔记

原文网址&#xff1a;http://blog.csdn.net/mliubing2532/article/details/7567164 如果你只需要修改某一个模块的内容&#xff0c;但是却每次都要执行make, 最后等待很长时间。使用模块编译&#xff0c;那只需要在你所在的模块的目录或者其子目录&#xff0c;执行mm&#xff0…

微信小程序之上传附件

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 目前微信API开放上传图片&#xff0c;录音文件&#xff0c;视频文件&#xff0c;but 只能下载并打开附件&#xff0c;不能上传附件&#xff0c;以后会不会开放附件上传目前还不确定&…

微软在.NET官网上线.NET 架构指南频道

微软在Visual Studio 2017 正式发布的时候也上线了一个参考应用https://github.com/dotnet/eShopOnContainers , 最近微软给这个参考应用写了完善的文档&#xff0c;放在.NET官网的.NET架构频道https://www.microsoft.com/net/architecture。 整个.NET 架构按照4个部分展开&…

初学者css常见问题_5分钟内学习CSS Grid-初学者教程

初学者css常见问题Grid layouts are fundamental to the design of websites, and the CSS Grid module is the most powerful and easiest tool for creating it. I personally think it’s a lot better than for example Bootstrap (read why here).网格布局是网站设计的基础…

HBase保存的各个字段意义解释

// Author&#xff1a;xxx0624 HomePage&#xff1a;http://www.cnblogs.com/xxx0624/ // nutch2.2.1集成HBase0.94.25, 可以查询nutch的conf文件中的gora-hbase-mapping.xml查看原文件 <gora-orm><table name"webpage"><family name"p" ma…

AJAX基础篇

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 正文&#xff1a; 整理一下AJAX相关的知识&#xff0c;帮助自己复习的同时希望还能够帮助到新加入前端阵营的小伙伴们。 1.AJAX是什么&#xff1f; AJAX 异步的JavaScript和XML 2.AJAX的作用&…

在运筹学中什么样的解决方案是最优的

问题: 1.在运筹学中什么样的解决方案是最优的 2.线性代数解决的是什么问题? 3.运筹学与线性代数的关系? 4.如何使用matlab 解决运筹学中的问题? 转载于:https://www.cnblogs.com/lwy1103/p/6676373.html

npm构建脚本_NPM脚本简介

npm构建脚本by Mohammed Ajmal Siddiqui由Mohammed Ajmal Siddiqui NPM脚本简介 (Introduction to NPM Scripts) NPM scripts are among my favorite features of NPM. They are simple. They reduce the need for tools. Hence they reduce the number of configuration file…

SQL 中循环、for循环、游标

我们使用SQL语句处理数据时&#xff0c;可能会碰到一些需要循环遍历某个表并对其进行相应的操作&#xff08;添加、修改、删除&#xff09;&#xff0c;这时我们就需要用到咱们在编程中常常用的for或foreach&#xff0c;但是在SQL中写循环往往显得那么吃力&#xff0c;翻遍网上…

Unix Linux大学教程(三):过滤器、正则表达式、vi

第16章 过滤器&#xff1a;简介和基本操作 删除数据列用colrm&#xff1a;colrm [startcol [endcol]] 如果没有endcol则删除从startcol至行末尾所有的列。 第17章 过滤器&#xff1a;比较和抽取 比较任意两个文件&#xff1a;cmp file1 file2 显示不同字节数及所在行。 比…

微信小程序获取多选框选中值和选中值对应的id

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 官方文档中只有获取多选框的值的方法&#xff0c;但是我需要获取选中的值同时还要获取选中值对应的id&#xff0c;但是又不能操作DOM获取&#xff0c;相信和我有同样需求的伙伴们都会为此发愁&#xff0…

调试代码遗留_陷入遗留代码地狱吗? 这里有一些想法可以帮助您处理情况

调试代码遗留by Felipe Lopes通过Felipe Lopes 陷入遗留代码地狱吗&#xff1f; 这里有一些想法可以帮助您处理情况 (Stuck in legacy code hell? Here are some few thoughts to help you manage the situation) I’m gonna tell you a little story about how I ended up i…

求二维数组最大子数组

结对队友&#xff1a;胡康臻、杨寒寒 1、设计思想&#xff1a; 首先定义产生二维数组&#xff0c;定义可输入二维数组行和列&#xff0c;各位数随机产生&#xff1b; 然后进行最大子数组的求和比较&#xff0c;从每行的第一个数为子数组的起点开始进行不同的子数组遍历比较&…

UVa 11021 (概率 递推) Tribles

Tribble是麻球&#xff1f; 因为事件都是互相独立的&#xff0c;所以只考虑一只麻球。 设f(i)表示一只麻球i天后它以及后代全部死亡的概率&#xff0c;根据全概率公式&#xff1a; f(i) P0 P1 * f(i-1) P2 * f(i-1)2 ... Pn * f(n)n 每个麻球死亡是独立的&#xff0c;所以…

积分页面布局记录

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 、 // pages/user/my_integral/record/record.js var app getApp(); var util require("../../../utils/util.js") Page({data: {url_data: ,username: ,usertop: ,tab: 1…

uber_Uber是如何制成的

uberby Dmytro Brovkin由Dmytro Brovkin Uber是如何制成的 (How Uber was made) Uber has transformed the world. Indeed, its inconceivable to think of a world without the convenience of the innovative ride sharing service. Tracing its origins in a market which …

HTML 标签包含规范,规避脱标流,图片和文字垂直居中对齐,

&#xff11; 标签包含规范 ◆div可以包含所有的标签。 ◆p标签不能包含div h1等标签。 ◆h1可以包含p&#xff0c;div等标签。 ◆行内元素尽量包含行内元素&#xff0c;行内元素不要包含块元素。 2 规避脱标流 ◆尽量使用标准流。 ◆标准流解决不了的使用浮动。 ◆浮动解决不了…

Java Socket发送与接收HTTP消息简单实现

在上次Java Socket现实简单的HTTP服务我 们实现了简单的HTTP服务&#xff0c;它可以用来模拟HTTP服务&#xff0c;用它可以截获HTTP请求的原始码流&#xff0c;让我们很清楚的了解到我们向服务发的HTTP消息的结 构&#xff0c;对HTTP请求消息有个清晰的认识。这一节我想写了一个…

微信小程序日期相减得出天数

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; // 日期结算 num_data: function (e) {var start_date new Date(this.data.start_date.replace(/-/g, "/"));var end_date new Date(this.data.end_date.replace(/-/g, &q…

es6 ... 添加属性_如何在10分钟内免费将HTTPS添加到您的网站,以及为什么您现在不止需要这样做......

es6 ... 添加属性by Ayo Isaiah通过Ayo Isaiah 如何在10分钟内免费将HTTPS添加到您的网站&#xff0c;以及为什么现在比以往更需要这样做 (How to add HTTPS to your website for free in 10 minutes, and why you need to do this now more than ever) Last week, Google ann…

程序员跳槽全攻略——读书笔记

有同学说&#xff0c;我技术很好啊&#xff0c;又会机器学习又会编译原理&#xff0c;凭什么那些写Javascript的薪水比我高一倍&#xff1f; 谁让你在一家建站公司上班呢。对一家做网站的公司而言&#xff0c;机器学习和编译原理是不能为它带来收益的&#xff0c;而Javascript写…

[转] Gradle: 此时不应有 Androidandroid-studiosdk oolslib\find_java.exe。解决方法

上述问题主要是java路径的问题&#xff0c;这里主要给出解决方案&#xff0c;至于为什么这么解决的&#xff0c;大家可以学学bat语言。想问的可以留言我。 dx.bat 根据安装目录&#xff0c;我的是D:\Program Files (x86)\Android\android-studio\sdk\build-tools\android-4.2.2…

微信小程序和微信小程序之间的跳转和传参示例代码附讲解

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 一&#xff1a;微信小程序跳转 使用限制 需要用户触发跳转 从 2.3.0 版本开始&#xff0c;若用户未点击小程序页面任意位置&#xff0c;则开发者将无法调用此接口自动跳转至其他小…

电子界卡组构建2019_2018–2019年构建现代Android应用程序的路线图

电子界卡组构建2019Kriptofolio应用程序系列—简介 (Kriptofolio app series — Introduction) Welcome to this series of blog posts where I will be creating a modern Android app. I will use the best tools and practices available in the year 2018–2019. I am doin…

python操作mysql数据库实现增删改查

Python 标准数据库接口为 Python DB-API&#xff0c;Python DB-API为开发人员提供了数据库应用编程接口。 Python 数据库接口支持非常多的数据库&#xff0c;你可以选择适合你项目的数据库&#xff1a; GadFlymSQLMySQLPostgreSQLMicrosoft SQL Server 2000InformixInterbaseOr…

弹性布局,自动按比例居中

1. 让行类盒子及盒子的元素 自动按比例居中效果图 html <view classaaa><view classbbb>aaaaaaaaa</view><view classbbb>aaaaaaaaa</view><view classbbb>bb</view><view classbbb>aaaaaaaaa</view> </view> c…

Ubuntu 14.04系统托盘图标问题,skype托盘图标显示

Ubuntu 14.04系统托盘图标问题&#xff0c;skype托盘图标显示_瑞波支付_新浪博客 Ubuntu 14.04 取消了系统托盘白名单机制&#xff0c;导致使用传统系统托盘技术的程序无法显示出托盘图标,dconf-editor也无力解决这个问题 。Ubuntu Unity桌面目前使用的技术是indicator-applica…

以太坊去中心化_开发以太坊去中心化投票应用程序的指南

以太坊去中心化by Timothy Ko蒂莫西高(Timothy Ko) 开发以太坊去中心化投票应用程序的指南 (A guide to developing an Ethereum decentralized voting application) After the entire cryptocurrency market passed 700 billion dollars in market cap, the cryptocurrency s…

Intellij IDEA的下载和使用(针对学生的免费使用计划)

一、下载和使用授权&#xff08;针对学生&#xff09; 1、下载 可以在Intellij IDEA官网上下载需要的版本。下载地址&#xff1a;https://www.jetbrains.com/idea/ 2、学生免费试用 首先&#xff0c;你得现有你们学校的官方邮箱账户&#xff0c;例如XXXYYY.edu.cn 其次&#xf…