Spring4笔记10--SSH整合1--Spring与Hibernate整合
SSH 框架整合技术:
1. Spring与Hibernate整合(对比Spring与JDBC模板):
Service业务层代码和测试类都不变,添加实体类的映射配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.tongji.beans">
<class name="Student">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="age"/>
</class>
</hibernate-mapping>
修改Dao接口的实现类:
由于 Dao 实现类要通过 Hibernate 来操作 DB,所以在该类中需要获取到 Session 工厂对象 SessionFactory。当然,最终目的是获取到 Hibernate 的 Session 对象。而 SessionFactory对象的创建,也是由 Spring 容器来管理的,所以,需要在 Dao 实现类中添加 SessionFactory属性,以便 Spring 容器通过 setXXX() 将 Session 工厂注入。
package com.tongji.dao; import java.util.List; import org.hibernate.Session;
import org.hibernate.SessionFactory; import com.tongji.beans.Student; public class StudentDaoHbnImpl implements IStudentDao{
private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} @Override
public void insertStudent(Student student) {
sessionFactory.getCurrentSession().save(student);
} @Override
public void deleteStudent(int id) {
Student student = sessionFactory.getCurrentSession().get(Student.class, id);
//Student student = this.selectStudentById(id);
sessionFactory.getCurrentSession().delete(student);
} @Override
public void updateStudent(Student student) {
sessionFactory.getCurrentSession().update(student);
} @Override
public String selectStudentNameById(int id) {
// Student student = this.selectStudentById(id);
// if (student != null) {
// return student.getName();
// }
// return null; String hql = "select name from Student where id= :id";
String name = (String) sessionFactory.getCurrentSession().createQuery(hql).
setInteger("id", id).
uniqueResult();
return name;
} @Override
public List<String> selectStudentNames() {
String hql = "select name from Student";
return sessionFactory.getCurrentSession().createQuery(hql).list();
} @Override
public Student selectStudentById(int id) {
return sessionFactory.getCurrentSession().get(Student.class, id);
} @Override
public List<Student> selectStudents() {
String hql = "from Student";
return sessionFactory.getCurrentSession().createQuery(hql).list();
} }
修改Spring配置文件:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 注册数据源:C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean> <!-- 注册JDBC属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 参考hibernate的主配置文件来配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.current_session_context_class"> org.springframework.orm.hibernate5.SpringSessionContext</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations" value="com/tongji/beans"/>
</bean> <!-- 注册Dao -->
<bean id="studentDao" class="com.tongji.dao.StudentDaoHbnImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- 注册Service -->
<bean id="studentService" class="com.tongji.service.StudentServiceImpl">
<property name="dao" ref="studentDao"></property>
</bean> <!-- 注册事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- 事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 这些被指定的方法应用哪些事务特性 -->
<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="remove*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="modify*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice> <!-- AOP配置 -->
<aop:config>
<!-- 这里的切入点指的是,要将事务通知织入到哪些类的哪些方法上 -->
<aop:pointcut expression="execution(* *..service.*.*(..))" id="servicePointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>
</aop:config>
</beans>
解释:
(1)配置 SessionFactory
Spring 的精髓是,所有的 Bean 均由 Spring 容器统一管理,所以在 Spring 中若要使用Hibernate,就需要将 SessionFactory 交由 Spring 来管理。
配置 SessionFactory 的 Bean,将 hibernate.cfg.xml 文件替换掉。使用的实现类为LocalSessionFactoryBean,注意,是 hibernate5 包下的。其用于设置的属性主要有三个:数据源,映射文件,及 hibernate 特性。其设置内容,与 Hibernate 主配置文件的基本相同。
至于 Hibernate 特性的设置,除了 hibernate.current_session_context_class 外,其余 key与 Hibernate 主配置文件中的属性名相同,值也相同。
属性 hibernate.current_session_context_class,用于指定当前 Session 所执行的上下文环境。其值不再是 thread,而是 SpringSessionContext,表示 Session 将交由 Spring 容器来管理。
(2)配置事务管理器:
由于 Hibernate 的 Session 要求必须在事务环境下才能运行,所以在 Spring 中使用Hibernate ,必须要配置事务管理器,以开启事务环境。此时使用的事务管理器为HibernateTransactionManager。需要注意的是,使用 Jdbc 的事务管理器,需要注入一个数据源 dataSource,而使用 Hibernate 的事务管理器,则需要注入一个 sessionFactory 属性。
补充:在 Spring 中一般不使用 Hibernate 模板对象
Spring4笔记10--SSH整合1--Spring与Hibernate整合的更多相关文章
- SSH框架之Spring+Struts2+Hibernate整合篇
回顾 -Hibernate框架 ORM: 对象关系映射.把数据库表和JavaBean通过映射的配置文件映射起来, 操作JavaBean对象,通过映射的配置文件生成SQL语句,自动执行.操作数据库. 1 ...
- 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)
轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)(国家级奖项获奖作品升级版,四版累计印刷27次发行量超10万册的轻量级Jav ...
- 框架篇:Spring+SpringMVC+hibernate整合开发
前言: 最近闲的蛋疼,搭个框架写成博客记录下来,拉通一下之前所学知识,顺带装一下逼. 话不多说,我们直接步入正题. 准备工作: 1/ IntelliJIDEA的安装配置:jdk/tomcat等..(本 ...
- spring+springmvc+hibernate整合遇到的问题
spring+springmvc+hibernate整合遇到的问题2016年10月20日 23:24:03 守望dfdfdf 阅读数:702 标签: ssh学习经历的异常exception异常框架更多 ...
- Spring第九篇【Spring与Hibernate整合】
前言 前面已经学习了如何使用Spring与Struts2进行整合,本博文主要讲解如何使用Spring对Hibernate进行整合 Spring和Hibernate整合的关键点: SessionFact ...
- spring+springmvc+hibernate 整合
三大框架反反复复搭了很多次,虽然每次都能搭起来,但是效率不高.最近重新搭了一次,理顺了思路,整理了需要注意的地方,分享出来. 工具:Eclipse(jdk 1.7) spring和hibernate版 ...
- springmvc框架(Spring SpringMVC, Hibernate整合)
直接干货 model 考虑给用户展示什么.关注支撑业务的信息构成.构建成模型. control 调用业务逻辑产生合适的数据以及传递数据给视图用于呈献: view怎样对数据进行布局,以一种优美的方式展示 ...
- Spring与Hibernate整合,实现Hibernate事务管理
1.所需的jar包 连接池/数据库驱动包 Hibernate相关jar Spring 核心包(5个) Spring aop 包(4个) spring-orm-3.2.5.RELEASE.jar ...
- Spring与Hibernate整合中,使用OpenSessionInViewFilter后出现sessionFactory未注入问题
近期在知乎看到一句话,保持学习的有一种是你看到了很多其它的牛人,不甘心,真的不甘心. Spring和hibernate整合的时候,jsp页面做展现,发现展现属性出现: org.apache.jaspe ...
- spring和hibernate整合,事务管理
一.spring和hibernate整合开发步骤 1 引入jar文件,用户libarary列表如下 //spring_core spring3..9core\commons-logging-1.2.j ...
随机推荐
- 使用flex布局调换两个按钮的位置
组件用的时antd的Modal组件,里面的按钮需要调换一下位置 今天发现用flex布局非常方便,代码如下: display: flex; justify-content: center; flex-f ...
- delphi手动创建dataset并插入值
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...
- BZOJ2806_Cheat
Ctsc2012的题目.做完感觉自己瞬间变高富帅了. 不过回想其实也觉得不难,想到用单调队列就很简单了,还有二分= =.呵 对于给出的一篇文章,如果你们将它分成若干段,并在所有长度不小于L的片段在字典 ...
- MT【118】利用线面角最小解题
解:如图将正四面体放到立方体中,让AB通过$\alpha$面,让$\alpha$面绕着AB动起来.问题就转化成为EF与面$\alpha$线面角$\theta$了.EF的投影为$|EF|cos\thet ...
- BZOJ 4454: C Language Practice
4454: C Language Practice Time Limit: 20 Sec Memory Limit: 24 MBSubmit: 501 Solved: 112[Submit][St ...
- 洛谷 P1972 [SDOI2009]HH的项链 解题报告
P1972 [SDOI2009]HH的项链 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断 ...
- Linux编程中 #define _XOPEN_SOURCE的作用
[误解]#define _XOPEN_SOURCE决不是简单的宏定义它是使程序符合系统环境的不可缺少的部分 [概念]Glibc 所实现全部或部分规范下的功能有:1.ISO C: C语言国际标准. 2. ...
- Java中的三目运算符可能出现的问题
你真的了解Java中的三目运算符吗? 原创 2018-04-27 刨根问底的 Hollis Hollis Hollis 微信号 hollischuang 功能介绍 一个对Coding有着独特追求的人. ...
- 【DP】【CF1099C】 Postcard
Description 给定一个长度为 \(n\) 的字符串,尽可能包含小写字母,字符 '?' 和字符 '*'.保证上面两种特殊字符若出现则一定出现在一个小写字母的后面一位.要求构造一个长度为 \(k ...
- MySQL 第七篇:视图、触发器、事务、存储过程、函数
一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...