java深入探究12-框架整合
1.Spring与Hibernate整合
需要配置的就是hibernate和bean.xml
1)关键点:sessionFactory创建交给SpringIOC;session的事务处理交给Spring的事务处理
2)jar包:
连接池/数据库驱动包
Hibernate相关jar
Spring 核心包(5个)
Spring aop 包(4个)
spring-orm-3.2.5.RELEASE.jar【spring对hibernate的支持】
spring-tx-3.2.5.RELEASE.jar【事务相关】
3)整合步骤:A:创建sessionFactory 的bean节点;B:对逻辑层的业务方法添加事务
A:sessionFactory整合在Spring bean中的三种方式:
1.全部hibernate.cfg.xml引入bean.xml中
2.部分引入:dataSource连接池写在Spring中
3.全部在bean.xml中写
B:对逻辑层添加事务用aop事务方式
4)例子:
.bean.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:p="http://www.springframework.org/schema/p"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- dao 实例 -->
<bean id="deptDao" class="dao.DeptDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- service 实例 -->
<bean id="deptService" class="cn.itcast.service.DeptService">
<property name="deptDao" ref="deptDao"></property>
</bean>
<!-- 数据源配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value=""></property>
<property name="maxPoolSize" value=""></property>
<property name="maxStatements" value=""></property>
<property name="acquireIncrement" value=""></property>
</bean> <!-- ###########Spring与Hibernate整合 start########### -->
<!-- 方式一:直接加载hibernate.cfg.xml文件的方式整合 这种方式 -->
<!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean> --> <!-- 方式二:连接池交给Spring来生成【一部分配置写到hibernate中,一份分在spring中完成】 -->
<!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean> --> <!-- 推荐 方式三:所有配置全部写到Spring配置中 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 连接池配置 -->
<property name="dataSource" ref="dataSource"></property>
<!-- hibernate常用配置会 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- hibernate映射配置 -->
<!-- hibernate映射配置
<property name="mappingLocations">
<list>
<value>classpath:entity/*.hbm.xml</value>
</list>
</property>
-->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:entity/</value>
</list>
</property> </bean> <!-- ###########Spring与Hibernate整合 end########### -->
<!-- 事务配置 -->
<!-- a. 配置事务管理器类 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- b. 配置事务增强(拦截到方法后如果管理事务?) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- c. Aop配置 -->
<aop:config>
<aop:pointcut expression="execution(* service.*.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
2.SSH:三大框架整合在一起
1)引入SSH Jar
Struts 核心jar
Hibernate 核心jar
Spring
Core 核心功能
Web 对web模块支持
Aop aop支持
Orm 对hibernate支持
Jdbc/tx jdbc支持包、事务相关包
2)配置
Web.xml
初始化struts功能、spring容器
Struts.xml 配置请求路径与映射action的关系
Spring.xml IOC容器配置
bean-base.xml 【公用信息】
bean-service.xml
bean-dao.xml
bean-action.xml
3)例子:
web.xml配置
1.配置spring的OpenSessionInView模式 ;2.struts2配置 3.Spring配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- 配置spring的OpenSessionInView模式 【目的:JSp页面访问懒加载数据】 -->
<!-- 注意:访问struts时候需要带上*.action后缀 -->
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.配置src下的各层bean.xml
.bean-base.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:p="http://www.springframework.org/schema/p"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 所有配置的公共部门 --> <!-- ) 连接池实例 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value=""></property>
<property name="maxPoolSize" value=""></property>
</bean> <!-- ) SessionFactory实例创建 -->
<!-- 所有的配置都由spring维护(项目中不需要hibernate.cfg.xml啦) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- a. 连接池 -->
<property name="dataSource" ref="dataSource"></property> <!-- b. hibernate常用配置: 方言、显示sql、自动建表等 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property> <!-- c. 映射配置 -->
<property name="mappingLocations">
<list>
<value>classpath:entity/*.hbm.xml</value>
</list>
</property>
</bean> <!-- 3) 事务配置 -->
<!-- # 事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- # 事务增强 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- # AOP配置 -->
<aop:config>
<aop:pointcut expression="execution(* service.*.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config> </beans>
2.bean.dao.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:p="http://www.springframework.org/schema/p"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="employeeDao" class="cn.itcast.dao.EmployeeDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> </beans>
3.bean-service.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:p="http://www.springframework.org/schema/p"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="employeeService" class="cn.itcast.service.EmployeeService">
<property name="employeeDao" ref="employeeDao"></property>
</bean> </beans>
4.bean-action.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:p="http://www.springframework.org/schema/p"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="employeeAction" class="cn.itcast.action.EmployeeAction" scope="prototype">
<property name="employeeService" ref="employeeService"></property>
</bean> </beans>
3.配置structs
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="emp" extends="struts-default"> <!-- action实例交给spring容器创建 -->
<action name="show" class="employeeAction" method="execute">
<result name="success">/index.jsp</result>
</action> </package> </struts>
4.ssh在用的时候要注意的
1)三者结合在一起时,对数据层进行操作本来是有hibernate的session对象来做的,现在可由spring来获得hibernate的得到hibernateTemple对象来代替springDao中的jdbcTemple和hibernate中的session和dbutil等工具
使用它时需要实现HibernateDaoSupport接口;使用此接口的类也需要在bean中添加sessionFactory对象
spring 中获得由spring所配置的hibernate的操作对象,然后利用此对象进行,保存,修改和删除等操作,此方法是在配置了spring以后,hibernate由spring接管,不直接使用hibernate的session了
HibernateTemplate提供非常多的常用方法来完成基本的操作,比如通常的增加、删除、修改、查询等操作,Spring .0更增加对命名SQL查询的支持,也增加对分页的支持。大部分情况下,使用Hibernate的常规用法,就可完成大多数DAO对象的CRUD操作。下面是HibernateTemplate的常用方法简介:
q void delete(Object entity):删除指定持久化实例
q deleteAll(Collection entities):删除集合内全部持久化类实例
q find(String queryString):根据HQL查询字符串来返回实例集合
q findByNamedQuery(String queryName):根据命名查询返回实例集合
q get(Class entityClass, Serializable id):根据主键加载特定持久化类的实例
q save(Object entity):保存新的实例
q saveOrUpdate(Object entity):根据实例状态,选择保存或者更新
q update(Object entity):更新实例的状态,要求entity是持久状态
q setMaxResults(int maxResults):设置分页的大小
getHibernateTemplate已经封装好了一些基本的方法,可以直接去用,也就是template嘛,
而getSession只是获取一个数据工厂的session,然后大部分方法都需要自己写,加hql语句,然后用query方法执行 谈不上什么优点缺点,类似添加删除更新这样的可以直接用getHibernateTemplate而大部分带条件查询的就需要用getSession自己写了
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
主流技术到底就是主流技术,效率就不一般。
Hibernate封装了对数据库的例行操作,比单纯的jdbc的DAO,开发效率要高很多了。而Springframework对Hibernate的操作又进行了进一步的包装,又将开发效率提升不少。下面的例子是Spring自己给的petclinic的样本程序。
原来b/s架构的大量数据库操作可以这么轻松搞定
java深入探究12-框架整合的更多相关文章
- Java EE互联网轻量级框架整合开发— SSM框架(中文版带书签)、原书代码
Java EE互联网轻量级框架整合开发 第1部分 入门和技术基础 第1章 认识SSM框架和Redis 2 1.1 Spring框架 2 1.2 MyBatis简介 6 1.3 Spring MVC简介 ...
- 【Java】SSM框架整合 附源码
前言 前面已经介绍Spring和Mybatis整合,而本篇介绍在IDEA下Spring.Spring MVC.Mybatis(SSM)三个框架的整合,在阅读本篇之前,建议大家先去了解一下Spring. ...
- 使用Spring框架整合Java Mail
我的博客名为黑客之谜,今天演示的案例中会出现我的邮箱,还不赶紧收藏!我现在是小白,但是随着时间的流逝,我会逐渐向大神走进,所以,喜欢我的,或者喜欢大神的,点一波关注吧!顺便说一下,双十二快到了,有什么 ...
- [Java] SSH框架笔记_框架整合示例(一)
本文描述的是框架SSH集成的示例,由于在这个过程中有一些小的细节容易被遗忘,特别撰写了一篇小的博文来记录这个过程,希望对自己以及后来者能够起到积极意义. 本文中使用的框架和版本号为: struts-2 ...
- Struts2+Hibernate4+Spring4框架整合搭建Java项目原型
收藏 http://www.cnblogs.com/mageguoshi/p/5850956.html Struts2+Hibernate4+Spring4框架整合搭建Java项目原型
- 【Java】Spring MVC 扩展和SSM框架整合
开发web项目通常很多地方需要使用ajax请求来完成相应的功能,比如表单交互或者是复杂的UI设计中数据的传递等等.对于返回结果,我们一般使用JSON对象来表示,那么Spring MVC中如何处理JSO ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSM(Spring+Spring MVC+MyBatis)框架整合搭建详细步骤
因为 Spring MVC 是 Spring 框架中的一个子模块,所以 Spring 与 SpringMVC 之间不存在整合的问题.实际上,SSM 框架的整合只涉及 Spring 与 MyBatis ...
- SSM框架整合( Spring 、 SpringMVC 和 Mybatis )
1.基本概念 1.1.Spring Spring 是一个开源框架, Spring 是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作 Expert O ...
- spring、spring mvc、mybatis框架整合基本知识
学习了一个多月的框架知识了,这两天很想将它整合一下.网上看了很多整合案例,基本都是基于Eclipse的,但现在外面公司基本都在用Intellij IDEA了,所以结合所学知识,自己做了个总结,有不足之 ...
随机推荐
- uva 12730(期望经典)
选自: http://blog.csdn.net/myhelperisme/article/details/39724515 用dp(n)表示有n个位置时的期望值,那么,对于一个刚进来的人来说,他有 ...
- QL 获取当前日期,年、月、日、周、时、分、秒
?select GETDATE() as '当前日期', DateName(year,GetDate()) as '年', DateName(month,GetDate()) as '月', Date ...
- CSS伪类选择器active模拟JavaScript点击事件
一.说明 设置元素在被用户激活(在鼠标点击与释放之间发生的事件)时的样式. IE7及更早浏览器只支持a元素的:active,从IE8开始支持其它元素的:active. 另:如果需要给超链接定义:访问前 ...
- ZRGGBS00 GGB1替代问题
ZRGGBS00ZRGGBS00ZRGGBS00 和Validation不同的是,Validation只做检测,一般不做相应数据的修改,Substitution弥补了这反面的缺陷,它和user exi ...
- 软件磁盘阵列(RAID)
RAID软件磁盘阵列 RAID 即廉价磁盘冗余阵列,其高可用性和可靠性适用于大规模环境中,相比正常使用,数据更需要被保护.RAID 是将多个磁盘整合的大磁盘,不仅具有存储功能,同时还有数据保护功能. ...
- django事物回滚
往数据库写入数据时,不经意间就会写入不完整的数据,我们称之为脏数据.事务管理(transaction)可以防止这种情况发生.事务管理一旦检测到写入异常,会执行回滚操作,即要么写入完整的数据,要么不写入 ...
- Git处理 行结束符
Dealing with line endings (Windows) 如果你正在使用Git在GitHub上和别人协作的话,确保Git处理行结束符的配置已经正确配置了. 每次在键盘上按下return键 ...
- Java技术相关
1.System.getProperty("user.dir") References:API
- 解释python中join()和split()函数
join能让我们将指定字符添加至字符串中 a=') print(a) print(type(a)) #1,2,3,4,5,6 #<class 'str'> split()能让我们用指定字符 ...
- LeetCode:罗马数字转整数【13】
LeetCode:罗马数字转整数[13] 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 10 ...