SSH笔记-Spring整合Hibernate

1、整合内容:
(1)由IOC容器管理Hibernate的SessionFactory
(2)让Hibernate使用Spring的声明式事务

2、整合步骤:
(1)加入Hibernate
①加入jar包
②添加hibernate配置文件:hibernate.cfg.xml
③编写持久化类对应的hbm.xml
(2)加入Spring
①加入jar包(注意要加入spring-orm-x.x.x.RELEASE.jar,不然配置TransactionManager的时候会说找不到类)
②加入Spring配置文件
(3)整合

3、Spring Hibernate 事务流程
(1)在方法开始前
①获取Session
②把Session和当前线程绑定,在Dao中使用SessionFactory的getCurrentSession()方法获取Session
③开启事务
(2)方法正常结束时
①提交事务
②解绑当前线程绑定的Session
③关闭Session
(3)方法抛出异常时
①回滚事务
②解绑当前线程绑定的Session
③关闭Session

4、注意事项:
①不推荐使用HbernateTemplate和HibernateDaoSupport,因为会导致Dao和Spring API耦合,使可以移植性变差
②在Spring配置文件中配置hibernate配置文件时,建议创建创建hibernate.cfg.xml文件并且根据路径配置,而不是直接把hibernate配置数据配置在Spring配置文件中

5、配置hibernate基本属性
①数据源配置到IOC容器,在此不许配置数据源
②关联的hbm.xml在IOC容器配置SeesionFactory实例时配置
③配置Hibernate基本属性:方言、SQL显示、生成数据表策略、二级缓存

6、配置Spring基本属性
①配置自动扫描的包
②配置数据源,导入资源文件
③配置C3P0
④通过Spring的LocalSessionFactoryBean配置Hibernate的SessionFactory实例

①配置数据源属性
②配置hibernate配置文件的位置和名称
③配置hibernate映射文件的位置和名称,可以使用通配符

⑤配置Spring的声明式事务

①配置事务管理器
②配置事务属性,需要事务管理器
③配置事务切点,并关联切点和事务

7、文件
①TestMain.java:测试类
②Account.java:数据模型
③Product.java:数据模型
④Account.hbm.xml:数据模型的Hibernate配置文件
⑤Product.hbm.xml:数据模型的Hibernate配置文件
⑥ProductDao.java:逻辑方法接口
⑦SummaryPurchase.java:逻辑方法接口
⑧TransactionDao.java:逻辑方法接口
⑨MyException.java:自定义错误抛出类
⑩ProductImpl.java:逻辑方法接口的实现类
⑩TransactionDaoImpl.java:逻辑方法接口的实现类
⑩SummaryPurchaseImlp.java:逻辑方法接口的实现类
⑩applicationContext.xml:Spring配置文件
⑩db.properties:数据库信息文件
⑩hibernate.cfg.xml:Hibernate配置文件

8、TestMain.java

package com.demo.sshtest;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.demo.sshtest.dao.SummaryPurchase;

/*
1、整合内容:
(1)由IOC容器管理Hibernate的SessionFactory
(2)让Hibernate使用Spring的声明式事务

2、整合步骤:
(1)加入Hibernate
    ①加入jar包
    ②添加hibernate配置文件:hibernate.cfg.xml
    ③编写持久化类对应的hbm.xml
(2)加入Spring
    ①加入jar包(注意要加入spring-orm-x.x.x.RELEASE.jar,不然配置TransactionManager的时候会说找不到类)
    ②加入Spring配置文件
(3)整合

3、Spring Hibernate 事务流程
(1)在方法开始前
    ①获取Session
    ②把Session和当前线程绑定,在Dao中使用SessionFactory的getCurrentSession()方法获取Session
    ③开启事务
(2)方法正常结束时
    ①提交事务
    ②解绑当前线程绑定的Session
    ③关闭Session
(3)方法抛出异常时
    ①回滚事务
    ②解绑当前线程绑定的Session
    ③关闭Session
*/
public class TestMain {

    private static ApplicationContext applicationContext=null;
    private static SummaryPurchase summaryPurchase = null;

    public static void main(String[] args) {
        try{
            testConnection();
            testTransaction();
        }catch(Exception e){
            System.out.println("error message::"+e.getMessage());
        }
    }

    //获取相关配置文件,连接数据库
    public static void testConnection() throws SQLException{
        applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = applicationContext.getBean(DataSource.class);
        System.out.println(dataSource.getConnection());
    }

    public static void testTransaction(){
        summaryPurchase = (SummaryPurchase)applicationContext.getBean(SummaryPurchase.class);
        Map<Integer,Integer> productMap = new HashMap<Integer,Integer>();
        productMap.put(1, 1);
        productMap.put(2, 1);
        productMap.put(3, 1);
        summaryPurchase.sPurchase(1, productMap);
    }
}

9、Account.java

package com.demo.sshtest;

public class Account {

    private Integer id;
    private String name;
    private double balance;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }

}

10、Product.java

package com.demo.sshtest;

public class Product {

    private Integer id;
    private String name;
    private double amount;
    private Integer quantity;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getAmount() {
        return amount;
    }
    public void setAmount(double amount) {
        this.amount = amount;
    }
    public Integer getQuantity() {
        return quantity;
    }
    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

}

11、Account.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.demo.sshtest.Account" table="ACCOUNT">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="balance" type="double">
            <column name="BALANCE" />
        </property>
    </class>
</hibernate-mapping>

12、Product.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.demo.sshtest.Product" table="PRODUCT">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="amount" type="double">
            <column name="AMOUNT" />
        </property>
        <property name="quantity" type="java.lang.Integer">
            <column name="QUANTITY" />
        </property>
    </class>
</hibernate-mapping>

13、ProductDao.java

package com.demo.sshtest.dao;

public interface ProductDao {

    public double getPriceById(int id);
    public void updateQuantity(int id,int quantity);
    public void updateAccountBalance(int id,double amount,int quantity);
}

14、SummaryPurchase.java

package com.demo.sshtest.dao;

import java.util.Map;

public interface SummaryPurchase {

    public void sPurchase(int buyerId,Map<Integer, Integer>productMap);

}

15、TransactionDao.java

package com.demo.sshtest.dao;

public interface TransactionDao {

    public void purchase(int buyerId,int productId,int quantity);
}

16、MyException.java

package com.demo.sshtest.exception;

public class MyException extends RuntimeException{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public MyException() {
        super();
    }

    public MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

    public MyException(String message, Throwable cause) {
        super(message, cause);
    }

    public MyException(String message) {
        super(message);
    }

    public MyException(Throwable cause) {
        super(cause);
    }

}

17、ProductImpl.java

package com.demo.sshtest.Impl;


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

import com.demo.sshtest.dao.ProductDao;
import com.demo.sshtest.exception.MyException;

/*
注意事项:
1、不推荐使用HbernateTemplate和HibernateDaoSupport,因为会导致Dao和Spring API耦合,使可以移植性变差
*/

@Repository
public class ProductImpl implements ProductDao {

    //1、创建SessionFactory,用于获取跟当前线程绑定的session
    @Autowired
    private SessionFactory sessionFactory;

    //2、获取获取跟当前线程绑定的session,写Hibernate的获取session的方法
    private Session getSession(){
        return sessionFactory.getCurrentSession();
    }

    public double getPriceById(int id) {
        String hql = "SELECT p.amount FROM Product p WHERE p.id=:id";
        Query query = getSession().createQuery(hql).setString("id", String.valueOf(id));
        double price = (Double)query.uniqueResult();
        System.out.println("getPriceById >> productId:"+id+", price:"+price);
        return price;
    }

    public void updateQuantity(int id, int quantity) {
        //验证库存
        String check_sql = "SELECT p.quantity FROM Product p WHERE p.id=:id";
        int qu = (int)getSession().createQuery(check_sql).setString("id", String.valueOf(id)).uniqueResult();
        System.out.println("updateQuantity >> productId:"+id+", db quantity:"+qu);
        if(qu==0){
            throw new MyException("no enough quantity");
        }
        //更新库存
        String update_hql = "UPDATE Product p SET p.quantity=(p.quantity-:quantity) where p.id=:id";
        System.out.println("updateQuantity >> productId:"+id+", quantity:"+quantity+",id:"+id);
        getSession().createQuery(update_hql).setString("quantity", String.valueOf(quantity)).setString("id", String.valueOf(id)).executeUpdate();
    }

    public void updateAccountBalance(int id, double amount, int quantity) {
        //验证账户余额
        String check_sql = "SELECT a.balance FROM Account a WHERE a.id=:id";
        double balance = (double)getSession().createQuery(check_sql).setString("id", String.valueOf(id)).uniqueResult();
        System.out.println("updateAccountBalance >> AccountId:"+id+", db balance:"+balance);
        System.out.println("amount:"+amount+",quantity:"+quantity+",ismorethan:"+(balance<(amount*quantity)));
        if( balance<(amount*quantity) ){
            throw new MyException("no enough balance");
        }
        //更新账户余额
        String update_hql = "UPDATE Account a SET a.balance=a.balance-:amount*:quantity where a.id=:id";
        System.out.println("updateAccountBalance >> amount:"+amount+",quantity:"+quantity+",id:"+id);
        getSession().createQuery(update_hql).setString("amount", String.valueOf(amount)).setString("quantity", String.valueOf(quantity)).setString("id", String.valueOf(id)).executeUpdate();
    }

}

18、SummaryPurchaseImlp.java

package com.demo.sshtest.Impl;

import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.sshtest.dao.SummaryPurchase;
import com.demo.sshtest.dao.TransactionDao;

@Service
public class SummaryPurchaseImpl implements SummaryPurchase {

    @Autowired
    private TransactionDao transactionDao;

    public void sPurchase(int buyerId, Map<Integer, Integer> productMap) {
        for (Map.Entry<Integer, Integer> entry : productMap.entrySet()) {
            int productId = (int)entry.getKey();
            int quantity = (int)entry.getValue();
            System.out.println("--------sPurchase >> produtId:"+productId+",quantity:"+quantity);
            transactionDao.purchase(buyerId, productId, quantity);
        }
    }

}

19、TransactionDaoImpl.java

package com.demo.sshtest.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.demo.sshtest.dao.ProductDao;
import com.demo.sshtest.dao.TransactionDao;

@Component
public class TransactionDaoImpl implements TransactionDao{

    @Autowired
    private ProductDao productDao;

    public void purchase(int buyerId, int productId, int quantity) {
        double price = productDao.getPriceById(productId);
        productDao.updateQuantity(productId, quantity);
        productDao.updateAccountBalance(buyerId, price, quantity);
    }

}

20、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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="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.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.demo.sshtest"></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.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>

    <!-- 配置Hibernate的SessionFactory实例 -->
    <!-- 通过Spring的LocalSessionFactoryBean配置 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 配置数据源属性 -->
        <property name="dataSource" ref="dataSource"></property>

        <!-- 配置hibernate配置文件的位置和名称 -->
        <!-- 方法1:通过configLocation配置hibernate配置文件 -->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <!-- 方法2:通过hibernateProperties配置hibernate配置文件,此方法可以省略不用创建hibernate.cfg.xml,但是不建议使用,因为不方便后面加插件和代码检查 -->
        <!--
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</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/demo/sshtest/*.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="advice" 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.demo.sshtest.*.*.*(..))" id="poingCut"/>
        <aop:advisor advice-ref="advice" pointcut-ref="poingCut"/>
    </aop:config>

</beans>

21、db.properties

jdbc.user=root
jdbc.password=
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost/Spring_Hibernate
jdbc.initialPoolSize=5
jdbc.maxPoolSize=10

22、hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

        <!-- 配置hibernate基本属性 -->
        <!-- 1、数据源配置到IOC容器,在此不许配置数据源 -->
        <!-- 2、关联的hbm.xml在IOC容器配置SeesionFactory实例时配置 -->
        <!-- 3、配置Hibernate基本属性:方言、SQL显示、生成数据表策略、二级缓存 -->
        <!-- 配置hibernate基本信息-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- 执行操作室是否在控制台打印sql -->
        <property name="hibernate.show_sql">false</property>
        <!-- 是否对sql格式化 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 指定自动生成数据表的策略 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 启用二级缓存 -->
        <property name="hibernate.cache.use_second_level_cache">true</property>
        <!-- 配置使用的二级缓存的产品 -->
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
        <!-- 配置启用查询缓存 -->
        <property name="cache.use_query_cache">true</property>


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

23、项目目录
项目目录

24、demo
https://download.csdn.net/download/qq_22778717/10483514

猜你喜欢

转载自blog.csdn.net/qq_22778717/article/details/80719262
今日推荐