学习后的SSH

关于SSH(Struts-Spring-Hibernate)整合相关信息及步骤

一、项目创建

1.新建Javaweb项目(只应用于Java编程)

学习后的SSH

1.1单击文件(file)-new-webproject

1.2.输入项目名和相关信息

学习后的SSH

二、环境配置

1.导入Struts相关jar包以及构建环境

1.1.鼠标点击当前的项目点击鼠标右键选择MyEclipse-addStrutscapab

学习后的SSH

1.2修改相关与项目所需信息

学习后的SSH

 

2.导入Spring相关jar包和构建Spring环境,步骤和Struts相似。

2.1为避免导入的jar包与Hibernate的jar包产生冲突,导入Spring jar包时注意:根据项目所涉及的信息导入所需的jar包,Spring.jaraspectjrt.jaraspectjweaver.jar常用的基本的jar包。

2.2导入日志文件log4j.propertiesapplicationContext.Xml信息配置文件。注意:改文件必须放在SRC源文件目录下。

3、导入Hibernate相关jar包和构建环

学习后的SSH

 

3.1导入Hibernate相关jar包和文件:hibernate.cfg.xml hibernate信息配置文件,

3.2新建一个文件存放项目配置的信息UserBean.hbm.xml

该文件完成映射数据库ORM操作。

以上所描述:SSH整合的环境已构建好!

 

三、根据业务所需完成相关内容

示例如下:完成对数据的简单增删查

1.创建DAO接口和接口实现类

import java.util.List;

 

import com.lovo.bean.MessageBean;

 

 

public interface IMessageDao {

public void save(MessageBean bean);

public void del(int id);

public List findAll();

}

2.创建业务接口和业务实现类。业务实现类中书写DAO接口变量,以便让spring注入

2.1创建业务接口

2.2 接口的实现

package com.lovo.dao.impl;

 

import java.util.List;

 

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

 

import com.lovo.bean.MessageBean;

import com.lovo.dao.IMessageDao;

 

public class MessageDaoImpl extends HibernateDaoSupport implements IMessageDao{

 

public void save(MessageBean bean) {

this.getHibernateTemplate().save(bean);

}

 

public void del(int id) {

Object obj this.getHibernateTemplate().get(MessageBean.classid);

this.getHibernateTemplate().delete(obj);

 

}

 

public List findAll() {

List list =this.getHibernateTemplate().find("from MessageBean");

return list;

}

}

3.配置Spring 的配置信息文件applicationContext.xml

详细介绍如下:

<?xml version="1.0" encoding="UTF-8"?>

 

<!--

  Application context definition for JPetStore's business layer.

  Contains bean references to the transaction manager and to the DAOs in

  dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").

  -->

<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:tx="http://www.springframework.org/schema/tx"

     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<!-- 配置SessionFactory (会话工厂)-->

<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

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

</bean>

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

<bean id="trans" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<!-- name="sessionFactory" 意思是管理绘话工厂 ref引用的是会话工厂sessionFactory-->

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

</bean>

<!-- 配置事物通知,配置那些方法需要使用事物管理器 (声明式事物)在这里声明了增删该查-->

<tx:advice id="myadvice" transaction-manager="trans">

<tx:attributes>

<tx:method name="save.*" propagation="REQUIRED"></tx:method>

 

<tx:method name="del.*" propagation="REQUIRED"></tx:method>

 

<tx:method name="update.*" propagation="REQUIRED"></tx:method>

 

<tx:method name="*" propagation="REQUIRED"></tx:method>

</tx:attributes>

</tx:advice>

<!-- 配置那些类需要事务管理(声明事物管理对象) -->

<!-- 面向切面,使用刀法 -->

<aop:config>

<aop:pointcut id="mycut" expression="execution(* com.lovo.service..*.*(..))"></aop:pointcut>

<!-- advice-ref:业务引用对象 pointcut-ref:切面引用 -->

<aop:advisor advice-ref="myadvice" pointcut-ref="mycut"></aop:advisor>

</aop:config>

<!-- 配置DAO数据访问对象 -->

<bean id="messageDao" class="com.lovo.dao.impl.MessageDaoImpl">

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

</bean>

<!-- 配置业务组件id业务组件识别号,class是对应的类路径 -->

<bean id="messageService" class="com.lovo.service.impl.MessageServiceImpl">

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

</bean>

<!-- 配置Action 必须使用name因为Path -->

<bean name="/message"class="com.lovo.struts.action.MessageAction"><propertyname="messageService"ref="messageService"></property>

</bean>

</beans>

4.编写ActionFormAction(此处省略)

5.applicationContext.xml中注册Action

<!-- 配置Action 必须使用name因为Path -->

<bean name="/message"class="com.lovo.struts.action.MessageAction"><propertyname="messageService"ref="messageService"></property>

</bean>

6.修改web.xml以便于在容器启动时加载applicationContext.xml文件

<filter>

   <filter-name>hibernateFilter</filter-name>

   <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

  </filter>

  <filter-mapping>

   <filter-name>hibernateFilter</filter-name>

   <url-pattern>*.do</url-pattern>

<dispatcher>FORWARD</dispatcher>

<dispatcher>REQUEST</dispatcher>

  </filter-mapping>

  <filter>

   <filter-name>charset</filter-name>

   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

   <init-param>

   <param-name>encoding</param-name>

   <param-value>UTF-8</param-value>

   </init-param>

  </filter>

  <filter-mapping>

   <filter-name>charset</filter-name>

   <url-pattern>/*</url-pattern>

  </filter-mapping>

  <listener>

  <!-- 监听上下文产生 -->

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

  </listener>

  <context-param>

   <!-- 上下文产生时候,读取spring的配置文件 -->

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

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

</context-param>

注意:通过以上相关描述与配置,SSH整合系数完成,每一个SSH整合的项目都应有这些步骤;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值