ssh整合的配置

1.导入依赖

可以参考我的上一个博客:点击打开链接

2.在src下新建一个applicationContext.xml文件

3.建立一个user类、action类、userService类作为演示

user类

package com.mrchen.bean;

public class User {
	private Integer id;
	private String name;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

action类

package com.mrchen.bean;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{

	public String login() throws Exception {
		
		System.out.println("this is login");
		return SUCCESS;
	}
	
	
}

userService接口

package com.mrchen.service;

import com.mrchen.bean.User;

public interface UserService {
	User getUserById(Integer id);
}

userService实现类

package com.mrchen.service;

import com.mrchen.bean.User;

public class UserServiceImpl implements UserService {

	public User getUserById(Integer id) {
		System.out.println("userServiceImpl");
		return null;
	}

}

3.引入约束

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

</beans>

并将userServiceImpl、action配置到applicationContext.xml中

<bean name="userAction" class="com.mrchen.bean.UserAction" scope="prototype"></bean>
<bean name="userService" class="com.mrchen.service.UserServiceImpl"></bean>

4.单独整合spring到web中

在web.xml中添加
<!-- 让spring随web启动而创建监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

<!-- 配置spring配置文件参数 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

5.单独整合struts到web中

(1)在src下新建一个struts.xml,注意:名字不能错,不然会出现404,怎么找都找不到错误。

(2)在struts.xml中配置action信息

<struts>
	<package name="demo" namespace="/"	extends="struts-default">
		<action name="UserAction_*" class="com.mrchen.bean.UserAction" method="{1}">
			<result name="SUCCESS">/success.jsp</result>
		</action>
	</package>
</struts>

(3)建立上一步骤所需的success.jsp文件,(内容是什么不重要)

(4)在web.xml中配置struts2核心过滤器

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

6.spring和struts2

(1)更改struts2.xml常量

<constant name="struts.objectFactory" value="spring"></constant>

(2)将applicationContext.xml中的action配置name复制到struts.xml中的action中。

(3)这时候userAction依赖属性要手动添加了

<bean name="userAction" class="com.mrchen.bean.UserAction" scope="prototype">
		<property name="userService" ref="userService"></property>
	</bean>

7.单独整合hibernate

(1)在src下新建hibernate.cfg.xml

(2)配置

<session-factory>
    <!-- 配置链接 -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql:///spring_demo</property>
		<property name="connection.username">root</property>
		<property name="connection.password"></property>
    
    <!-- 配置方言 -->
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    
    <!-- 在启动时根据配置更新数据库 -->
	<property name="hbm2ddl.auto">update</property>
    
    <!-- 在控制台输出sql语句 -->
	<property name="show_sql">true</property>
    
    
    
    </session-factory>

8.整合hibernate与spring

(1)原理:将sessionFactory交给spring容器管理

(2)加载配置方案:applicationContext.xml

<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 配置hibernate基本信息 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
				<prop key="hibernate.connection.url">jdbc:mysql:///spring_demo</prop>
				<prop key="hibernate.connection.username">root</prop>
				<prop key="hibernate.connection.password"></prop>
			<!-- 配置方言 -->
                   <prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>

(3)将hibernate.cfg.xml中和applicationContext.xml重复的去掉

9.引入c3p0链接池

(1)在src下新建db.properties

(2)配置db.properties

jdbc.jdbcUrl=jdbc:mysql:///spring_demo
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=

(3)引入连接池到spring中:applicationContext.xml

<!-- 读取db.properties -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置c3p0连接池 -->
	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbc.jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="jdbc.driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbc.user" value="${jdbc.user}"></property>
		<property name="jdbc.password" value="${jdbc.password}"></property>
	</bean>

(4)将连接池注入sessionFactory

<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 注入链接池 -->
		<property name="dataSource" ref="dataSource"></property>
		
		<!-- 配置hibernate基本信息 -->
		<property name="hibernateProperties">
			<props>
				<!-- <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
				<prop key="hibernate.connection.url">jdbc:mysql:///spring_demo</prop>
				<prop key="hibernate.connection.username">root</prop>
				<prop key="hibernate.connection.password"></prop> -->
				<!-- 配置方言 -->
				<prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
				
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>

此时applicationContext.xml中的配置信息要去掉

10.扩大session作用范围

在web.xml中配置

<filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>






  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值