Spring 学习笔记之整合Hibernate

    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"));
}

时间: 2024-08-02 06:59:19

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

Spring学习笔记之整合hibernate

1.web.xml里边要配置好对应的springxml的路径 <context-param> <param-name>contextConfigLocation</param-name> <param-value> conf/kernel/spring_kernel/spring-*.xml, conf/business/spring_business/spring-*.xml, conf/custom/spring_custom/spring-*.xml

Sping学习笔记_整合hibernate

1,Spring 指定datasource a)     参考文档,找dbcp.BasicDataSource(3种方式) c3p0        dbcp ( 数据库连接池)     proxool b)     在DAO或者Service中注入dataSource @Component("u") public class UserDAOImpl implements UserDAO { private DataSource dataSource; public DataSource

Spring学习笔记之整合struts

1.现有项目是通过 <action    path="/aaaaAction"                type="org.springframework.web.struts.DelegatingActionProxy"                name="plDraftPrLineForm"                scope="request"                parameter=&

《Spring学习笔记》:Spring、Hibernate、struts2的整合(以例子来慢慢讲解,篇幅较长)

<Spring学习笔记>:Spring.Hibernate.struts2的整合(以例子来慢慢讲解,篇幅较长) 最近在看马士兵老师的关于Spring方面的视频,讲解的挺好的,到了Spring.Hibernate.struts2整合这里,由于是以例子的形式来对Spring+Hibernate+struts2这3大框架进行整合,因此,自己还跟着写代码的过程中,发现还是遇到了很多问题,因此,就记录下. 特此说明:本篇博文完全参考于马士兵老师的<Spring视频教程>. 本篇博文均以如下这

Spring学习笔记(一)

Spring学习笔记(一) Spring核心思想: IOC:  Inversion Of Control (控制反转) / DI: Dependency Injection (依赖注入) AOP: Aspect Oriented Programming (面向切面编程) IOC 1. 简单的应用 Model package com.wangj.spring.model; public class User { private String username; private String pas

不错的Spring学习笔记(转)

Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是spring.jar和commons-logging.jar.此外为了便于测试加入了JUnit包.   在Myeclipse中创建Java项目.   编写一个接口类,为了简单,只加入了一个方法.   Java代码   1.package com.szy.spring.interfacebean;  

[原创]java WEB学习笔记95:Hibernate 目录

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 -----------------------------------------------------------------------------------------------------------------

spring学习笔记(19)mysql读写分离后端AOP控制实例

在这里,我们接上一篇文章,利用JNDI访问应用服务器配置的两个数据源来模拟同时操作不同的数据库如同时操作mysql和oracle等.实际上,上个例子可能用来模拟mysql数据库主从配置读写分离更贴切些.既然如此,在本例中,我们就完成读写分离的模拟在web端的配置实例. 续上次的例子,关于JNDI数据源的配置和spring datasource的配置这里不再重复.下面着重加入AOP实现DAO层动态分库调用.可先看上篇文章<spring学习笔记(18)使用JNDI模拟访问应用服务器多数据源实例 >

Spring学习笔记(三)

Spring学习笔记(三) AOP 一.使用Annotation方式实现AOP.步骤: xml里加入配置:<aop:aspectj-autoproxy /> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org