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
<?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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.atguigu.spring.hibernate"></context:component-scan> <!-- 配置数据源 -->
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/> <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.initPoolSize}"></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 配置文件的位置及名称 -->
<!--
<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.atguigu.spring.hibernate.service.*.*(..))"
id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config> </beans>
hibernate配置文件: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> <!-- 配置 hibernate 的基本属性 -->
<!-- 1. 数据源需配置到 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">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置 hibernate 二级缓存相关的属性. --> </session-factory>
</hibernate-configuration>
数据库配置文件:db.properties
jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///spring jdbc.initPoolSize=5
jdbc.maxPoolSize=10
#...
DAO层接口文件:BookShopDao
package com.lltse.spring.hibernate.dao; public interface BookShopDao { //根据书号获取书的单价
public int findBookPriceByIsbn(String isbn); //更新数的库存. 使书号对应的库存 - 1
public void updateBookStock(String isbn); //更新用户的账户余额: 使 username 的 balance - price
public void updateUserAccount(String username, int price);
}
DAO层接口实现类:
Entity:Account
package com.lltse.spring.hibernate.entities; 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 int getBalance() {
return balance;
} public void setBalance(int balance) {
this.balance = balance;
} }
Book
package com.lltse.spring.hibernate.entities; public class Book { private Integer id;
private String bookName;
private String isbn;
private int price;
private int stock; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getBookName() {
return bookName;
} public void setBookName(String bookName) {
this.bookName = bookName;
} public String getIsbn() {
return isbn;
} public void setIsbn(String isbn) {
this.isbn = isbn;
} public int getPrice() {
return price;
} public void setPrice(int price) {
this.price = price;
} public int getStock() {
return stock;
} public void setStock(int stock) {
this.stock = stock;
} }
hibernate对实体的映射文件:
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.lltse.spring.hibernate.entities.Account" table="SH_ACCOUNT"> <id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id> <property name="username" type="java.lang.String">
<column name="USERNAME" />
</property> <property name="balance" type="int">
<column name="BALANCE" />
</property> </class>
</hibernate-mapping>
Book.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.lltse.spring.hibernate.entities.Book" table="SH_BOOK"> <id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id> <property name="bookName" type="java.lang.String">
<column name="BOOK_NAME" />
</property> <property name="isbn" type="java.lang.String">
<column name="ISBN" />
</property> <property name="price" type="int">
<column name="PRICE" />
</property> <property name="stock" type="int">
<column name="STOCK" />
</property> </class>
</hibernate-mapping>
自定义异常:
BookStockException.java
package com.lltse.spring.hibernate.exceptions; public class BookStockException extends RuntimeException{ /**
*
*/
private static final long serialVersionUID = 1L; public BookStockException() {
super();
// TODO Auto-generated constructor stub
} public BookStockException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
} public BookStockException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
} public BookStockException(String message) {
super(message);
// TODO Auto-generated constructor stub
} public BookStockException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
} }
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
package com.lltse.spring.hibernate.service; public interface BookShopService { public void purchase(String username, String isbn); }
Cashier.java
package com.lltse.spring.hibernate.service; import java.util.List; public interface Cashier { public void checkout(String username, List<String> isbns); }
service实现类层:
BookShopServiceImpl.java
package com.lltse.spring.hibernate.service.impl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.lltse.spring.hibernate.dao.BookShopDao;
import com.lltse.spring.hibernate.service.BookShopService; @Service
public class BookShopServiceImpl implements BookShopService { @Autowired
private BookShopDao bookShopDao; /**
* Spring hibernate 事务的流程
* 1. 在方法开始之前
* ①. 获取 Session
* ②. 把 Session 和当前线程绑定, 这样就可以在 Dao 中使用 SessionFactory 的
* getCurrentSession() 方法来获取 Session 了
* ③. 开启事务
*
* 2. 若方法正常结束, 即没有出现异常, 则
* ①. 提交事务
* ②. 使和当前线程绑定的 Session 解除绑定
* ③. 关闭 Session
*
* 3. 若方法出现异常, 则:
* ①. 回滚事务
* ②. 使和当前线程绑定的 Session 解除绑定
* ③. 关闭 Session
*/
@Override
public void purchase(String username, String isbn) {
int price = bookShopDao.findBookPriceByIsbn(isbn);
bookShopDao.updateBookStock(isbn);
bookShopDao.updateUserAccount(username, price);
} }
CashierImpl.java
package com.lltse.spring.hibernate.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.lltse.spring.hibernate.service.BookShopService;
import com.lltse.spring.hibernate.service.Cashier; @Service
public class CashierImpl implements Cashier{ @Autowired
private BookShopService bookShopService; @Override
public void checkout(String username, List<String> isbns) {
for(String isbn:isbns){
bookShopService.purchase(username, isbn);
}
} }
junit测试用例
SpringHibernateTest.java
package com.lltse.spring.hibernate.test; import java.sql.SQLException;
import java.util.Arrays; import javax.sql.DataSource; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lltse.spring.hibernate.service.BookShopService;
import com.lltse.spring.hibernate.service.Cashier; public class SpringHibernateTest { private ApplicationContext ctx = null;
private BookShopService bookShopService = null;
private Cashier cashier = null; {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
bookShopService = ctx.getBean(BookShopService.class);
cashier = ctx.getBean(Cashier.class);
} @Test
public void testCashier(){
cashier.checkout("aa", Arrays.asList("1001","1002"));
} @Test
public void testBookShopService(){
bookShopService.purchase("aa", "1001");
} @Test
public void testDataSource() throws SQLException {
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
} }
Spring4.* 中整合 Hibernate的更多相关文章
- 笔记49 在Spittr应用中整合Hibernate
在前边构建的Spittr应用中整合Hibernate 由于最近所学的hibernate都是使用xml方式进行配置的,所以在与以Java方式配置的Spittr应用结合时就会出现一些小问题,在此进行总结. ...
- SSH整合,applicationContext.xml中配置hibernate映射文件问题
今天在applicationContext.xml中配置sessionFactory时遇到了各种头疼的问题,现在总结一下: 1.<property name="mappingDirec ...
- Struts2.3.34+Hibernate 4.x+Spring4.x 整合二部曲之上部曲
1 导入jar包 可以复制jar包或maven导入,本文最后会给出github地址 2 导入log4j.properties文件 og4j.appender.stdout=org.apache.log ...
- 使用IDEA整合spring4+spring mvc+hibernate
配置文件 spring-mvc.xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans x ...
- 【Spring实战-3】Spring整合Hibernate、Struts
作者:ssslinppp 1. 摘要 版本: Spring4.0.4:Hibernate4.3.5:struts2.3.16: 主要介绍了如下内容: 项目结构的规划: Spring下整合Hi ...
- Spring框架学习(4)spring整合hibernate
内容源自:spring整合hibernate spring整合注解形式的hibernate 这里和上一部分学习一样用了模板模式, 将hibernate开发流程封装在ORM层提供的模板类Hiber ...
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
- spring整合hibernate的详细步骤
Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...
- spring整合hibernate
spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...
随机推荐
- Can a windows dll retrieve its own filename?
http://stackoverflow.com/questions/2043/can-a-windows-dll-retrieve-its-own-filename A windows exe fi ...
- DC-DC converter Control techniques
As shown in figure 3.4, PWM controller contains two main parts; voltage error-amplifier and voltage ...
- 【提醒】使用 iptables 时,特别注意 规则的顺序
在 centos 上安装 redis 服务器,很快就搞定了,服务器上使用 redis-cl 测试都没有问题了. 但到宿主机上测试,怎么测试都不通过,关键是:关闭了 centos 的 Iptables ...
- Lua中的元表和元方法
Lua中每个值都可具有元表. 元表是普通的Lua表,定义了原始值在某些特定操作下的行为.你可通过在值的原表中设置特定的字段来改变作用于该值的操作的某些行为特征.例如,当数字值作为加法的操作数时,Lua ...
- Spark学习散点总结
使用Spark 时,通常会有两种模式.一.在交互式编程环境(REPL, a.k.a spark-shell)下实现一些代码,测试一些功能点.二.像MapReduce 那样提前编写好源代码并编译打包(仅 ...
- RS交叉表按照预定的节点成员排序
需求:RS一个交叉表,显示所有(科室-职称-医生)的就诊量,但是针对同一个科室来说,该科室的主任执行报表首先需要第一个看到的是主任医师级别的工作量 效果如图: 得到这个需求后感觉很简单,就是根据职称排 ...
- 近200篇机器学习&深度学习资料分享(含各种文档,视频,源码等)(1)
原文:http://developer.51cto.com/art/201501/464174.htm 编者按:本文收集了百来篇关于机器学习和深度学习的资料,含各种文档,视频,源码等.而且原文也会不定 ...
- Unity5.1 新的网络引擎UNET(十五) Networking 引用--下
孙广东 2015.7.21 本节提供了与网络系统一起使用的组件的具体信息. 10.Network Proximity Checker Suggest a change Success! Than ...
- SQL语法 之 操作语句
一.插入语句 1.插入单行记录 INSERT INTO table_name|view_name[(column1_name [,column2_name, ... ])] VALUES( value ...
- ObjectMapper处理从远程获取的Object对象 (http://bbs.csdn.net/topics/390337813?page=1)这个网址也有讲解
微服务中从其他服务获取过来的对象,如果从Object强转为自定义的类型会报错,利用ObjectMapper转换. ObjectMapper mapper = new ObjectMapper(); D ...