zoukankan      html  css  js  c++  java
  • Spring4.* 中整合 Hibernate

    1. Spring 整合 Hibernate 整合什么 ?

    1). 有 IOC 容器来管理 Hibernate 的 SessionFactory
    2). 让 Hibernate 使用上 Spring 的声明式事务

    2. 整合步骤:

    1). 加入 hibernate
    ①. jar 包
    ②. 添加 hibernate 的配置文件: hibernate.cfg.xml
    ③. 编写了持久化类对应的 .hbm.xml 文件。

    2). 加入 Spring
    ①. jar 包

    spring-aop-4.0.0.RELEASE.jar
    spring-aspects-4.0.4.RELEASE.jar

    spring-beans-4.0.0.RELEASE.jar
    spring-context-4.0.0.RELEASE.jar
    spring-core-4.0.0.RELEASE.jar
    spring-expression-4.0.0.RELEASE.jar
    commons-logging-1.2.jar

    ②. 加入 Spring 的配置文件

    3). 整合.

    3. 编写代码

    整合工程目录结构如下图所示:

    引用到的jar包如下:

    spring的配置文件:applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:tx="http://www.springframework.org/schema/tx"
     6     xmlns:aop="http://www.springframework.org/schema/aop"
     7     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     9         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    10         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    11     
    12     <!-- 配置自动扫描的包 -->
    13     <context:component-scan base-package="com.atguigu.spring.hibernate"></context:component-scan>
    14     
    15     <!-- 配置数据源 -->
    16     <!-- 导入资源文件 -->
    17     <context:property-placeholder location="classpath:db.properties"/>
    18     
    19     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    20         <property name="user" value="${jdbc.user}"></property>
    21         <property name="password" value="${jdbc.password}"></property>
    22         <property name="driverClass" value="${jdbc.driverClass}"></property>
    23         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    24 
    25         <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
    26         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    27     </bean>
    28     
    29     <!-- 配置 Hibernate 的 SessionFactory 实例: 通过 Spring 提供的 LocalSessionFactoryBean 进行配置 -->
    30     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    31         <!-- 配置数据源属性 -->
    32         <property name="dataSource" ref="dataSource"></property>
    33         <!-- 配置 hibernate 配置文件的位置及名称 -->
    34         <!--  
    35         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    36         -->
    37         <!-- 使用 hibernateProperties 属相来配置 Hibernate 原生的属性 -->
    38         <property name="hibernateProperties">
    39             <props>
    40                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
    41                 <prop key="hibernate.show_sql">true</prop>
    42                 <prop key="hibernate.format_sql">true</prop>
    43                 <prop key="hibernate.hbm2ddl.auto">update</prop>
    44             </props>
    45         </property>
    46         <!-- 配置 hibernate 映射文件的位置及名称, 可以使用通配符 -->
    47         <property name="mappingLocations" 
    48             value="classpath:com/atguigu/spring/hibernate/entities/*.hbm.xml"></property>
    49     </bean>
    50 
    51     <!-- 配置 Spring 的声明式事务 -->
    52     <!-- 1. 配置事务管理器 -->
    53     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    54         <property name="sessionFactory" ref="sessionFactory"></property>
    55     </bean>
    56 
    57     <!-- 2. 配置事务属性, 需要事务管理器 -->
    58     <tx:advice id="txAdvice" transaction-manager="transactionManager">
    59         <tx:attributes>
    60             <tx:method name="get*" read-only="true"/>
    61             <tx:method name="purchase" propagation="REQUIRES_NEW"/>
    62             <tx:method name="*"/>
    63         </tx:attributes>
    64     </tx:advice>
    65 
    66     <!-- 3. 配置事务切点, 并把切点和事务属性关联起来 -->
    67     <aop:config>
    68         <aop:pointcut expression="execution(* com.atguigu.spring.hibernate.service.*.*(..))" 
    69             id="txPointcut"/>
    70         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    71     </aop:config>
    72 
    73 </beans>

    hibernate配置文件:hibernate.cfg.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE hibernate-configuration PUBLIC
     3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     5 <hibernate-configuration>
     6     <session-factory>
     7     
     8         <!-- 配置 hibernate 的基本属性 -->
     9         <!-- 1. 数据源需配置到 IOC 容器中, 所以在此处不再需要配置数据源 -->
    10         <!-- 2. 关联的 .hbm.xml 也在 IOC 容器配置 SessionFactory 实例时在进行配置 -->
    11         <!-- 3. 配置 hibernate 的基本属性: 方言, SQL 显示及格式化, 生成数据表的策略以及二级缓存等. -->
    12         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    13 
    14         <property name="hibernate.show_sql">true</property>
    15         <property name="hibernate.format_sql">true</property>
    16         
    17         <property name="hibernate.hbm2ddl.auto">update</property>
    18         
    19         <!-- 配置 hibernate 二级缓存相关的属性. -->
    20                 
    21     </session-factory>
    22 </hibernate-configuration>

    数据库配置文件:db.properties

    1 jdbc.user=root
    2 jdbc.password=123456
    3 jdbc.driverClass=com.mysql.jdbc.Driver
    4 jdbc.jdbcUrl=jdbc:mysql:///spring
    5 
    6 jdbc.initPoolSize=5
    7 jdbc.maxPoolSize=10
    8 #...

    DAO层接口文件:BookShopDao

     1 package com.lltse.spring.hibernate.dao;
     2 
     3 public interface BookShopDao {
     4 
     5     //根据书号获取书的单价
     6     public int findBookPriceByIsbn(String isbn);
     7     
     8     //更新数的库存. 使书号对应的库存 - 1
     9     public void updateBookStock(String isbn);
    10     
    11     //更新用户的账户余额: 使 username 的 balance - price
    12     public void updateUserAccount(String username, int price);
    13 }

    DAO层接口实现类:

    Entity:Account

     1 package com.lltse.spring.hibernate.entities;
     2 
     3 public class Account {
     4 
     5     private Integer id;
     6     private String username;
     7     private int balance;
     8 
     9     public Integer getId() {
    10         return id;
    11     }
    12 
    13     public void setId(Integer id) {
    14         this.id = id;
    15     }
    16 
    17     public String getUsername() {
    18         return username;
    19     }
    20 
    21     public void setUsername(String username) {
    22         this.username = username;
    23     }
    24 
    25     public int getBalance() {
    26         return balance;
    27     }
    28 
    29     public void setBalance(int balance) {
    30         this.balance = balance;
    31     }
    32 
    33 }

    Book

     1 package com.lltse.spring.hibernate.entities;
     2 
     3 public class Book {
     4 
     5     private Integer id;
     6     private String bookName;
     7     private String isbn;
     8     private int price;
     9     private int stock;
    10 
    11     public Integer getId() {
    12         return id;
    13     }
    14 
    15     public void setId(Integer id) {
    16         this.id = id;
    17     }
    18 
    19     public String getBookName() {
    20         return bookName;
    21     }
    22 
    23     public void setBookName(String bookName) {
    24         this.bookName = bookName;
    25     }
    26 
    27     public String getIsbn() {
    28         return isbn;
    29     }
    30 
    31     public void setIsbn(String isbn) {
    32         this.isbn = isbn;
    33     }
    34 
    35     public int getPrice() {
    36         return price;
    37     }
    38 
    39     public void setPrice(int price) {
    40         this.price = price;
    41     }
    42 
    43     public int getStock() {
    44         return stock;
    45     }
    46 
    47     public void setStock(int stock) {
    48         this.stock = stock;
    49     }
    50 
    51 }

    hibernate对实体的映射文件:

    Account.hbm.xml

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     4 
     5 <hibernate-mapping>
     6     <class name="com.lltse.spring.hibernate.entities.Account" table="SH_ACCOUNT">
     7         
     8         <id name="id" type="java.lang.Integer">
     9             <column name="ID" />
    10             <generator class="native" />
    11         </id>
    12         
    13         <property name="username" type="java.lang.String">
    14             <column name="USERNAME" />
    15         </property>
    16         
    17         <property name="balance" type="int">
    18             <column name="BALANCE" />
    19         </property>
    20         
    21     </class>
    22 </hibernate-mapping>

    Book.hbm.xml

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     4 
     5 <hibernate-mapping>
     6     <class name="com.lltse.spring.hibernate.entities.Book" table="SH_BOOK">
     7     
     8         <id name="id" type="java.lang.Integer">
     9             <column name="ID" />
    10             <generator class="native" />
    11         </id>
    12         
    13         <property name="bookName" type="java.lang.String">
    14             <column name="BOOK_NAME" />
    15         </property>
    16         
    17         <property name="isbn" type="java.lang.String">
    18             <column name="ISBN" />
    19         </property>
    20         
    21         <property name="price" type="int">
    22             <column name="PRICE" />
    23         </property>
    24         
    25         <property name="stock" type="int">
    26             <column name="STOCK" />
    27         </property>
    28         
    29     </class>
    30 </hibernate-mapping>

    自定义异常:

    BookStockException.java

     1 package com.lltse.spring.hibernate.exceptions;
     2 
     3 public class BookStockException extends RuntimeException{
     4 
     5     /**
     6      * 
     7      */
     8     private static final long serialVersionUID = 1L;
     9 
    10     public BookStockException() {
    11         super();
    12         // TODO Auto-generated constructor stub
    13     }
    14 
    15     public BookStockException(String message, Throwable cause,
    16             boolean enableSuppression, boolean writableStackTrace) {
    17         super(message, cause, enableSuppression, writableStackTrace);
    18         // TODO Auto-generated constructor stub
    19     }
    20 
    21     public BookStockException(String message, Throwable cause) {
    22         super(message, cause);
    23         // TODO Auto-generated constructor stub
    24     }
    25 
    26     public BookStockException(String message) {
    27         super(message);
    28         // TODO Auto-generated constructor stub
    29     }
    30 
    31     public BookStockException(Throwable cause) {
    32         super(cause);
    33         // TODO Auto-generated constructor stub
    34     }
    35 
    36     
    37 }

    UserAccountException.java

    package com.lltse.spring.hibernate.exceptions;
    
    public class UserAccountException extends RuntimeException{
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        public UserAccountException() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public UserAccountException(String message, Throwable cause,
                boolean enableSuppression, boolean writableStackTrace) {
            super(message, cause, enableSuppression, writableStackTrace);
            // TODO Auto-generated constructor stub
        }
    
        public UserAccountException(String message, Throwable cause) {
            super(message, cause);
            // TODO Auto-generated constructor stub
        }
    
        public UserAccountException(String message) {
            super(message);
            // TODO Auto-generated constructor stub
        }
    
        public UserAccountException(Throwable cause) {
            super(cause);
            // TODO Auto-generated constructor stub
        }
    
        
        
    }

    service 接口:

    BookShopService.java

    1 package com.lltse.spring.hibernate.service;
    2 
    3 public interface BookShopService {
    4     
    5     public void purchase(String username, String isbn);
    6     
    7 }

    Cashier.java

    1 package com.lltse.spring.hibernate.service;
    2 
    3 import java.util.List;
    4 
    5 public interface Cashier {
    6 
    7     public void checkout(String username, List<String> isbns);
    8     
    9 }

    service实现类层:

    BookShopServiceImpl.java

     1 package com.lltse.spring.hibernate.service.impl;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Service;
     5 
     6 import com.lltse.spring.hibernate.dao.BookShopDao;
     7 import com.lltse.spring.hibernate.service.BookShopService;
     8 
     9 @Service
    10 public class BookShopServiceImpl implements BookShopService {
    11 
    12     @Autowired
    13     private BookShopDao bookShopDao;
    14     
    15     /**
    16      * Spring hibernate 事务的流程
    17      * 1. 在方法开始之前
    18      * ①. 获取 Session
    19      * ②. 把 Session 和当前线程绑定, 这样就可以在 Dao 中使用 SessionFactory 的
    20      * getCurrentSession() 方法来获取 Session 了
    21      * ③. 开启事务
    22      * 
    23      * 2. 若方法正常结束, 即没有出现异常, 则
    24      * ①. 提交事务
    25      * ②. 使和当前线程绑定的 Session 解除绑定
    26      * ③. 关闭 Session
    27      * 
    28      * 3. 若方法出现异常, 则:
    29      * ①. 回滚事务
    30      * ②. 使和当前线程绑定的 Session 解除绑定
    31      * ③. 关闭 Session
    32      */
    33     @Override
    34     public void purchase(String username, String isbn) {
    35         int price = bookShopDao.findBookPriceByIsbn(isbn);
    36         bookShopDao.updateBookStock(isbn);
    37         bookShopDao.updateUserAccount(username, price);
    38     }
    39 
    40 }

    CashierImpl.java

     1 package com.lltse.spring.hibernate.service.impl;
     2 
     3 import java.util.List;
     4 
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Service;
     7 
     8 import com.lltse.spring.hibernate.service.BookShopService;
     9 import com.lltse.spring.hibernate.service.Cashier;
    10 
    11 @Service
    12 public class CashierImpl implements Cashier{
    13     
    14     @Autowired
    15     private BookShopService bookShopService;
    16     
    17     @Override
    18     public void checkout(String username, List<String> isbns) {
    19         for(String isbn:isbns){
    20             bookShopService.purchase(username, isbn);
    21         }
    22     }
    23 
    24 }

    junit测试用例

    SpringHibernateTest.java

     1 package com.lltse.spring.hibernate.test;
     2 
     3 import java.sql.SQLException;
     4 import java.util.Arrays;
     5 
     6 import javax.sql.DataSource;
     7 
     8 import org.junit.Test;
     9 import org.springframework.context.ApplicationContext;
    10 import org.springframework.context.support.ClassPathXmlApplicationContext;
    11 
    12 import com.lltse.spring.hibernate.service.BookShopService;
    13 import com.lltse.spring.hibernate.service.Cashier;
    14 
    15 public class SpringHibernateTest {
    16 
    17     private ApplicationContext ctx = null;
    18     private BookShopService bookShopService = null;
    19     private Cashier cashier = null;
    20     
    21     {
    22         ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    23         bookShopService = ctx.getBean(BookShopService.class);
    24         cashier = ctx.getBean(Cashier.class);
    25     }
    26     
    27     @Test
    28     public void testCashier(){
    29         cashier.checkout("aa", Arrays.asList("1001","1002"));
    30     }
    31     
    32     @Test
    33     public void testBookShopService(){
    34         bookShopService.purchase("aa", "1001");
    35     }
    36     
    37     @Test
    38     public void testDataSource() throws SQLException {
    39         DataSource dataSource = ctx.getBean(DataSource.class);
    40         System.out.println(dataSource.getConnection());
    41     }
    42 
    43 }
  • 相关阅读:
    VIM下的可视模式的相关知识
    VIM下的插入模式的相关知识:
    VIM下的普通模式的相关知识
    VIM中一些按键的作用:
    大师源于勤奋
    在你放弃以前,问问自己这6个问题
    一键查看IE密码!IE PassView简易教程
    如何查看Chrome浏览器保存的账号密码
    如何查看Firefox中保存的登录密码
    谈谈360浏览器保存密码的差异
  • 原文地址:https://www.cnblogs.com/lltse/p/5636244.html
Copyright © 2011-2022 走看看