• 背景:

  基于之前两篇文章《Spring(三):Spring整合Hibernate》、《Spring(四):Spring整合Hibernate,之后整合Struts2》,了解了如何整合SSH的过程,但还不知道整合后在项目中该怎么开发使用,本文主要讲解的是基于SSH实现Employee信息查询功能的使用。

  • 新建Employee,Department实体类,并添加对应类的hibernate实体配置文件

  新建包com.dx.ssh.entities,在该包下创建Employee、Department实体类,并添加对应的hibernate实体配置文件Employee.hbm.xml、Department.hbm.xml

Employee.java

  1. package com.dx.ssh.entities;
  2.  
  3. public class Department {
  4. private Integer id;
  5. private String deparmentName;
  6.  
  7. public Integer getId() {
  8. return id;
  9. }
  10.  
  11. public void setId(Integer id) {
  12. this.id = id;
  13. }
  14.  
  15. public String getDeparmentName() {
  16. return deparmentName;
  17. }
  18.  
  19. public void setDeparmentName(String deparmentName) {
  20. this.deparmentName = deparmentName;
  21. }
  22. }

Employee.hbm.xml

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <!-- Generated 2017-5-9 16:33:32 by Hibernate Tools 3.5.0.Final -->
  5. <hibernate-mapping>
  6. <class name="com.dx.ssh.entities.Department" table="SSH_DEPARTMENT">
  7. <id name="id" type="java.lang.Integer">
  8. <column name="ID" />
  9. <generator class="native" />
  10. </id>
  11. <property name="deparmentName" type="java.lang.String">
  12. <column name="DEPARMENTNAME" />
  13. </property>
  14. </class>
  15. </hibernate-mapping>

Department.java

  1. package com.dx.ssh.entities;
  2.  
  3. import java.util.Date;
  4.  
  5. public class Employee {
  6. private Integer id;
  7. private String lastName;
  8. private String email;
  9. private Date birth;
  10. // 不能被修改
  11. private Date createTime;
  12. // n:1的关系
  13. private Department department;
  14.  
  15. public Integer getId() {
  16. return id;
  17. }
  18.  
  19. public void setId(Integer id) {
  20. this.id = id;
  21. }
  22.  
  23. public String getLastName() {
  24. return lastName;
  25. }
  26.  
  27. public void setLastName(String lastName) {
  28. this.lastName = lastName;
  29. }
  30.  
  31. public String getEmail() {
  32. return email;
  33. }
  34.  
  35. public void setEmail(String email) {
  36. this.email = email;
  37. }
  38.  
  39. public Date getBirth() {
  40. return birth;
  41. }
  42.  
  43. public void setBirth(Date birth) {
  44. this.birth = birth;
  45. }
  46.  
  47. public Date getCreateTime() {
  48. return createTime;
  49. }
  50.  
  51. public void setCreateTime(Date createTime) {
  52. this.createTime = createTime;
  53. }
  54.  
  55. public Department getDepartment() {
  56. return department;
  57. }
  58.  
  59. public void setDepartment(Department department) {
  60. this.department = department;
  61. }
  62. }

Department.hbm.xml

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <!-- Generated 2017-5-9 16:33:32 by Hibernate Tools 3.5.0.Final -->
  5. <hibernate-mapping>
  6. <class name="com.dx.ssh.entities.Employee" table="SSH_EMPLOYEE">
  7. <id name="id" type="java.lang.Integer">
  8. <column name="ID" />
  9. <generator class="native" />
  10. </id>
  11. <property name="lastName" type="java.lang.String">
  12. <column name="LASTNAME" />
  13. </property>
  14. <property name="email" type="java.lang.String">
  15. <column name="EMAIL" />
  16. </property>
  17. <property name="birth" type="java.util.Date">
  18. <column name="BIRTH" />
  19. </property>
  20. <property name="createTime" type="java.util.Date">
  21. <column name="CREATETIME" />
  22. </property>
  23. <!-- 单项N:1关联关系 -->
  24. <many-to-one name="department" class="com.dx.ssh.entities.Department">
  25. <column name="DEPARTMENT_ID" />
  26. </many-to-one>
  27. </class>
  28. </hibernate-mapping>
  • 添加Dao、Service、Action,并配置相关配置文件

添加包com.dx.ssh.dao,并新建EmployeeDao.java

  1. package com.dx.ssh.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import org.hibernate.Session;
  6. import org.hibernate.SessionFactory;
  7.  
  8. import com.dx.ssh.entities.Employee;
  9.  
  10. public class EmployeeDao {
  11. private SessionFactory sessionFactory;
  12.  
  13. public void setSessionFactory(SessionFactory sessionFactory) {
  14. this.sessionFactory = sessionFactory;
  15. }
  16.  
  17. private Session getSession() {
  18. return this.sessionFactory.getCurrentSession();
  19. }
  20.  
  21. public List<Employee> getAll() {
  22. String hql = "From Employee e LEFT OUTER JOIN FETCH e.department";
  23. return getSession().createQuery(hql).list();
  24. }
  25. }

在com.dx.ssh.service包下,新建EmployeeService.java

  1. package com.dx.ssh.service;
  2.  
  3. import java.util.List;
  4.  
  5. import com.dx.ssh.dao.EmployeeDao;
  6. import com.dx.ssh.entities.Employee;
  7.  
  8. public class EmployeeService {
  9. private EmployeeDao employeeDao;
  10.  
  11. public void setEmployeeDao(EmployeeDao employeeDao) {
  12. this.employeeDao = employeeDao;
  13. }
  14.  
  15. public List<Employee> getAll() {
  16. List<Employee> items = employeeDao.getAll();
  17.  
  18. return items;
  19. }
  20. }

在com.dx.ssh.actions下,新建Action类EmployeeAction.java

  1. package com.dx.ssh.service;
  2.  
  3. import java.util.List;
  4.  
  5. import com.dx.ssh.dao.EmployeeDao;
  6. import com.dx.ssh.entities.Employee;
  7.  
  8. public class EmployeeService {
  9. private EmployeeDao employeeDao;
  10.  
  11. public void setEmployeeDao(EmployeeDao employeeDao) {
  12. this.employeeDao = employeeDao;
  13. }
  14.  
  15. public List<Employee> getAll() {
  16. List<Employee> items = employeeDao.getAll();
  17.  
  18. return items;
  19. }
  20. }

配置applicationContext-beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd">
  3. <bean id="employeeDao" class="com.dx.ssh.dao.EmployeeDao">
  4. <property name="sessionFactory" ref="sessionFactory"></property>
  5. </bean>
  6.  
  7. <bean id="employeeService" class="com.dx.ssh.service.EmployeeService">
  8. <property name="employeeDao" ref="employeeDao"></property>
  9. </bean>
  10.  
  11. <bean id="employeeAction" class="com.dx.ssh.actions.EmployeeAction" scope="prototype">
  12. <property name="employeeService" ref="employeeService"></property>
  13. </bean>
  14. </beans>

配置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. <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  7. <constant name="struts.devMode" value="true" />
  8.  
  9. <package name="default" namespace="/" extends="struts-default">
  10. <action name="emp-*" class="employeeAction" method="{1}">
  11. <result name="list">/WEB-INF/views/emp-list.jsp</result>
  12. </action>
  13. </package>
  14. </struts>

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:tx="http://www.springframework.org/schema/tx"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  5. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
  7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
  8.  
  9. <!-- 加载配置文件 -->
  10. <context:property-placeholder location="classpath:jdbc.properties" file-encoding="utf-8" ignore-unresolvable="true" />
  11.  
  12. <!-- 配置数据源 -->
  13. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  14. <property name="driverClass" value="${jdbc.driverClassName}"></property>
  15. <property name="jdbcUrl" value="${jdbc.url}"></property>
  16. <property name="user" value="${jdbc.username}"></property>
  17. <property name="password" value="${jdbc.password}"></property>
  18. <property name="testConnectionOnCheckout" value="${jdbc.c3p0.testConnectionOnCheckout}"></property>
  19. <property name="testConnectionOnCheckin" value="${jdbc.c3p0.testConnectionOnCheckin}"></property>
  20. <property name="idleConnectionTestPeriod" value="${jdbc.c3p0.idleConnectionTestPeriod}"></property>
  21. <property name="initialPoolSize" value="${jdbc.c3p0.initialPoolSize}"></property>
  22. <property name="minPoolSize" value="${jdbc.c3p0.minPoolSize}"></property>
  23. <property name="maxPoolSize" value="${jdbc.c3p0.maxPoolSize}"></property>
  24. <property name="maxIdleTime" value="${jdbc.c3p0.maxIdleTime}"></property>
  25. </bean>
  26.  
  27. <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  28. <!-- 数据源dataSource -->
  29. <property name="dataSource" ref="dataSource" />
  30.  
  31. <!-- hibernate的配置方案一 -->
  32. <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
  33. <!-- hibernate的配置方案二 -->
  34. <!--
  35. <property name="hibernateProperties">
  36. <props>
  37. <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
  38. <prop key="hibernate.dialect">${hibernate.dialect}</prop>
  39. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
  40. <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
  41. <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
  42. </props>
  43. </property>
  44. -->
  45.  
  46. <!-- 持久化类的位置方案一,通过包进行扫描 -->
  47. <property name="mappingLocations" value="classpath:com/dx/ssh/entities/*.hbm.xml"></property>
  48. <!-- spring的spring.jar的jar包内,在org.springframework.orm.hibernate3.annotation下,
  49. 有一个AnnotationSessionFactoryBean类,其中有一个属性叫做"packagesToScan", 有个方法叫setpackagesToScan(),
  50. 也就是说我可以再spring里面将这个属性给设定上。
  51. packagesToScan是"包扫描"的意思,哪些包spring可以给我们扫描一下,看看有哪些实体类,
  52. 这一项在我们在配置文件中配置hibernate的实体类的时候可以这么配,只要给出具体的扫描范围就可以了,
  53. 不需要将实体类一个一个的写出来
  54. -->
  55. <!-- 持久化类的位置方案二,通过包进行扫描 -->
  56. <!--
  57. <property name="packagesToScan">
  58. <list>
  59. <value>com.dx.ssh.entities</value>
  60. </list>
  61. </property>
  62. -->
  63. <!-- 持久化类的位置方案三,通过类进行扫描 -->
  64. <!--
  65. <property name="annotatedClasses">
  66. <list>
  67. <value>com.dx.ssh.entities.Member</value>
  68. <value>com.dx.ssh.entities.Log</value>
  69. </list>
  70. </property>
  71. -->
  72. </bean>
  73. <!-- 配置Hibernate事务管理器 -->
  74. <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  75. <property name="sessionFactory" ref="sessionFactory" />
  76. </bean>
  77.  
  78. <!-- 配置事务异常封装
  79. <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
  80. -->
  81. <!-- 基于数据源的事务管理器 -->
  82. <!--
  83. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  84. <property name="dataSource" ref="dataSource"></property>
  85. </bean>
  86. -->
  87. <!-- 启用注解来管理事务 -->
  88.  
  89. <!-- 配合<tx:advice>和<aop:advisor>完成了事务切面的定义 -->
  90. <!-- 使用强大的切点表达式是语言轻松定义目标方法 -->
  91. <aop:config proxy-target-class="true">
  92. <!-- 通过aop定义事务增强切面 -->
  93. <aop:pointcut expression=" execution(* com.dx.ssh.service..*(..))" id="serviceMethod" />
  94. <!-- 引用事务增强 -->
  95. <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
  96. </aop:config>
  97. <!-- 事务增强 -->
  98. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  99. <!-- 事务属性定义 -->
  100. <tx:attributes>
  101. <tx:method name="*" />
  102. </tx:attributes>
  103. </tx:advice>
  104.  
  105. <!-- 第一种方式: 注解方式配置事物
  106. <tx:annotation-driven transaction-manager="transactionManager" />
  107. -->
  108. </beans>

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. <!--
  8. 把一下配置信息转移到jdbc.properties中。
  9. <property name="hibernate.connection.username">root</property>
  10. <property name="hibernate.connection.password">123456</property>
  11. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  12. <property name="hibernate.connection.url">jdbc:mysql://localhost/my_ssh</property>
  13. -->
  14.  
  15. <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  16. <property name="hibernate.show_sql">true</property>
  17. <property name="hibernate.format_sql">true</property>
  18. <property name="hibernate.hbm2ddl.auto">update</property>
  19. <!-- <property name="hibernate.current_session_context_class">thread</property> -->
  20. </session-factory>
  21. </hibernate-configuration>

jdbc.properties

  1. #-----------------------------------------------------
  2. # \u6570\u636E\u5E93\u914D\u7F6E
  3. #-----------------------------------------------------
  4. #\u670D\u52A1\u5668\u5730\u5740
  5. host=127.0.0.1
  6. dbName=my_ssh
  7. jdbc.driverClassName=com.mysql.jdbc.Driver
  8. jdbc.url=jdbc:mysql://${host}:3306/${dbName}
  9. jdbc.username=root
  10. jdbc.password=123456
  11.  
  12. #-----------------------------------------------------
  13. # \u9002\u7528\u4E8Ec3p0\u7684\u914D\u7F6E
  14. #-----------------------------------------------------
  15. #-----------------------------------------------------
  16. # c3p0\u53CD\u7A7A\u95F2\u8BBE\u7F6E\uFF0C\u9632\u6B628\u5C0F\u65F6\u5931\u6548\u95EE\u989828800
  17. #-----------------------------------------------------
  18. #idleConnectionTestPeriod\u8981\u5C0F\u4E8EMySQL\u7684wait_timeout
  19. jdbc.c3p0.testConnectionOnCheckout=false
  20. jdbc.c3p0.testConnectionOnCheckin=true
  21. jdbc.c3p0.idleConnectionTestPeriod=3600
  22. #-----------------------------------------------------
  23. # c3p0\u8FDE\u63A5\u6C60\u914D\u7F6E
  24. #-----------------------------------------------------
  25. #initialPoolSize, minPoolSize, maxPoolSize define the number of Connections that will be pooled.
  26. #Please ensure that minPoolSize <= maxPoolSize.
  27. #Unreasonable values of initialPoolSize will be ignored, and minPoolSize will be used instead.
  28. jdbc.c3p0.initialPoolSize=10
  29. jdbc.c3p0.minPoolSize=10
  30. jdbc.c3p0.maxPoolSize=100
  31. #maxIdleTime defines how many seconds a Connection should be permitted to go unused before being culled from the pool.
  32. jdbc.c3p0.maxIdleTime=3600
  33. #-----------------------------------------------------
  34. # hibernate\u8FDE\u63A5\u6C60\u914D\u7F6E
  35. #-----------------------------------------------------
  36. hibernate.connection.driverClass=com.mysql.jdbc.Driver
  37. hibernate.connection.url=jdbc:mysql://${host}:3306/${dbName}
  38. hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
  39. hibernate.show_sql=true
  40. hibernate.format_sql=true
  41. hibernate.hbm2ddl.auto=update

在WebContent下创建index.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <a href="emp-list">Employee List</a>
  11. </body>
  12. </html>

在WEB-INF下创建views文件夹,在views下创建emp-list.jsp页面

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@taglib prefix="s" uri="/struts-tags"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <s:if test="#request.employees==null || #request.employees.size()==0">
  12. 暂无记录!
  13. </s:if>
  14. <s:else>
  15. <table border="1" cellpadding="10" cellspacing="0">
  16. <tr>
  17. <td>ID</td>
  18. <td>LastName</td>
  19. <td>Email</td>
  20. <td>BirthDay</td>
  21. <td>CreateTime</td>
  22. <td>Department Name</td>
  23. <tr>
  24. <s:iterator value="#request.employees">
  25. <tr>
  26. <td>${id }</td>
  27. <td>${lastName }</td>
  28. <td>${email }</td>
  29. <td>${birth }</td>
  30. <td>${createTime }</td>
  31. <td>${department.deparmentName }</td>
  32. <tr>
  33. </s:iterator>
  34. </table>
  35. </s:else>
  36. </body>
  37. </html>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"
  3. version="3.0">
  4. <display-name>My-SSH</display-name>
  5.  
  6. <welcome-file-list>
  7. <welcome-file>index.jsp</welcome-file>
  8. <welcome-file>index</welcome-file>
  9. </welcome-file-list>
  10.  
  11. <!-- 配置启动IOC容器的Listener -->
  12. <!-- needed for ContextLoaderListener -->
  13. <context-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>classpath:applicationContext*.xml</param-value>
  16. </context-param>
  17.  
  18. <!-- Bootstraps the root web application context before servlet initialization -->
  19. <listener>
  20. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  21. </listener>
  22.  
  23. <!-- Struts2 filter -->
  24. <filter>
  25. <filter-name>struts2</filter-name>
  26. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  27. </filter>
  28.  
  29. <filter-mapping>
  30. <filter-name>struts2</filter-name>
  31. <url-pattern>/*</url-pattern>
  32. </filter-mapping>
  33.  
  34. </web-app>

项目结构如下:

  • 测试结果:

  • 常见抛出异常情况:

1、nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

错误原因:缺少aspectjweaver-1.8.9.jar包导致的错误。

从地址http://repo1.maven.org/maven2/org/aspectj/aspectjweaver/,下载包aspectjweaver-1.8.9.jar。

2、Action class [userAction] not found - action

在做一个 ssh三个框架集成的时候,经常遇到上诉问题。如果已经正确定义了action,仍报错,其实是缺少包struts2-sping-plugin 2.0.1.jar

解决方案一:

上诉问题说的 在struts.xml 没用找到 sping配置文件里的一个bean啊;也就是说:struts.xml 这个文件没有和sping配置文件关联起来;

在此 另一种解决方案是 在 lib 目录下加入一个jar包struts2-sping-plugin 2.0.1.jar问题就解决了.

解决方案二:

在struts.xml中还要加入这么一个bean

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

这个bean要放在package的外面

3、Struts Problem Report

Struts has detected an unhandled exception:

Messages:
  • createQuery is not valid without active transaction
File: org/hibernate/context/internal/ThreadLocalSessionContext.java
Line number: 351

解决方案:

包hibernate.cfg.xml中的配置项注意掉:

  1. <!-- <property name="hibernate.current_session_context_class">thread</property> -->

4、hibernate中采用了lazy加载,在查询hql=“From Employee e”,执行过程中dao被service调用,当action调用employeeService结束后,就立即关闭了session,导致页面没有版本加载数据导致的错误。

 Struts Problem Report

Struts has detected an unhandled exception:

Messages:
  1. could not initialize proxy - no Session
  2. Error reading 'deparmentName' on type com.dx.ssh.entities.Department_$$_jvst9ef_0
  3. javax.el.ELException: Error reading 'deparmentName' on type com.dx.ssh.entities.Department_$$_jvst9ef_0
File: org/hibernate/proxy/AbstractLazyInitializer.java
Line number: 146

解决该错误有三种解决方案:

解决方案一:修改Employee.hbm.xml man-to-one节点添加上属性 lazy="false",比较笨的方法。因为这是在用效率作为代价。

  1. <!-- 单项N:1关联关系 -->
  2. <many-to-one name="department" class="com.dx.ssh.entities.Department" lazy="false">
  3. <column name="DEPARTMENT_ID" />
  4. </many-to-one>

解决方案二:使用OpenSessionInViewFilter。这种方法是将session交给servlet filter来管理,每当一个请求来之后就会开

启一个session,只有当响应结束后才会关闭。如下:

  1. <filter-name>hibernateFilter</filter-name>
  2. <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class>
  3. </filter
  4. <filter-mapping>
  5. <filter-name>hibernateFilter</filter-name>
  6. <url-pattern>/*</url-pattern>
  7. </filter-mapping>

解决方案三:修改hql

  1. public List<Employee> getAll() {
  2. String hql = "From Employee e LEFT OUTER JOIN FETCH e.department";
  3. return getSession().createQuery(hql).list();
  4. }

My-SSH项目源代码下载地址:http://pan.baidu.com/s/1o87Ezge

Spring(五):Spring&Struts2&Hibernate整合后,实现查询Employee信息的更多相关文章

  1. spring3 hibernate4整合后无法查询数据库

    spring3和hibernate4整合后无法查询数据库,一方面是因为已经spring3中没有对hibernate4 HibernateTemplate的支持,另外一个就是需要在hibernate的配 ...

  2. SSH框架之Spring+Struts2+Hibernate整合篇

    回顾 -Hibernate框架 ORM: 对象关系映射.把数据库表和JavaBean通过映射的配置文件映射起来, 操作JavaBean对象,通过映射的配置文件生成SQL语句,自动执行.操作数据库. 1 ...

  3. [Java web]Spring+Struts2+Hibernate整合过程

    摘要 最近一直在折腾java web相关内容,这里就把最近学习的spring+struts2+hibernate进行一个整合,也就是大家经常说的ssh. 环境 工具IDE :Idea 2018 数据库 ...

  4. struts2+hibernate整合-实现登录功能

    最近一直学习struts2+hibernate框架,于是想把两个框架整合到一起,做一个小的登录项目.其他不多说,直接看例子. 1).Struts2 和hibernate的环境配置 包括jar包.web ...

  5. [Java web]Spring+Struts2+Hibernate整合过程(2)

    摘要 上篇文章介绍了一种整合方式,不妨就叫做有hibernate配置文件的方式,这里介绍一种不用hibernate.cfg.xml的一种配置方式,为了方便,就仍在上篇的demo中,继续修改了. 步骤 ...

  6. spring+struts2+hibernate整合

    web.xml需要配置 <context-param> <param-name>contextConfigLocation</param-name> <par ...

  7. (Spring+IBatis+Struts1+Struts2+Hibernate+Java EE+Oracle)

    原文出处:http://space.itpub.net/6517/viewspace-609654 1.Spring架构图 Spring是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的 ...

  8. Spring、SpringMVC、Hibernate整合 ----超详细教程

    一.数据库表 /* Navicat MySQL Data Transfer Source Server : 本地连接 Source Server Version : 50720 Source Host ...

  9. struts2+hibernate整合开发步骤

    百度的各种代码,步骤,自己整合了一下 1,创建数据库 常用mysql   creat table..... 2,在WebContent下的bin中添加相应的包 http://pan.baidu.com ...

随机推荐

  1. C语言第七次博客作业--一二维数组

    一.PTA实验作业 题目1:找鞍点 1. 本题PTA提交列表 2. 设计思路 定义n,i,j,ii,jj,a[7][7],flag,max 输入n for i=0 to i=n for j=0 to ...

  2. 笔记:Maven 项目基本配置

    Maven 的基本设置包含项目基本信息和项目信息,基本信息主要用于设置当前包的归属项目.当前项目等,配置文件结构如下: <project xmlns="http://maven.apa ...

  3. Vue之九数据劫持实现MVVM的数据双向绑定

    vue是通过数据劫持的方式来做数据绑定的,其中最核心的方法便是通过Object.defineProperty()来实现对属性的劫持,达到监听数据变动的目的. 如果不熟悉defineProperty,猛 ...

  4. CorelDraw X8 破解激活问题

    在为X8使用特殊辅助手段激活时,通过菜单“帮助”-“产品详细信息”页中的“我有序列号”链接打开对话框,输入序列号激活. 然而,由于安装时要求联网登陆,很有可能获取了试用序列号,导致点击链接后,输入对话 ...

  5. 远程通信的几种选择(RPC,Webservice,RMI,JMS的区别)

    RPC(Remote Procedure Call Protocol) RPC使用C/S方式,采用http协议,发送请求到服务器,等待服务器返回结果.这个请求包括一个参数集和一个文本集,通常形成&qu ...

  6. 【Python】 压缩文件处理 zipfile & tarfile

    [zipfile] 虽然叫zipfile,但是除了zip之外,rar,war,jar这些压缩(或者打包)文件格式也都可以处理. zipfile模块常用的一些操作和方法: is_zipfile(file ...

  7. 记一次mysql故障处理

    突然间,个人网站崩溃了!相信这个报错作为运维都应该清楚的,是数据库宕机了. 数据库我采用mysql 5.1.63,上机查看错误日志: 171010 10:11:01 mysqld_safe Start ...

  8. python+pycahrm+windows环境准备

    python安装教程和Pycharm安装详细教程 首先我们来安装python 1.首先进入网站下载:点击打开链接(或自己输入网址https://www.python.org/downloads/),进 ...

  9. [HNOI2002]营业额统计_Treap

    [HNOI2002]营业额统计 题目大意:给你一串n数序列,对于每一个刚输入的数a,找到一个前面的数k,使得|a-k|最小. 注释:$n<=32767,ai<=10^6$. 想法:刚学Tr ...

  10. java 对象和封装

    软件出现的目的     面向对象设计和开发程序的好处用计算机语言描述现实世界    交流更加流畅用计算机解决现实世界的问题   提高设计和开发效率 面向对象的思想    描述→ 面向对象的世界     ...