spring+hibernate+struts2整合

本笔记通过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

(5)配置过滤器在web.xml下

其中包括struts2过滤器--监听器--

其中指定配置文件和监听器配套使用在服务器开启时就开启spring配置文件的加载

<display-name>SSH_1</display-name>

<!-- 指定配置文件的位置 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

<!-- 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配置文件 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

(6)配置spring配置文件

<?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">

<!-- 配置c3p0连接池 -->

<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>

</bean>

<!-- sessionFactory创建交给spring管理 -->

<bean id="sessionFactory"

class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

<!-- 因为在hibernate核心配置文件中,没有数据库配置,数据库配置在spring里面配置,注入dataSource -->

<property name="dataSource" ref="dataSource"></property>

<!-- 指定使用hibernate核心配置文件 -->

<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>

</bean>

<!-- 配置事务管理器 -->

<bean id="transactionManager"

class="org.springframework.orm.hibernate5.HibernateTransactionManager">

<!-- 注入事务管理器 -->

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

<!-- 开启事务注解 -->

<tx:annotation-driven transaction-manager="transactionManager" />

<!-- 配置action的对象 -->

<bean id="userAction" class="Tf.Ssh.demo.UserAction" scope="prototype">

<!-- 注入service -->

<property name="service" ref="service"></property>

</bean>

<!-- 配置service對象 -->

<bean id="service" class="Tf.Ssh.service.Ssh_Service">

<property name="dao" ref="userDaoImpl"></property>

</bean>

<!-- 配置实现类的对象userdaoimpl -->

<bean id="userDaoImpl" class="Tf.Ssh.dao.UserDaoImpl">

<property name="hibernateTemplate" ref="hibernateTemplate"></property>

</bean>

<!-- 创建hibernateTemplate对象 -->

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">

<!-- 注入sessionFactory -->

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

</beans>

(7)java编程,主要基于mvc思想

在之前书写的基础上添加注解,

daoimpl书写各类实现方法操纵entity,对数据库的信息进行增删改查,并抽象接口与dao,并有service调用dao层进行业务逻辑处理

action进行请求

(1)package Tf.Ssh.service

(1)着重强调关于注解调用其他类

具有接口的配置

在配置文件体现(相当于 UserDao dao = new userDaoImpl()

<bean id="service" class="Tf.Ssh.service.Ssh_Service">

<property name="dao" ref="userDaoImpl"></property>

</bean>

                     在java程序中提现

@Transactional

public class Ssh_Service {

//注入dao实现结合配置文件

private UserDao dao;

public void setDao(UserDao dao) {

this.dao = dao;

}

public void Add(){

System.out.println("service.....");

dao.add();

}

普通配置

在配置文件体现(相当于 Service service = new Service())

在配置文件中体现

<bean id="userAction" class="Tf.Ssh.demo.UserAction" scope="prototype">

<!-- 注入service -->

<property name="service" ref="service"></property>

</bean>

在java程序中提现

 public class UserAction extends ActionSupport {

    //注入service     

private Ssh_Service service;

public void setService(Ssh_Service service) {

this.service = service;

}

@Override

public String execute() throws Exception {

// TODO Auto-generated method stub

System.out.println("action.......");

service.Add();

return NONE;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值