SSM整合

SSM整合

一、框架整合方式

目前流行的框架整合方式:

SSM =Spring + SpringMVC + MyBatis 这是目前企业最为常用的整合框架组合。

SSH =Spring + Struts2 + Hibernate 这是早期的常用框架组合。

SSH2 =Spring + SpirngMVC + Hibernate,目前也有部分项目的框架组合方式。

二、SSM整合

SSM整合注意事项:SpringMVCSpring框架下的一个子集,所以在版本兼容性上不需要考虑,没有要求。同时,这两个框架在整合时与平时所学习的用法一样,不需要改变。整合时要动的地方是MyBatis框架与Spring框架的整合。

(一)Spring 框架要处理的地方:

IOC容器的创建,在Java项目中是有Main方法的,可以在Main方法中创建IOC容器,但是Web项目是没有main方法的,如何创建IOC容器?

Web项目的Web.xml配置文件加载一个监听器(ContextLoaderListener),随项目启动而启动,IOC容器在监听里面实现了创建的过程.

 <!-- 配置Spring框架的配置文件路径 -->

  <context-param>

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

    <param-value>classpath:config/applicationcontext.xml</param-value>

  </context-param>

  <!-- Spring 框架的入口,此监听器在项目启动时自动执行 -->

  <listener>

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

  </listener>

SpirngMVC的启动配置

<!-- Spring MVC 框架启动配置 -->

  <servlet>

    <servlet-name>springMVC</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

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

      <param-value>classpath:config/springMVC-servlet.xml</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>springMVC</servlet-name>

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

  </servlet-mapping>

Web.xml中,监听器先执行-> 过滤器->Servlet , 如果Servlet没有配置加载时启动,它是在第一次访问时才执行。

(二)MyBatis框架要动的地方是MyBatis运行时相关API的处理:

SqlSessionFactory工厂的对象创建由Spring框架如何实现?MyBatis在整合Spring时,需要用到一个MyBatisSpring整合的包:mybatis-spring-1.2.0.jar,这个包中有一个对象SqlSessionFactoryBean对象,替换以前的SqlSessionFactory对象。

SqlSessionFactoryBean 为整合应用提供SqlSession对象资源

MapperFactoryBean 很据指定Mapper接口生成Bean实例

MapperScannerConfigurer 很据指定包,扫描Mapper接口并生成实例

三、SSM整合步骤:

  1. 导入相关jar

      

aopalliance-1.0.0.jar

aspectjweaver-1.6.8.jar

commons-dbcp-1.4.jar

commons-logging-1.1.1.jar

commons-pool-1.5.3.jar

jstl.jar

mybatis-3.2.8.jar

mybatis-spring-1.2.0.jar

mysql-connector-java-5.1.38-bin.jar

spring-aop-4.3.10.RELEASE.jar

spring-aspects-4.3.10.RELEASE.jar

spring-beans-4.3.10.RELEASE.jar

spring-context-4.3.10.RELEASE.jar

spring-context-support-4.3.10.RELEASE.jar

spring-core-4.3.10.RELEASE.jar

spring-expression-4.3.10.RELEASE.jar

spring-jdbc-4.3.10.RELEASE.jar

spring-tx-4.3.10.RELEASE.jar

spring-web-4.3.10.RELEASE.jar

spring-webmvc-4.3.10.RELEASE.jar

spring-webmvc-portlet-4.3.10.RELEASE.jar

standard-1.1.2.jar

  1. 导入相关的配置文件

     Spring 配置文件,主要配置数据源及MyBatisAPI,事务管理

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

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

http://www.springframework.org/schema/aop

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

">

<!-- 启用注解配置 -->

<context:annotation-config />

<!-- 扫描包中的类,把有注解的类自动创建出对象,存储在IOC容器中 controller由SpringMVC框架处理, entity和dao由MyBatis处理,所以,只有Service和util归Spring来扫描 -->

<context:component-scan base-package="com.sgxy.service" />

<context:component-scan base-package="com.sgxy.util" />

<!-- 加载连接数据的的属性文件 -->

<bean id="propertyPlaceholderConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="location" value="classpath:config/jdbc.properties" />

</bean>

<!-- <context:property-placeholder location="classpath:config/jdbc.properties"/> -->

<!-- 配置数据源对象,此数据源用的是dbcp连接池 -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<!-- 连接数据库的基本配置 ${driver}读取到属性文件的键对应的值 -->

<property name="driverClassName" value="${driver}" />

<property name="url" value="${url}" />

<property name="username" value="${username}" />

<property name="password" value="${password}" />

<!-- 配置连接池相关属性,初始大小,最大连接数,最大空闲,最小空闲,最大空闲时间 -->

<property name="initialSize" value="${initialSize}" />

<property name="maxActive" value="${maxActive}" />

<property name="maxIdle" value="${maxIdle}" />

<property name="minIdle" value="${minIdle}" />

<property name="maxWait" value="${maxWait}" />

</bean>

<!-- SqlSessionFactoryBean对象的创建 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<!-- 创建工厂对象时需要的数据源 -->

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

<!-- MyBatis的主配置文件 -->

<property name="configLocation" value="classpath:config/mybatis-config.xml" />

<!-- MyBatis持久化接口的映射文件 -->

<property name="mapperLocations" value="classpath:com/sgxy/mapping/*.xml" />

<!-- 实体对象的别名处理 -->

<property name="typeAliasesPackage" value="com.sgxy.entity" />

</bean>

<!-- 创建持久化接口的代理对象,通过MapperScannerConfigurer对象,根据 sqlSessionFactory对象来创建某个包中的所有持久化接口的代理对象 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

<property name="basePackage" value="com.sgxy.dao"></property>

</bean>

<!-- 事务处理 -->

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

<bean id="transactionManager" 

    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

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

</bean>

<!-- 启用事务 -->

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

</beans>

SpringMVC 的配置文件:不变

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

    xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"

<!-- 启动注解 -->      

<mvc:annotation-driven/>

<!-- 扫描此包中所有的@Controller注解的类,创建这些类的对象,这些对象存放在IOC容器中。 -->

<context:component-scan base-package="com.sgxy.controller"/>

<!-- 静态资源处理 -->

<!-- <mvc:resources location="/images/" mapping="/images/**"/>

<mvc:resources location="/css/" mapping="/css/**"/>

<mvc:resources location="/js/" mapping="/js/**"/> -->

<!-- 静态资源的处理交给容器的Servlet来处理-->

<mvc:default-servlet-handler/> 

<!-- 配置视图解析器,把控制器的视图名称解析出具体视图文件路径.     

  bean节点表示创建一个class属性中表示的类的对象,对象命名为id属性中的值,此对象也是存放在IOC容器中

     比较在控制器中指定视图名称叫:gradeList,则映射出的视图文件路径为:  /gradeList.jsp ,就会去项目中拿视图文件

 -->

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <!-- 给对象的属性赋值 -->

        <property name="prefix" value="/"/>

        <property name="suffix" value=".jsp"/>

    </bean>    

</beans>

  MyBatis的配置文件,因为配置多在Spring的配置文件中创建SqlSessionFactoryBean对象时指定了,所以配置基本上没有了。

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

<!DOCTYPE configuration  

  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  

  "http://mybatis.org/dtd/mybatis-3-config.dtd">

<!-- dtd 文档就是对此文件的: 节点名称,节点数量,节点顺序,节点数据类型,节点的属性名....进行约束。-->

<configuration>  

    <!-- 所有的配置除settings对MyBatis框架自身的配置,其它的都可以移动到Spring中配置。 -->

    <settings>

      <setting name="cacheEnabled" value="true"/>

    </settings>

</configuration>

   

  1. Web.xml中启动Spring框架及SpringMVC框架

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

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

  <display-name>SSMDemo0307</display-name>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <!-- 配置Spring框架的配置文件路径 -->

  <context-param>

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

    <param-value>classpath:config/applicationcontext.xml</param-value>

  </context-param>

  <!-- Spring 框架的入口,此监听器在项目启动时自动执行 -->

  <listener>

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

  </listener>

  

  <!-- Spring MVC 框架启动配置 -->

  <servlet>

    <servlet-name>springMVC</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

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

      <param-value>classpath:config/springMVC-servlet.xml</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>springMVC</servlet-name>

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

  </servlet-mapping>

  

  <!-- SpringMVC 解决中文乱码的问题, -->

  <filter>

     <filter-name>characterFilter</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>

     <init-param>

       <param-name>forceEncoding</param-name>

       <param-value>true</param-value>

     </init-param>

  </filter>

  <filter-mapping>

     <filter-name>characterFilter</filter-name>

     <!-- /*  过滤到所有的请求  -->

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

  </filter-mapping>

</web-app>

  1. 项目的架构搭建,主要看业务层

package com.sgxy.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Isolation;

import org.springframework.transaction.annotation.Propagation;

import org.springframework.transaction.annotation.Transactional;

import com.sgxy.dao.GradeDao;

import com.sgxy.entity.Grade;

@Service   //Spring扫描

public class GradeService {

@Autowired // 直接从IOC容器中取代理对象

GradeDao dao;

@Transactional  //事务管理

public List<Grade> find() {

return dao.find();

}

}

原文地址:https://www.cnblogs.com/warriors-curry/p/10489391.html