Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心SQL语句,直接与对象打交道。 Spring提供了对Hibernate的SessionFactory的集成功能。

    1.建立Spring的bean.xml文件,在其里面配置 SessionFactory 以及事务,在hibernate.cfg.xml中不需要配置什么。  

      

<!-- 配置 Hibernate 的 SessionFactory 实例: 通过 Spring 提供的 LocalSessionFactoryBean 进行配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 配置数据源属性 -->
  <property name="dataSource" ref="dataSource"></property>
<!-- 配置 hibernate 配置文件的位置及名称 -->
<!-- 
  <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
-->
<!-- 使用 hibernateProperties 属相来配置 Hibernate 原生的属性 -->
  <property name="hibernateProperties">
    <props>
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
      <prop key="hibernate.show_sql">true</prop>
      <prop key="hibernate.format_sql">true</prop>
      <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
  </property>
<!-- 配置 hibernate 映射文件的位置及名称, 可以使用通配符 -->
  <property name="mappingLocations" 
    value="classpath:com/atguigu/spring/hibernate/entities/*.hbm.xml"></property>
</bean>

<!-- 配置 Spring 的声明式事务 -->
<!-- 1. 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 2. 配置事务属性, 需要事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
    <tx:method name="get*" read-only="true"/>
    <tx:method name="purchase" propagation="REQUIRES_NEW"/>
    <tx:method name="*"/>
  </tx:attributes>
</tx:advice>

<!-- 3. 配置事务切点, 并把切点和事务属性关联起来 -->
<aop:config>
  <aop:pointcut expression="execution(* com.localhost.spring.hibernate.service.*.*(..))" 
    id="txPointcut"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>

    2.建立实体类,以及*.hbm.xml文件。  

        

public class Account {

private Integer id;
private String username;
private int balance;
public Integer getId() {
  return id;
}
public void setId(Integer id) {
  this.id = id;
}
public String getUsername() {
  return username;
}
public void setUsername(String username) {
  this.username = username;
}
public Integer getBalance() {
  return balance;
}
public void setBalance(int balance) {
  this.balance = balance;
}
}

3.建立Dao接口以及实现类

public interface BookShopDao {

public int findBookpriceByIsbn(String isbn);

public void updateBookStock(String isbn);

public void updateUserAccount(String username, int price);

}

@Repository
public class BookShopDaoIml implements BookShopDao {

@Autowired
private SessionFactory sessionfactory;

private Session getSession(){
return sessionfactory.getCurrentSession();
}

@Override
public int findBookpriceByIsbn(String isbn) {
  String sql="select b.price from Book b where b.isbn = ?";
  Query query = getSession().createQuery(sql).setString(0, isbn);
  return (Integer) query.uniqueResult();

}

@Override
public void updateBookStock(String isbn) {

}

@Override
public void updateUserAccount(String username, int price) {
// TODO Auto-generated method stub

}

}

        4.测试test

private ApplicationContext ctx=null;
private BookShopDao bookshopDao;

{
ctx = new ClassPathXmlApplicationContext("Application.xml");
bookshopDao=ctx.getBean(BookShopDao.class);
}

接口类的方法
@Test
public void test() {
System.out.println(bookshopDao.findBookpriceByIsbn("1001"));
}

Spring 学习笔记之整合Hibernate的更多相关文章

  1. Spring学习笔记之整合hibernate

    1.web.xml里边要配置好对应的springxml的路径 <context-param> <param-name>contextConfigLocation</par ...

  2. Spring学习笔记四 整合SSH

    三大框架架构(整合原理) 步骤1:导包 Hibernate包 1.Hibernate包,hibernate/lib/required 2.hibernate/lib/jpa | java persis ...

  3. Spring学习笔记之整合struts

    1.现有项目是通过 <action    path="/aaaaAction"                type="org.springframework.w ...

  4. Spring MVC 学习笔记12 —— SpringMVC+Hibernate开发(1)依赖包搭建

    Spring MVC 学习笔记12 -- SpringMVC+Hibernate开发(1)依赖包搭建 用Hibernate帮助建立SpringMVC与数据库之间的联系,通过配置DAO层,Service ...

  5. Spring学习笔记(六)—— SSH整合

    一.整合原理 二.整合步骤 2.1 导包 [hibernate] hibernate/lib/required hibernate/lib/jpa 数据库驱动 [struts2] struts-bla ...

  6. Spring Boot 学习笔记(六) 整合 RESTful 参数传递

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...

  7. Java架构师之路 Spring学习笔记(一) Spring介绍

    前言 这是一篇原创的Spring学习笔记.主要记录我学习Spring4.0的过程.本人有四年的Java Web开发经验,最近在面试中遇到面试官总会问一些简单但我不会的Java问题,让我觉得有必要重新审 ...

  8. Spring学习笔记(一)

    Spring学习笔记(一) 这是一个沉淀的过程,大概第一次接触Spring是在去年的这个时候,当初在实训,初次接触Java web,直接学习SSM框架(当是Servlet都没有学),于是,养成了一个很 ...

  9. Spring学习笔记2——表单数据验证、文件上传

    在上一章节Spring学习笔记1——IOC: 尽量使用注解以及java代码中,已经搭建了项目的整体框架,介绍了IOC以及mybatis.第二节主要介绍SpringMVC中的表单数据验证以及文件上传. ...

随机推荐

  1. JavaScript初探系列之数组的基本操作

    在程序语言中数组的重要性不言而喻,JavaScript中数组也是最常使用的对象之一,数组是值的有序集合,由于弱类型的原因,JavaScript中数组十分灵活.强大,不像是Java等强类型高级语言数组只 ...

  2. 模拟Excel同一列相同值的单元格合并

    背景 项目中有一个查询工作量,可以将查询的结果导出到Excel表中.在Excel工具中,有一个合并居中功能,可以将选中的单元格合并成一个大的单元格.现在需要在程序中直接实现查询结果的汇总, 问题分析 ...

  3. hashMap原理(java8)

    (1) HashMap:它根据键的hashCode值存储数据,大多数情况下可以直接定位到它的值,因而具有很快的访问速度,但遍历顺序却是不确定的. HashMap最多只允许一条记录的键为null,允许多 ...

  4. HDU 1995 R-汉诺塔V

    https://vjudge.net/contest/67836#problem/R 用1,2,...,n表示n个盘子,称为1号盘,2号盘,....号数大盘子就大.经典的汉诺塔问 题经常作为一个递归的 ...

  5. Zabbix监控配置

    Zabbix在线文档 https://www.zabbix.com/documentation/4.0/zh/manual/config/hosts 1.我们启动服务后,我们看到了端口都正在监听,但是 ...

  6. Jenkins系列-Jenkins用户权限和角色配置

    由于jenkins默认的权限管理体系不支持用户组或角色的配置,因此需要安装第三发插件来支持角色的配置,这边将使用Role Strategy Plugin,介绍页面:https://wiki.jenki ...

  7. Kafka Strem

    Overview Concepts Topology Time States Window Hopping time windows Tumbling time windows Sliding win ...

  8. SIM卡是什么意思?你所不知道的SIM卡知识扫盲(详解)【转】

    原文链接:http://www.jb51.net/shouji/359262.html 日常我们使用手机,SIM卡是手机的必须,没有了它就不能接入网络运营商进行通信服务.SIM卡作为网络运营商对于我们 ...

  9. 能选择日期范围js控件

    html页面中使用日期控件是常有的事,好控件能使用开发变的快捷,下面是在开发过程中发现的几款日期控件,比较不错,收藏 1.基于bootstrap的jQuery日期范围选择插件 2.jQuery多功能日 ...

  10. bzoj1835[ZJOI2010]基站选址

    主席树+决策单调,重写一遍比之前短多了……题解:http://www.cnblogs.com/liu-runda/p/6051422.html #include<cstdio> #incl ...