Hibernate+Spring整合开发步骤
Hibernate是一款ORM关系映射框架+Spring是结合第三方插件的大杂烩,Hibernate+Spring整合开发效率大大提升。
整合开发步骤如下:
第一步:导入架包:
1、Hibernate基础包+Spring基础包(AOP代理包和cglib...)
第二步:在spring配置文件中配置datasource(数据库连接信息要么写在hibernate.cfg.xml中;要么写在datasource中)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 设置类扫描器;自动装配Bean -->
<context:component-scan base-package="com.msit.ssh.sh" /> <!-- 引入外部属性文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean> <!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 配置数据源信息 -->
<property name="url">
<value>${connection.url}</value>
</property>
<property name="driverClassName">
<value>${connection.driver_class}</value>
</property>
<property name="username">
<value>${connection.username}</value>
</property>
<property name="password">
<value>${connection.password}</value>
</property>
<!-- 最大连接数 -->
<property name="maxActive">
<value>${jdbc.maxactive}</value>
</property>
<!-- 最大空闲数 -->
<property name="maxIdle">
<value>${jdbc.maxidle}</value>
</property>
<!-- 最小空闲数 -->
<property name="minIdle">
<value>${jdbc.minidle}</value>
</property>
</bean> <!-- sessionFactory(管理hibernate sessionfactory) -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 第一种方式:引入hibernate.cfg.xml -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <!-- 第二种方式:所有的hibernate配置配置在spring中 --> <!-- 配置映射文件 -->
<!--
<property name="mappingDirectoryLocations">
<list>
<value>com/msit/ssh/sh/entity/User.hbm.xml</value>
</list>
</property> --> <!-- 配置其他选项 -->
<!-- <property name="hibernateProperties">
<props>
<prop key=""></prop>
<prop key="show_sql">true</prop>
<prop key="hbm2ddl.auto">update</prop>
</props>
</property> -->
</bean> <!-- 配置hibernate事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 配置哪些方法需要用到事务;哪些方法不需要事务 -->
<tx:method name="*" />
<tx:method name="get*" propagation="NOT_SUPPORTED" />
</tx:attributes>
</tx:advice> <!-- 配置aop -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut id="txPointcut"
expression="execution(* com.msit.ssh.sh.service.impl.*.*(..))" />
<!-- 配置事务通知 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config> <!-- <bean class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property></bean> --> <!-- <bean id="hibernatedaosuppert"
class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"
abstract="true">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <bean id="userdao" class="com.msit.ssh.sh.dao.impl.UserDaoImpl"
parent="hibernatedaosuppert">
</bean> --> </beans>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory> <!-- 数据库连接信息 -->
<!-- <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.username">db2</property>
<property name="connection.password">db2</property> --> <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property> <!-- 其他配置 -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/msit/ssh/sh/entity/User.hbm.xml"/> <!-- <class-cache
class="org.hibernate.test.legacy.Simple"
region="Simple"
usage="read-write"/> -->
</session-factory>
</hibernate-configuration>
3、配置sessionfactory(hibernate交给sprig管理)
里边注入数据源(datasource);再把hibernate配置文件引入进来
或者hibernate所有配置都写在sessionfactory中(舍弃了hibernate配置文件) <!-- sessionFactory(管理hibernate sessionfactory) -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 引入hibernate.cfg.xml -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
4、dao层继承HibernateDaoSuppert(抽象类)(不能用注解);必须要标明abstract="true"
如果使用此方式: <bean id="hibernatedaosuppert"
class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"
abstract="true">
//将sessionFactory注入给HibernateDaoSuppert
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> //dao层Bean必须配置parent="hibernatedaosuppert" <bean id="userdao" class="com.msit.ssh.sh.dao.impl.UserDaoImpl"
parent="hibernatedaosuppert"> </bean> 还想用注解怎么办?
//编写一个超类继承HibernateDaoSuppert
public class BaseHibernateDaoSuppert extends HibernateDaoSupport{ @Resource
//注入sessionFactory
public void setMySessionFactory(SessionFactory sessionFactory){
this.setSessionFactory(sessionFactory);
} } //dao层就继承超类
@Repository("userdao")
public class UserDaoImpl extends BaseHibernateDaoSuppert implements IUserDao
您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态。 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内容有任何疑问, 可以通过评论或发邮件的方式联系我: 2276292708@qq.com 如果需要转载,请注明出处,谢谢!!
Hibernate+Spring整合开发步骤的更多相关文章
- Struts 2 + Hibernate + Spring 整合要点
Struts 2 和 Spring 的功能有重合,因此有必要说明下,整合中分别使用了两种框架的哪些技术. Struts 2 使用功能点: 1.拦截器.一处是对非登录用户购物进行拦截,一处是对文件上传的 ...
- 开发步骤Dubbo、spring mvc、springboot、SSM整合开发步骤
一.Dubbo开发步骤: 链接:https://pan.baidu.com/s/1pMPO1kf 密码:9zaa 第一: 1.创建consumer工程2.在pom.xml文件下添加配置3.添加appl ...
- Spring整合CXF步骤,Spring实现webService,spring整合WebService
Spring整合CXF步骤 Spring实现webService, spring整合WebService >>>>>>>>>>>> ...
- JAX-RS和 Spring 整合开发
JAX-RS 和 和 Spring 整合开发 1.建立maven项目 2.导入maven坐标 <dependencies> <!-- cxf 进行rs开发 必须导入 --> & ...
- Struts2+Hibernate+Spring 整合示例
转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...
- spring整合mybatis步骤分析
1.spring配置datasource bean的时候,不同的数据库连接方式有有不同的datasource实现类. 比如采用c3p0数据库连接池,要用c3p0的datasource实现类 com.m ...
- Struts2+Hibernate+Spring 整合示例[转]
原文 http://blog.csdn.net/tkd03072010/article/details/7468769 Spring整合Struts2.Hibernate原理概述: 从用户角度来看,用 ...
- spring程序开发步骤
1.使用spring框架之前的开发步骤 2.使用spring之后的开发步骤 3.文字描述 1.导入Spring开发的基本依赖 2.编写Dao接口和实现类 3.创建spring核心配置文件 4.在spr ...
- struts2+hibernate整合开发步骤
百度的各种代码,步骤,自己整合了一下 1,创建数据库 常用mysql creat table..... 2,在WebContent下的bin中添加相应的包 http://pan.baidu.com ...
随机推荐
- jQuery异步框架探究1:jQuery._Deferred方法
jQuery异步框架应用于jQuery数据缓存模块.jQuery ajax模块.jQuery事件绑定模块等多个模块,是jQuery的基础功能之中的一个.实际上jQuery实现的异步回调机制能够看做ja ...
- react 项目实战(六)提取布局组件
重复代码是混乱的根源!,本篇文章我们来继续消灭重复代码. 目标 细心的同学应该能发现:每一个Page组件(/src/pages下的组件)的render方法都拥有相似的jsx结构,比如: render ...
- 两个月后才更新一篇。。。。LIB和DLL的差别
共同拥有两种库: 一种是LIB包括了函数所在的DLL文件和文件里函数位置的信息(入口).代码由执行时载入在进程空间中的DLL提供,称为动态链接库dynamic link library. 一种是 ...
- appium desktop
Appium-desktop 下载地址:https://github.com/appium/appium-desktop/releases 一般功能 这些能力跨越多个驱动因素. 仅限Android 这 ...
- AVD那些事儿
启动了AVD却说找不到AVD 错误提示: No active compatible AVD's or devices found. Relaunch this configuration after ...
- @Override用在哪儿
帮朋友改一段代码,看到好多红叉都是指向@Override. 是这样,他代码里写了一个接口.方法都用抽象函数声明在接口类里.然后在继承自这个接口的实现类里写详细方法的空壳 ...
- POJ2451 Uyuw's Concert (半平面交)
POJ2451 给定N个半平面 求他们的交的面积. N<=20000 首先参考 POJ1279 多边形的核 其实就是这里要求的半平面交 但是POJ1279数据较小 O(n^2)的算法 看起来是 ...
- 转贴:CSS伪类与CSS伪元素的区别及由来具体说明
关于两者的区别,其实是很古老的问题.但是时至今日,由于各种网络误传以及一些不负责任的书籍误笔,仍然有相当多的人将伪类与伪元素混为一谈,甚至不乏很多CSS老手.早些年刚入行的时候,我自己也被深深误导,因 ...
- System.out.println()的含义
system是java.lang包中定义的一个内置类,在该类中定义了一个静态对象out out是PrintStream类的实例对象 println是PrintStream类中的方法
- 在Linux环境下使用OpenSSL对消息和文件进行加密(转载)
转自:http://netsecurity.51cto.com/art/201301/378513.htm 1.简介 OpenSSL是一款功能强大的加密工具包.我们当中许多人已经在使用OpenSSL, ...