spring、hibernate的整合

Spring+hibernate框架整合步骤详解  

第一步:新建web 项目:

 

第二步:右键项目myeclipse->addXXXX,按顺序添加hibernatespring开发包

注:先添加hibernate

 

next


这个部分的话,玩过hibernate的小伙伴应该知道如何弄出来的,不会的可以百度一下,这个还是比较简单的。


到这里hibernate开发包就添加成功了。

接下来是添加spring开发包

这里要注意看我选了哪六个。

(ps:这里的话jar library installation最好还是选择第一个)

这里的话jar library installation最好还是选择第一个

spring添加成功,接下来就是hibernate逆向工程操作了。首先切换视图到myeclipse的数据库界面

这里需要注意一下,daotype那里要选择spring Dao

到这里,我们就差不多可以开始正式配置项目了

这里是applicationContext.xml  这里最核心的部分是头部和我写了注释的地方,中间的那些bean是自动生成的,到时候你们可以直接复制我的头部和尾部。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task-3.0.xsd">

	<!-- 配置service 扫描service -->
	<context:component-scan base-package="com.service"></context:component-scan>
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="root"></property>
		<property name="password" value="admin"></property>
		<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1/enterprising">
		</property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml">
		</property>
	</bean>
	<bean id="DepDAO" class="com.dao.DepDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>


	<!-- 事物管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 事物属性 -->
	<tx:advice id="mytx" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*" />
		</tx:attributes>
	</tx:advice>

	<!-- 织入 -->
	<aop:config>
		<aop:advisor advice-ref="mytx" pointcut="execution(* com.service.*.*(..))" />
		<aop:advisor advice-ref="mytx"
			pointcut="execution(* com.back.service.*.*(..))" />
	</aop:config>
</beans>
接下来是最重要的一步,也就是web.xml的配置

web.xml里面的重要部分代码,这个是需要配置的,但通过代码实现效果也是一样的。

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:app*.xml</param-value>
 </context-param>
 <filter>
  <filter-name>openSession</filter-name>
  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
 </filter>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <servlet>
  <servlet-name>springMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
新建springMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task-3.0.xsd">

	<!-- 扫描action -->
	<context:component-scan base-package="com.action"></context:component-scan>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml">
		</property>
	</bean>
	<bean id="DepDAO" class="com.dao.DepDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
</beans>

注意事项:

1.会出现asm包的冲突,解决方法:删除包:cglib-2.2.jar

2.可能会少了个c3p0的包,这个去网上找c3p0-0.9.1.jar

action的写法,一定要注意注解部分,也就是加了@符号的部分:例如

package com.action;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pojo.Dep;
import com.service.DepService;

@Controller
@RequestMapping("/dep.do")
public class DepAction {
	
	@Autowired
	private DepService depService;
	
	@Autowired
	private HttpServletRequest request;
	
	@RequestMapping(params= "p=findall")
	public String findall(){
		int id = 1;
		System.out.println("来了action");
		Dep dep = depService.find(id);
		System.out.println(dep);
		request.setAttribute("dep", dep);
		return "/show.jsp";
	}

}
service也是如此,例如:

package com.service;

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

import com.dao.DepDAO;
import com.pojo.Dep;

@Service
public class DepService {

	@Autowired
	private DepDAO depDAO;
	
	public Dep find(int id){
		System.out.println("来了service");
		Dep dep = depDAO.findById(id);
		return dep;
	}
	
	
}
在jsp里面写

<jsp:forward page="dep.do?p=findall"></jsp:forward> 就能正常运行项目了

效果如下:


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第24次课-1 SpringHibernate整合 本节主要内容 24.1 概述 24.2 管理SessionFactory 24.3 SpringHibernate的简化 24.1 概述 24.1.1 概述 Spring提供了很多IoC特性的支持,方便处理大部分典型的Hibernate整合问题。 如:SessionFactory的注入、HibernateTemplate的简化操作、DAO的支持等。 为了更好地与持久层框架整合Spring还提供了统一的异常处理体系和事务管理方法。 24.1 概述 24.1.1 概述 如果SpringHibernate进行了整合,则Hibernate便处于被Spring管理的状态下,Hibernate所需的基础资源,都由Spring以注入的方式提供。 由Spring接管的内容包括: Hibernate创建SessionFactory时需要的DataSource 执行持久化必需的Session 持久层访问必需的事务控制 24.1 概述 24.1.1 概述 Spring的管理方式: 将DataSource、SessionFactory、Transaction等作为Spring的Bean,通过配置文件的方式来管理。 24.1 概述 24.1.1 概述 Spring提供了DAO支持,可以简化DAO组件的开发,特别是IoC容器的使用,提供了DAO组件与业务逻辑组件之间的松耦合组合方式。 所有的DAO组件,都由容器负责注入到业务逻辑组件中,使用业务逻辑组件无需关心DAO组件的实现。 24.1 概述 24.1.2 两者结合的优势 通用的资源管理:Spring的ApplicationContext能够管理SessionFactory,通过配置文件可以方便改写相关的配置。 有效的Session管理:Spring提供了有效、简单、安全的Hibernate Session处理。 IoC容器降低了DAO组件与业务逻辑层之间的耦合性。 DAO模式的使用,降低了系统重构的代价。 方便的事务管理:Spring提供的声明式事务处理可以全面有效地处理事务。 异常包装:Spring能够包装Hibernate的异常,使开发者可以选择恰当的层来处理异常。 24.2 管理SessionFactory Hibernate的SessionFactory,是单个数据库映射关系编译后的内存镜像,是Hibernate执行持久化访问的基础。 Spring通过ApplicationContext管理SessionFactory,可以不使用Hibernate应用必需的hibernate.cfg.xml。 Spring配置管理SessionFactory与数据库的连接,在实际的应用中,数据源会采用依赖注入的方式,传递给SessionFactory。 见beans-config_sh.xml 24.3 SpringHibernate的简化 24.3.1 概述 Hibernate的持久层访问步骤: 创建Configuration实例 创建SessionFactory实例 创建Session实例 打开事务 开始持久化访问 提交事务 如遇异常,回滚事务 关闭Session 24.3 SpringHibernate的简化 24.3.1 概述 Spring提供的持久层访问的方式,无须显式地打开和关闭Session,也无须在代码中执行任何的事务操作语句。 Spring提供了HibernateTemplate,用于持久层访问。它只要获得SessionFactory的引用,就可以智能地打开Session,并在持久化访问结束后关闭Session,程序开发只需完成持久层逻辑,通用的操作则由HibernateTemplate完成。 24.3 SpringHibernate的简化 24.3.2 简化的具体表现 SpringHibernate的简化包括: 基于依赖注入的SessionFactory管理机制。由依赖注入完成,无需手动创建,它的创建和维护均由BeanFactory负责管理。 更优秀的Session管理机制。Spring对Session的管理是透明的,无须在代码中操作。 统一的事务管理。无论是编程式事务还是声明式事务,Spring都提供一致的编程模型。 24.3 SpringHibernate的简化 24.3.2 简化的具体表现 SpringHibernate的简化包括: 统一的异常处理机制。不再强制开发者在持久层捕捉异常,持久层异常被包装成DataAccessException,底层数据库异常包装成业务异常。开发者可以自己决定在合适的层处理。 HibernateTemplate支持类。可以完成大量Hibernate持久层的操作。 24.3 SpringHibernate的简化 24.3.3 HibernateTemplate概述 Spring提供了org.springframework.orm.hibernate3.HibernateTemplate类和org.springframework.orm.hibernate3.HibernateCallback接口来方便和Hibernate整合HibernateTemplate类封装了Hibernate的主要类,它提供了很多方便的操作数据的方法。 24.3 SpringHibernate的简化 24.3.3 HibernateTemplate概述 HibernateTemplate可将Hibernate的持久层访问模板化。创建HibernateTemplate后,注入一个SessionFactory的引用,就可以执行相关操作了。 HibernateTemplate提供了3个构造函数 HibernateTemplate(SessionFactory sf) HibernateTemplate(SessionFactory sf, boolean allowCreate) 24.3 SpringHibernate的简化 24.3.3 HibernateTemplate的常用方法 HibernateTemplate提供了很多常用的数据访问方法(CRUD)。 另外,从Spring 2.0开始增加了对命名SQL查询的支持,也增加了对分页的支持。 24.3 SpringHibernate的简化 24.3.3 HibernateTemplate的常用方法 void delete(Object entity):删除指定的持久化实例 void deleteAll(Collection entities):删除集合内全部持久化类的实例 List find(String queryString):根据HQL查询字符串来返回实例集合 List findByNamedQuery(String queryName):根据命名查询返回实例集合 Object get(Class entityClass, Serializable id):根据主键加载特定持久化类的实例 24.3 SpringHibernate的简化 24.3.3 HibernateTemplate的常用方法 Serializable save(Object entity):保存新的实例 void saveOrUpdate(Object entity):根据实例状态,选择保存或更新 void update(Object entity):更新实例的状态 void setMaxResults(int maxResults):设置分页的大小 24.3 SpringHibernate的简化 24.3.4 HibernateTemplate的复杂用法 HibernateTemplate还提供了一种更灵活的方式来操作数据库,通过这种方式可以完全使用Hibernate的操作方式。 HibernateTemplate的灵活访问方式是通过如下两个方法完成的: Object execute(HibernateCallback action) List execute(HibernateCallback action) 开发者通过HibernateCallback,可以完全使用Hibernate灵活的方式来访问数据库,解决了Spring封装Hibernate后灵活性不足的缺陷。 24.3 SpringHibernate的简化 24.3.4 HibernateTemplate的复杂用法 HibernateCallback是一个接口,位于org.springframework.orm.hibernate3中。 该接口中只有一个方法doInHibernate(Session session)。 通常,程序中采用实现HibernateCallback的匿名内部类来获取HibernateCallback的实例,方法doInHibernate()就是Spring执行的持久化操作。 24.3 SpringHibernate的简化 24.3.5 HibernateDaoSupport Spring为与Hibernate进行整合,提供了一个工具类HibernateDaoSupport HibernateDaoSupport提供了基于AOP事务的自动处理,程序员完全可以不用理会事务的开始与提交,它会自动完成SessionFactory的注入和事务的注入。 24.3 SpringHibernate的简化 24.3.5 HibernateDaoSupport HibernateDaoSupport类提供的主要方法: public final HibernateTemplate getHibernateTemplate() public final void setSessionFactory(SessionFactory sf) 思考题 1. Spring中是怎么对Hibernate进行支持的? 2. 如何进行SpringHibernate整合?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值