一、Spring整合Hibernate

  1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了;如果一个DAO类没有继承HibernateDaoSupport,需要有一个HibernateTemplate的属性,并且在配置文件中进行注入。注意,之前使用的是JdbcDaoSupport和JdbcTemplate,传递的是DataSource,现在使用的是HibernateDaoSupport和HibernateTemplate,传递的是SessionFactory。

  2.整合Spring整合Hibernate示例。

    (1)hibernate.cfg.xml配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <property name="connection.driver_class">
  8. com.mysql.jdbc.Driver
  9. </property>
  10. <property name="connection.username">root</property>
  11. <property name="connection.password">5a6f38</property>
  12. <property name="connection.url">
  13. jdbc:mysql://localhost:3306/test
  14. </property>
  15. <property name="show_sql">true</property>
  16. <property name="hbm2ddl.auto">update</property>
  17. <property name="dialect">
  18. org.hibernate.dialect.MySQLDialect
  19. </property>
  20. <property name="javax.persistence.validation.mode">none</property>
  21. <mapping resource="com/kdyzm/spring/hibernate/xml/Course.hbm.xml" />
  22. </session-factory>
  23. </hibernate-configuration>

hibernate.cfg.xml

    (2)几个类

  1. package com.kdyzm.spring.hibernate.xml;
  2.  
  3. import java.io.Serializable;
  4.  
  5. /*
  6. * 课程类
  7. */
  8. public class Course implements Serializable{
  9. private static final long serialVersionUID = 3765276226357461359L;
  10. private Long cid;
  11. private String cname;
  12.  
  13. public Course() {
  14. }
  15. @Override
  16. public String toString() {
  17. return "Course [cid=" + cid + ", cname=" + cname + "]";
  18. }
  19. public Long getCid() {
  20. return cid;
  21. }
  22. public void setCid(Long cid) {
  23. this.cid = cid;
  24. }
  25. public String getCname() {
  26. return cname;
  27. }
  28. public void setCname(String cname) {
  29. this.cname = cname;
  30. }
  31. }

com.kdyzm.spring.hibernate.xml.Course

  1. package com.kdyzm.spring.hibernate.xml;
  2.  
  3. public interface CourseDao {
  4. public Course getCourse(Long cid);
  5. public Course updateCourse(Course course);
  6. public Course deleteCourse(Course course);
  7. public Course addCourse(Course course);
  8. }

com.kdyzm.spring.hibernate.xml.CourseDao

    一个非常重要的类:com.kdyzm.spring.hibernate.xml.CourseDaoImpl

  1. package com.kdyzm.spring.hibernate.xml;
  2.  
  3. import org.springframework.orm.hibernate3.HibernateTemplate;
  4.  
  5. public class CourseDaoImpl implements CourseDao{
  6. //这里使用HibernateTemplate,而不是使用JdbcTemplate
  7. private HibernateTemplate hibernateTemplate;
  8. public HibernateTemplate getHibernateTemplate() {
  9. return hibernateTemplate;
  10. }
  11.  
  12. public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
  13. this.hibernateTemplate = hibernateTemplate;
  14. }
  15.  
  16. @Override
  17. public Course getCourse(Long cid) {
  18. return (Course) this.getHibernateTemplate().get(Course.class, cid);
  19. }
  20.  
  21. @Override
  22. public Course updateCourse(Course course) {
  23. this.getHibernateTemplate().update(course);
  24. return course;
  25. }
  26.  
  27. @Override
  28. public Course deleteCourse(Course course) {
  29. this.getHibernateTemplate().delete(course);
  30. return course;
  31. }
  32.  
  33. @Override
  34. public Course addCourse(Course course) {
  35. this.getHibernateTemplate().saveOrUpdate(course);
  36. return course;
  37. }
  38.  
  39. }

    这里使用HibernateTemplate作为成员变量,也可以继承HibernateDaoSupport类,效果是相同的。

  1. package com.kdyzm.spring.hibernate.xml;
  2.  
  3. public interface CourseService {
  4. public Course getCourse(Long cid);
  5. public Course updateCourse(Course course);
  6. public Course deleteCourse(Course course);
  7. public Course addCourse(Course course);
  8. }

com.kdyzm.spring.hibernate.CourseService

  1. package com.kdyzm.spring.hibernate.xml;
  2.  
  3. public class CourseServiceImpl implements CourseService{
  4. private CourseDao courseDao;
  5.  
  6. public CourseDao getCourseDao() {
  7. return courseDao;
  8. }
  9.  
  10. public void setCourseDao(CourseDao courseDao) {
  11. this.courseDao = courseDao;
  12. }
  13.  
  14. @Override
  15. public Course getCourse(Long cid) {
  16. return courseDao.getCourse(cid);
  17. }
  18.  
  19. @Override
  20. public Course updateCourse(Course course) {
  21. return courseDao.updateCourse(course);
  22. }
  23.  
  24. @Override
  25. public Course deleteCourse(Course course) {
  26. return courseDao.deleteCourse(course);
  27. }
  28.  
  29. @Override
  30. public Course addCourse(Course course) {
  31. return courseDao.addCourse(course);
  32. }
  33.  
  34. }

com.kdyzm.spring.hibernate.xml.CourseServiceImpl

    最后:测试代码

  1. ApplicationContext context=new ClassPathXmlApplicationContext("com/kdyzm/spring/hibernate/xml/applicationContext.xml");
  2. 2 CourseService courseService=(CourseService) context.getBean("courseService");
  3. 3 Course course = new Course();
  4. // course.setCid(11L);
  5. 5 course.setCname("赵日天");
  6. 6 courseService.addCourse(course);
  7. 7 course =new Course();
  8. 8 course.setCname("王大锤");
  9. //通过/0的异常测试事务回滚!
  10. // int a=1/0;
  11. 11 courseService.addCourse(course);

    运行结果:

    

    (3)com/kdyzm/spring/hibernate/xml/applicationContext.xml配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7.  
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  13. ">
  14. <!-- 将hibernate.cfg.xml配置文件导入进来 -->
  15. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  16. <property name="configLocation">
  17. <value>classpath:hibernate.cfg.xml</value>
  18. </property>
  19. </bean>
  20.  
  21. <!-- 程序员做的事情 -->
  22. <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
  23. <property name="sessionFactory" ref="sessionFactory"></property>
  24. </bean>
  25. <bean id="courseDao" class="com.kdyzm.spring.hibernate.xml.CourseDaoImpl">
  26. <property name="hibernateTemplate" ref="hibernateTemplate"></property>
  27. </bean>
  28.  
  29. <bean id="courseService" class="com.kdyzm.spring.hibernate.xml.CourseServiceImpl">
  30. <property name="courseDao">
  31. <ref bean="courseDao"/>
  32. </property>
  33. </bean>
  34.  
  35. <!-- Spring容器做的事情 -->
  36. <!-- 定义事务管理器 -->
  37. <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  38. <property name="sessionFactory" ref="sessionFactory"></property>
  39. </bean>
  40. <!-- 哪些通知需要开启事务模板 -->
  41. <tx:advice id="advice" transaction-manager="hibernateTransactionManager">
  42. <tx:attributes>
  43. <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
  44. </tx:attributes>
  45. </tx:advice>
  46. <!-- 配置切面表达式和通知 -->
  47. <aop:config>
  48. <aop:pointcut expression="execution(* com.kdyzm.spring.hibernate.xml.*ServiceImpl.*(..))" id="perform"/>
  49. <aop:advisor advice-ref="advice" pointcut-ref="perform"/>
  50. </aop:config>
  51. </beans>

  3.总结和分析

    (1)和使用JDBC的流程基本上是相同的,需要在配置文件中注入SessionFactory对象,注入HibernateTemplate对象。

    (2)以上的程序事务回滚没有实现!!!!原因不明

二、Spring整合Hibernate,使用注解的形式。

  1.在配置文件中使用spring的自动扫描机制。

  1. <context:component-scan base-package="com.kdyzm.spring.hibernate.xml"></context:component-scan>

  2.在配置文件中引入注解解析器(需要指定事务管理器)

  1. <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

  3.在service层通过@Transaction进行注解。

三、Struts2自定义结果集

  1.struts-default.xml文件中定义了一些结果集类型。

  1. <result-types>
  2. <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
  3. <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
  4. <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
  5. <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
  6. <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
  7. <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
  8. <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
  9. <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
  10. <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
  11. <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
  12. <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
  13. </result-types>

struts-default.xml配置文件中对结果集类型的定义

  2.我们可以通过实现Result接口或者继承StrutsResultSupport类自己定义结果集类型

    * 如果我们不需要跳转页面(使用了Ajax),则实现Result接口

     * 如果我们需要在业务逻辑处理完毕之后进行页面的跳转(重定向或者转发),则继承StrutsResultSupport类。

  3.自定义结果集小案例

    (1)自定义结果集类型com.kdyzm.struts2.myresult.MyResult.java,这里继承了StrutsResultSupport类,这里的代码模仿了DispatcherResult类中的写法。

       自定义结果集类型中的核心写法已经重点标注。

  1. package com.kdyzm.struts2.myresult;
  2.  
  3. import javax.servlet.RequestDispatcher;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6.  
  7. import org.apache.struts2.ServletActionContext;
  8. import org.apache.struts2.dispatcher.StrutsResultSupport;
  9.  
  10. import com.opensymphony.xwork2.ActionInvocation;
  11.  
  12. public class MyResult extends StrutsResultSupport{
  13. private static final long serialVersionUID = 8851051594485015779L;
  14.  
  15. @Override
  16. protected void doExecute(String finalLocation, ActionInvocation invocation)
  17. throws Exception {
  18. System.out.println("执行了自定义的 结果集类型!");
  19. HttpServletRequest request=ServletActionContext.getRequest();
  20. HttpServletResponse response = ServletActionContext.getResponse();
  21. RequestDispatcher requestDispatcher = request.getRequestDispatcher(finalLocation);
  22. requestDispatcher.forward(request, response);
  23. }
  24. }

    (2)测试Action:com.kdyzm.struts2.myresult.MyResultAction.java

  1. package com.kdyzm.struts2.myresult;
  2.  
  3. import com.opensymphony.xwork2.ActionSupport;
  4.  
  5. public class MyResultAction extends ActionSupport {
  6. private static final long serialVersionUID = -6710770364035530645L;
  7.  
  8. @Override
  9. public String execute() throws Exception {
  10. return super.execute();
  11. }
  12.  
  13. public String add() throws Exception{
  14. return "myresult";
  15. }
  16. }

    (3)局部配置文件com.kdyzm.strus2.myresult.myResult_struts.xml

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <package name="myResult" namespace="/myResultNamespace" extends="struts-default">
  7. <!-- 自定义结果集类型 -->
  8. <result-types>
  9. <result-type name="myResult" class="com.kdyzm.struts2.myresult.MyResult"></result-type>
  10. </result-types>
  11. <action method="add" name="myResultAction" class="com.kdyzm.struts2.myresult.MyResultAction">
  12. <result name="myresult" type="myResult">
  13. <param name="location">
  14. /main/index.jsp
  15. </param>
  16. </result>
  17. </action>
  18. </package>
  19. </struts>

    (4)classpath:struts.xml

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <!-- namespace一定要加上/,否则一般会报错! -->
  6. <struts>
  7. <package name="kdyzm" extends="struts-default" namespace="/kdyzm_namespace">
  8. <action name="helloAction" class="com.kdyzm.struts2.test.HelloWorldAction" method="execute">
  9. <result name="kdyzm_result">
  10. /main/index.jsp
  11. </result>
  12. </action>
  13. </package>
  14. <include file="com/kdyzm/struts2/myresult/myResult_struts.xml"></include></struts>

struts.xml

    (5)测试Jsp

  1. <a href="${pageContext.servletContext.contextPath}/myResultNamespace/myResultAction.action">
  2. 测试自定义结果集
  3. </a>

    (6)跳转到/main/index.jsp,显示出

      

四、SSH整合

  1.整合的第一步:导入jar包,在/WEB-INF/lib文件夹下,按照功能划分为几个文件夹,分别存放不同类型的jar包。

    * common:存放公共包

    * db:存放数据库驱动包

    * hibernate:存放hiberante相关包

    * junit:存放单元测试相关包

    * spring:存放spring相关包

    * struts2:存放struts2相关包

    尽可能的将jar包关联上源代码,便于代码追踪和书写。

  2.创建三个项目资源文件夹和对应的包

    (1)src:存放源代码

      * dao

      * dao.impl

      * service:

      * service.impl

      * struts.action

      * domain

    (2) config:存放配置文件

      * hibernate

        * hibernate.cfg.xml

      * spring

        * applicationContext-db.xml

        * applicationContext-person.xml

        * applicationContext.xml

      * struts2

        struts-user.xml

      struts.xml

    (3)test:单元测试

  3.SSH整合的jar包、关键类文件和配置文件

    (1)Spring和Hibernate的整合过程见前面的笔记。

    (2)关键的就是Spring和Struts2的整合

    (3)Spring和Struts2整合需要一个关键的jar包:struts-spring-plugin-x.x.x.jar,该jar包可以在struts2项目中的lib文件夹中找到。

    (4)在struts-spring-plugin.x.x.x.jar包中有一个非常重要的配置文件:struts-plugin.xml配置文件,该配置文件中的配置会覆盖掉struts-default.xml中的配置。

    (5)需要在struts.xml配置文件中进行如下配置:

  1. <constant name="struts.objectFactory" value="spring"></constant>

      进行这样的配置之前必须导入struts-spring-plugin.x.x.x.jar包。

      能够这样配置的依据是该jar包中的struts-plugin.xml配置文件中的配置:

  1. <bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />

    (6)怎样将Spring容器的初始化和Web服务器绑定在一起,使得服务器启动之后Spring容器就已经初始化成功。

      解决方案就是在web.xml配置文件中添加一项监听器的配置。

  1. <listener>
  2. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  3. </listener>
  4. <context-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>classpath:spring/applicationContext.xml</param-value>
  7. </context-param>

      其中param-name标签中的内容contextConfigLocation是固定字符串,不能更改。contextConfigLocation字符串的出处:

      

    (7)applicationContext.xml配置文件默认位置为:/WEB-INF/applicationContext.xml,通过XmlWebApplicationContext.xml配置文件就可以看出来。

      

      所以,如果applicationContext.xml在/WEB-INF目录下的话,就不需要再配置

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>classpath:spring/applicationContext.xml</param-value>
  4. </context-param>

      了。

五、整合模板代码

  https://github.com/kdyzm/day53_ssh_merge

  

【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】的更多相关文章

  1. Spring事务管理——回滚(rollback-for)控制

    探讨Spring事务控制中,异常触发事务回滚原理.文章进行了6种情况下的Spring事务是否回滚. 以下代码都是基于Spring与Mybatis整合,使用Spring声明式事务配置事务方法. 1.不捕 ...

  2. Spring事务管理回滚问题

    Spring事务管理不能回滚问题 在前段时间学习SpringMVC的练习中,碰到声明式事务管理时,事务不能回滚的情况,通过查看博客和资料,解决了问题. 原因 导致Spring事务管理不能回滚的原因有两 ...

  3. Spring事务异常回滚,捕获异常不抛出就不会回滚(转载) 解决了我一年前的问题

    最近遇到了事务不回滚的情况,我还考虑说JPA的事务有bug? 我想多了.......    为了打印清楚日志,很多方法我都加tyr catch,在catch中打印日志.但是这边情况来了,当这个方法异常 ...

  4. Spring事务不回滚原因分析

    Synchronized用于线程间的数据共享,而ThreadLocal则用于线程间的数据隔离. 在我完成一个项目的时候,遇到了一个Spring事务不回滚的问题,通过aspectJ和@Transacti ...

  5. Spring,SpringMvc配置常见的坑,注解的使用注意事项,applicationContext.xml和spring.mvc.xml配置注意事项,spring中的事务失效,事务不回滚原因

    1.Spring中的applicationContext.xml配置错误导致的异常 异常信息: org.apache.ibatis.binding.BindingException: Invalid ...

  6. spring@Transactional注解事务不回滚不起作用无效的问题处理

    这几天在项目里面发现我使用@Transactional注解事务之后,抛了异常居然不回滚.后来终于找到了原因. 如果你也出现了这种情况,可以从下面开始排查. 一.特性先来了解一下@Transaction ...

  7. Spring事务为什么不会自动回滚?Spring事务怎样才会自动回滚?事务自动回滚条件及手动回滚

    原文:https://blog.csdn.net/qq_32331073/article/details/76508147 更多Spring事务问题请访问链接:Spring事务回滚问题疑难详解 在此, ...

  8. 难道你还不知道Spring之事务的回滚和提交的原理吗,这篇文章带你走进源码级别的解读。

    上一篇文章讲解了获取事务,并通过获取的connection设置只读,隔离级别等:这篇文章讲事务剩下的回滚和提交. 事务的回滚处理 之前已经完成了目标方法运行前的事务准备工作.而这些准备工作的最大目的无 ...

  9. spring + myBatis 常见错误:注解事务不回滚

    最近项目在用springMVC+spring+myBatis框架,在配置事务的时候发现一个事务不能回滚的问题. 刚开始配置如下:springMVC.xml配置内容: spring.xml配置内容 从上 ...

  10. 抛出自定义异常,spring AOP事务不回滚的解决方案

    spring AOP 默认对RuntimeException()异常或是其子类进行事务回滚,也就是说 事务回滚:throw new RuntimeException("xxxxxxxxxxx ...

随机推荐

  1. NSString属性什么时候用copy,什么时候用strong?

           我们在声明一个NSString属性时,对于其内存相关特性,通常有两种选择(基于ARC环境):strong与copy.那这两者有什么区别呢?什么时候该用strong,什么时候该用copy呢 ...

  2. supermpa配置遇到的问题

    环境 vs2010  supermap idesktop7.1.2  iobject7.1.2.net windowform 问题 在安装iobject7.1.2 64位时 在vs中的工具箱是不显示s ...

  3. 统计单词数(WordCount)

    1.首先新建两个文件夹: 往文件夹添加内容: 2.启动hadoop-查看是否启动成功. 3.先对nameNode进行初始化. 4.查看hadoop下面有哪些文件. 5.在hadoop目录下创建inpu ...

  4. <<编程之美>>1.2读后有感

    问题提出 中国象棋的"将","帅"问题,他俩不能在一条直线上.求出他们的合法位置,并且只能用一个变量. 分析 一头雾水,不明所以.往下看了下,感觉像是程序员为难 ...

  5. python 单例模式

    单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时,单例对象的类必须保证只有一个实例存在 用装饰器方式实现单例模式 #!/usr/bin/python # coding=utf-8 d ...

  6. EFCore教程

    https://docs.microsoft.com/en-us/ef/core/modeling/alternate-keys aspnet core 教程 https://docs.microso ...

  7. 关于C中内存操作

     from:http://blog.csdn.net/shuaishuai80/article/details/6140979 malloc.calloc.realloc的区别 C Language ...

  8. CocoaPod 使用方法

    huangyichengdeMacBook-Pro:~ Jack$ pod search AFNetworking/Library/Ruby/Site/2.0.0/rubygems.rb:250:in ...

  9. java合集框架第一天

    文章目录 1 collection接口 2  list接口 3 Iterator 4 Vertor 5  ArrayList 6 LinkedList 主体部分: (1)collection Java ...

  10. apache 配虚拟主机转发到tomcat

    我用的是apache2.4.23, 连接tomcat使用自带的 proxy-ajp,需要开启相关模块 引用 http://www.server110.com/apache/201404/10273.h ...