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 ...
随机推荐
- how install svn client on MacOS
how install svn client on MacOS svn https://www.smartsvn.com/downloads/smartsvn/smartsvn-macosx-11_0 ...
- Java中LinkedList的fori和foreach效率比较
在<Java中ArrayList的fori和foreach效率比较>中对ArrayList的两种循环方式进行了比较,本次对LinkedList的两种遍历方式进行效率的比较. 1. list ...
- Spring、SpringMVC、MyBatis整合
项目结构: 新建web项目:File->new->Dynamic Web Project 一.准备所需jar包1. Spring框架的jar包 spring-framework-5.0.4 ...
- Ubuntu终端命令--查看端口占用及关闭
1.查看已连接的服务端口 (ESTABLISHED) netstat-a 2.查看所有的服务端口(LISTEN,ESTABLISHED) netstat-ap 3.查看指定端口,可以结 ...
- P2234 [HNOI2002]营业额统计
题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是 ...
- [BZOJ1559]密码 AC自动机+状压
问题 K: [JSOI2009]密码 时间限制: 1 Sec 内存限制: 64 MB 题目描述 众所周知,密码在信息领域起到了不可估量的作用.对于普通的登陆口令,唯一的破解 方法就是暴力破解一逐个尝 ...
- .NET 复制对象会影响到复制源对象
IList<string> list=new List<string>(); list.add("a"); list.add("b"); ...
- LINQ 模糊搜索
IList<entity> ls = new List<entity>(); ls = (from k in ls where k.Name.Contains("sa ...
- BGP的那些安全痛点(转)
0x00 BGP(RFC 1771. RFC 4271)定义 全称是Border Gateway Protocol, 对应中文是边界网关协议,最新版本是BGPv4. BGP是互联网上一个核心的互联网去 ...
- java的序列化流和打印流
对象操作流(序列化流) 每次读取和写出的都是JavaBean对象. 序列化:将对象写入到文件中的过程 反序列化:从文件中读取对象到程序的过程 transient: 标识瞬态,序列化的时候,该修饰符修饰 ...