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

 

原文地址:https://www.cnblogs.com/tandy/p/5125455.html