SSH整合

关于SSH整合,采用两两整合的方式,struts和spring整合,hibernate和spring整合。

整合环境:JDK 1.8

Tomcat 9

struts 2.3

hibernate 5.2

spring 4.3

第一步:整合struts和spring

1.导入sturts和spring的jar包(加入struts和spring的整合包struts2-spring-plugin-2.5.10.jar)

2.新建UserAction、UserService、UserDao,把UserDao封装成UserService的属性,把UserService封装成UserAction的属性,以备调用方法。

UserAction代码:

package com.lee.action;

import com.lee.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
	private UserService userService;
	//调用默认方法
	public String execute() throws Exception {
		System.out.println("action执行。。。。。。");
		userService.countMoney();
		return SUCCESS;
	}
	
	
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
}

UserService代码:

package com.lee.service;

import com.lee.dao.UserDao;

public class UserService {
	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	

	public void countMoney(){
		System.out.println("service执行。。。。");
		userDao.add();
	}
}

UserDao代码:

package com.lee.dao;

public class UserDao {
	
	public void add(){
		System.out.println("dao执行。。。。");
	}
}


3.在web.xml中配置struts过滤器和spring的监听器

   <!-- 指定spring配置文件的位置,默认配置在WEB-INF目录下 -->  
   <context-param>  
       <param-name>contextConfigLocation</param-name>  
       <param-value>classpath:ApplicationContextSSH.xml</param-value>
  </context-param>
  
  <!-- 配置struts过滤器 -->
    <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>
  
    
  	<!-- 配置监听器 -->
  	<listener>  
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
   </listener>  


4.配置struts.xml(这里有个问题,struts.devMode设置为true会报错,不知道为什么)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <!-- 开发者模式 -->
    <constant name="struts.devMode" value="false" />
   
    <package name="font" namespace="/" extends="struts-default">
    	
    	<action name="UserAction" class="userAction">
    		<result>index.jsp</result>
    	</action>
    	
    </package>
    
</struts>

5.配置spring的ApplicationContext.xml

<?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:tx="http://www.springframework.org/schema/tx"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
                  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
                  http://www.springframework.org/schema/context  
                  http://www.springframework.org/schema/context/spring-context-3.2.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-3.2.xsd"> 
	
	<!-- UserDao的bean配置 -->
	<bean id="userDao" class="com.lee.dao.UserDao"></bean>
	<!-- UserService的bean配置 -->	
	<bean id="userService" class="com.lee.service.UserService">
		<!-- 注入UserDao -->
		<property name="userDao" ref="userDao" />
	</bean>	

	<!-- 配置struts的action创建 -->
	<bean id="userAction" class="com.lee.action.UserAction" scope="prototype">
		<!-- 注入UserService -->
		<property name="userService" ref="userService"></property>
	</bean>
</beans>


6.启动服务器访问action,控制台正常输出


第二步、spring和hibernate的整合

1.导入hibernate的jar包(还需要spring关于orm的jar包spring-orm-4.3.10.RELEASE.jar)

2.配置hibernate核心文件和bean映射文件

实体类:

package com.lee.bean;

public class User {
	private String userid;
	private String name;
	private String password;
	private int money;
	
	
	//set,get方法省略
	
}

映射文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD//EN" 
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<!-- name配置实体对象所在的类,table为数据库对应的表名-->
    <class name="com.lee.bean.User" table="user">
		<id name="userid"><!-- 配置主键,可能数据库表中没有主键,这里需要指定 -->
			<generator class="assigned" /><!-- 主键生产策略 -->
		</id>
		<!-- 实体类的属性 -->
		<property name="name" />
		<property name="password" />
		
		<property name="money" />
	</class>

</hibernate-mapping>

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<!-- 配置数据库方言 -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 控制台显示hibernate的sql -->
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		
		<!-- 引入映射文件 -->
		<mapping resource="com/lee/bean/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>
关于核心配置文件的数据源配置交给spring管理,其实hibernate核心文件的内容可以全部由spring来管理。

3.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:tx="http://www.springframework.org/schema/tx"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
                  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
                  http://www.springframework.org/schema/context  
                  http://www.springframework.org/schema/context/spring-context-3.2.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-3.2.xsd"> 
	
	<!-- 配置c3p0代替hibernate核心配置文件中的数据源配置 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"/>
		<property name="jdbcUrl" value="jdbc:mysql:///sun" />
		<property name="user" value="root" />
		<property name="password" value="sunshine" />
	</bean>
	
	<!-- hibernate的sessionFactory交给spring创建 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 由于hibernate的核心配置文件没有数据源的配置,这里需要配置 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- sessionFaction的创建需要读取hibernate的核心配置文件 -->
		<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
	</bean>
	
	<!-- 配置hibernateTemplate用于操作数据(spring对hibernate操作数据的封装) -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
		<!-- hibernateTemplate的有参构造需要传入sessionFactory对象-->
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<!-- 事务中需要指定所要操作的数据库信息,数据源,这里可以配置sessionFactory,里面包含了DataSource -->
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 开起事务管理器注解扫描 (指定事务管理器)-->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	
	<!-- 配置struts的action创建 -->
	<bean id="userAction" class="com.lee.action.UserAction" scope="prototype">
		<!-- 注入UserService -->
		<property name="userService" ref="userService"></property>
	</bean>
	
	<!-- userService的创建 -->
	<bean id="userService" class="com.lee.service.UserService">
		<!-- 注入userDao -->
		<property name="userDao" ref="userDao" />
	</bean>	
	
	<!-- userDao的创建 -->
	<bean id="userDao" class="com.lee.dao.UserDao">
		<!-- userDao通过hibernateTemplate操作数据库 -->
		<property name="hibernateTemplate" ref="hibernateTemplate" />
	</bean>	
	
	
</beans>

4.action中代码不变,service代码(通过注解形式配置事务)

package com.lee.service;


import org.springframework.transaction.annotation.Transactional;

import com.lee.dao.UserDao;

@Transactional
public class UserService {
	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	
	//模拟转账过程
	public void saveUser(){
		System.out.println("service执行。。。。");
		userDao.save();
	}
}

4.UserDao代码

package com.lee.dao;

import org.springframework.orm.hibernate5.HibernateTemplate;

import com.lee.bean.User;

public class UserDao {
	private HibernateTemplate hibernateTemplate;
	
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void save(){
		System.out.println("dao执行。。。。");
		User user = new User();
		user.setUserid("cindy");
		user.setName("cindy.liu");
		user.setPassword("123");
		user.setMoney(12312);
		hibernateTemplate.save(user);
	}
}


利用save方法保存一个对象。

5.启动服务器,访问action地址,完成数据保存



至此,SSH整合完成。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值