本笔记通过spring配置文件整合struts2以及hibernate
首先我们做一下准备工作
(1)向工程下的WebContent--lib下导入各类jar包
(2)引入struts2文件,引入hibernate文件,一般放在
(3)引入struts2配置文件:其中action中的class对应为spring管理下userAction实例化的对象id
(4)进入hibernate文件:节本配置与单独使用hibernate框架一直,其中不需要配置数据库相关信息,只需要配置一些基础hibernate操作设置
此处不能<!-- <property name="current_session_context_class">thread</property> -->开启sessionfactory
会报错spring和hibernate整合时异常(详解): getFlushMode is not valid without active transaction
其中指定配置文件和监听器配套使用在服务器开启时就开启spring配置文件的加载
<display-name>SSH_1</display-name>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<filter-name>struts2</filter-name>
<!-- 配置监听器 服务器启动是加载spring配置文件 -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<?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: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:///spring_ssh"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<!-- sessionFactory创建交给spring管理 -->
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 因为在hibernate核心配置文件中,没有数据库配置,数据库配置在spring里面配置,注入dataSource -->
<property name="dataSource" ref="dataSource"></property>
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="userAction" class="Tf.Ssh.demo.UserAction" scope="prototype">
<property name="service" ref="service"></property>
<bean id="service" class="Tf.Ssh.service.Ssh_Service">
<property name="dao" ref="userDaoImpl"></property>
<bean id="userDaoImpl" class="Tf.Ssh.dao.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
<!-- 创建hibernateTemplate对象 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
daoimpl书写各类实现方法操纵entity,对数据库的信息进行增删改查,并抽象接口与dao,并有service调用dao层进行业务逻辑处理
在配置文件体现(相当于 UserDao dao = new userDaoImpl())
<bean id="service" class="Tf.Ssh.service.Ssh_Service">
<property name="dao" ref="userDaoImpl"></property>
public void setDao(UserDao dao) {
System.out.println("service.....");
在配置文件体现(相当于 Service service = new Service())
<bean id="userAction" class="Tf.Ssh.demo.UserAction" scope="prototype">
<property name="service" ref="service"></property>
public class UserAction extends ActionSupport {
public void setService(Ssh_Service service) {
public String execute() throws Exception {
// TODO Auto-generated method stub