spring+hibernate

spring为主,持久层使用hibernate

  • Spring hibernate 事务的流程
  • 1. 在方法开始之前
  • ①. 获取 Session
  • ②. 把 Session 和当前线程绑定, 这样就可以在 Dao 中使用 SessionFactory 的
  • getCurrentSession() 方法来获取 Session 了
  • ③. 开启事务
  • - 2. 若方法正常结束, 即没有出现异常, 则
  • ①. 提交事务
  • ②. 使和当前线程绑定的 Session 解除绑定
  • ③. 关闭 Session
  • - 3. 若方法出现异常, 则:
  • ①. 回滚事务
  • ②. 使和当前线程绑定的 Session 解除绑定
  • ③. 关闭 Session
  • 所以一定要配置事务,才能使用getCurrentSession() 方法来获取 Session,才能保证session的线程安全。否则报错

项目结构:

daoimpl:

package com.cwh.spring.hibernate.dao.impl;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.cwh.spring.hibernate.dao.BookShopDao;
import com.cwh.spring.hibernate.exception.BookStockException;
import com.cwh.spring.hibernate.exception.UserAccountException;

[@Repository](https://my.oschina.net/u/3055569)
public class BookShopDaoImpl implements BookShopDao {
	
	/**
	 * 此处是dao层,有并发的问题,所以不能用sessionfactory的opensession方法,
	 * 要用getCurrentSession()获取跟当前线程绑定的session
	 */
	@Autowired
	private SessionFactory factory; 
	
	/**
	 * 1.import org.springframework.orm.hibernate3.HibernateTemplate;
	 * 由此可见,这个类属于spring 的 api,如果使用这个,就需要有spring 环境,
	 * 不能在原生的hibernate 环境使用,可移植性比较差。而且hibernate3,没有4优秀
	 * 所以不推荐使用
	 * 2.也不推荐继承hibernateDaoSupport,理由同上。较少Dao 和spring 的api 耦合
	 */
	private HibernateTemplate hibernateTemplate;
	
	
	private Session getSession(){
		return factory.getCurrentSession();
	}

	[@Override](https://my.oschina.net/u/1162528)
	public int findBookPriceByIsbn(String isbn) {
		String hql = "select b.price from Book b where b.isbn = ?";
		Query query = this.getSession().createQuery(hql).setString(0, isbn);
		return (int) query.uniqueResult();
	}

	[@Override](https://my.oschina.net/u/1162528)
	public void updateBookStock(String isbn) {
		//校验库存
		String hql0 = "select b.stock from Book b where b.isbn = ?";
		int stock = (int) this.getSession().createQuery(hql0).setString(0, isbn).uniqueResult();
		if(stock == 0){
			throw new BookStockException("库存不足 ");
		}
		
		String hql = "update Book b set b.stock = b.stock - 1 where b.isbn = ?";
		this.getSession().createQuery(hql).setString(0, isbn).executeUpdate();
	}

	[@Override](https://my.oschina.net/u/1162528)
	public void updateUserAccount(String username, int price) {
		// 验证余额
		String hql0 = "select a.balance from Account a where a.userName = ?";
		int balance = (int) this.getSession().createQuery(hql0).setString(0, username).uniqueResult();
		if(balance < price){
			throw new UserAccountException("余额不足 ");
		}
		
		String hql = "update Account a set a.balance = a.balance - ? where a.userName = ?";
		this.getSession().createQuery(hql).setInteger(0, price).setString(1, username).executeUpdate();
	}

}

seviceImpl:

package com.cwh.spring.hibernate.service.impl;

import org.hibernate.mapping.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cwh.spring.hibernate.dao.BookShopDao;
import com.cwh.spring.hibernate.service.BookShopService;

@Service("bookShopService")
public class BookShopServiceImpl implements BookShopService {

	@Autowired
	private BookShopDao bookShopDao;
	
	@Override
	public void purchase(String username, String isbn) {
		int price = bookShopDao.findBookPriceByIsbn(isbn);
		bookShopDao.updateBookStock(isbn);
		bookShopDao.updateUserAccount(username, price);
	}
}

applicationContext.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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <context:component-scan base-package="com.cwh.spring.hibernate"></context:component-scan>

    <!-- 引用外部资源文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- c3p0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.userName}"></property>
        <property name="password" value="${jdbc.passWord}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

        <property name="maxPoolSize" value="${maxPoolSize}"></property>
        <property name="initialPoolSize" value="${initialPoolSize}"></property>    
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 配置数据源属性 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 配置hibernate配置文件 -->
        <!-- 其实配置文件可以省略,不过需要在此处配置hibernate属性 ,建议保留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>
        -->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <!-- 配置hibernate的映射文件,可以使用通配符 -->
        <property name="mappingLocations" value="classpath:com/cwh/spring/hibernate/entities/*.hbm.xml"></property>
    </bean>

    <!-- 声明式事务 -->
    <!-- 1.配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 2.配置事务属性,在此之前需要有事务管理器 -->
    <tx:advice id="advice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*"/>
            <tx:method name="purchase" propagation="REQUIRES_NEW"/>
        </tx:attributes>
    </tx:advice>

    <!-- 3.配置切点,还有相应的事务属性 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.cwh.spring.hibernate.service.*.*(..))" id="pointcut"/>
        <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
    </aop:config>   
</beans>

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- 整合spring不必在此配置所有hibernate属性 -->
        <!-- 1.数据源dataSource配置到IOC容器中,不必在此配置数据源 -->
        <!-- 2.实体类关联的.hbm.xml 映射文件,在配置IOC容器中配置sessionFactory实例时一起配置-->
        <!-- 3.此处配置hibernate 基本属性:方言,SQL 显示及格式,生成数据表策略,缓存策略等 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">false</property>
        <property name="hibernate.hbm2ddl.auto">update</property>

    </session-factory>
</hibernate-configuration>

db.properties:

jdbc.userName=root
jdbc.passWord=zeng
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/springpractice?useUnicode=true&amp;characterEncoding=UTF-8

maxPoolSize=20
initialPoolSize=5

数据库表:

测试类:

package com.cwh.spring.hibernate.test;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.sql.SQLException;
import java.util.Arrays;

import javax.sql.DataSource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cwh.spring.hibernate.entities.Book;
import com.cwh.spring.hibernate.service.BookShopService;
import com.cwh.spring.hibernate.service.Cashier;

public class SpringHibernateTest {

    private ApplicationContext context = null;
    private SessionFactory sessionFactory = null;
    private BookShopService bookShopService = null;
    private Cashier cashier = null;
//  private Session session = null;此处不能这样用,这样会有并发时session的问题,要用在dao层的每个方法,每次都创建实时的session

    {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        bookShopService = (BookShopService) context.getBean("bookShopService");
        cashier = (Cashier) context.getBean("cashier");
//      sessionFactory = context.getBean(SessionFactory.class);
//      session = sessionFactory.openSession();
    }

    //测试数据库是否连接成功哦你
    [@Test](https://my.oschina.net/azibug)
    public void testConnection() throws SQLException{
        DataSource dataSource = context.getBean(DataSource.class);
        System.out.println(dataSource.getConnection());
    }

    [@Test](https://my.oschina.net/azibug)
    public void testBookShopService(){
        bookShopService.purchase("AA", "1001");
    }

    [@Test](https://my.oschina.net/azibug)
    public void testCasierService(){
        cashier.checkout("AA", Arrays.asList("1001","1002"));
    }
}

转载于:https://my.oschina.net/u/3780366/blog/1838951

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值