ssm整合原理

ssm是指Spring、SpringMVC、mybatis,现在web开发的三驾马车。

一,整合的目标是什么?

 

所谓的整合都是基于spring框架的,spring是一个java应用开发框架,最大的特点就是替开发者创建对象,是一个生产对象的大工厂。

java是面向对象的语言,简单来说,开发java应用程序典型步骤是:

① 创建一个类;

② 创建这个类的对象;

③ 调用这个类的方法。

在使用springmvc、mybatis框架时,本质上是要根据框架提供的类创建对象,然后调用对象的方法。

所以,ssm框架的整合,其实是把springmvc、mybatis整合到spring框架中,说的更简单一点,就是把之前由开发者创建springmvc、mybatis对象的工作交给spring。

而spring更强大的地方在于,整合之后,开发者能以更简单的方式使用这些框架。

比方说,单独使用mybatis,开发者需要通过SqlSession.getMapper方法拿到Mapper的代理对象,然后操作数据库。而使用整合到spring之后,直接将在需要使用Mapper的地方声明Mapper依赖,spring会自动注入代理对象。

二,整合步骤

提到具体的整合步骤,有几个问题需要考虑:

一是servlet规范下,MVC模式下,核心controller的声明和创建;

二是spring容器何时创建、如何创建。

上面这两个问题都和中间件有关,本文以tomcat为例。

1,springmvc的整合

 

当tomcat接收到请求后,会根据web.xml配置的映射关系,将请求转发给对应的servlet。

利用这个规则,springmvc提供了核心控制器,一般是DispatcherServlet,当DispatcherServlet接收到转发的请求后,调用请求映射器(HandlerMapping)和请求适配器(HandlerAdapter),找到对应的处理方法,之后将结果通过视图解析器处理之后作为响应返回。

而映射器、适配器、视图解析器都是spring创建的。

整合的第一步就是在web.xml中指定springmvc的核心控制器,在spring的配置文件中声明映射器、适配器和视图解析器。

在web.xml中指定springmvc的核心控制器:

<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*:wms/springmvc-servlet.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
     	<servlet-name>springmvc</servlet-name>
     	<url-pattern>/wms/*</url-pattern>
</servlet-mapping>

在spring的配置文件(applicationContext-springmvc.xml)中声明映射器、适配器和视图解析器:

<context:component-scan 
     base-package="com.rest" 
  	 name-generator="com.CustomBeanNameGenerator" 
/>

<!--映射器和适配器-->
<mvc:annotation-driven/>

<!--视图解析器-->
<bean class="org.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"></property>
</bean>

2,spring容器和创建

 

tomcat提供一系列监听器,监听tomcat容器的生命周期,利用这个规则,在tomcat容器启动时,创建spring容器。

在web.xml中配置监听器:

<listener>
	
    <listener-class>

        com.sinoservices.xframework.core.context.ContextLoaderListener
    
    </listener-class>
</listener>

在监听器中,创建spring容器:

public class ContextLoaderListener extends org.springframework.web.context.ContextLoaderListener {
    

    public ContextLoaderListener() {
    }

    // tomcat容器启动后,会调用该方法
    protected WebApplicationContext createWebApplicationContext(ServletContext var1) {
        
        // 创建spring容器
        ClasspathXmlApplicationContext var2 = new ClasspathXmlApplicationContext();
   }
}

3,mybatis的整合

 

spring整合mybatis需要考虑的是,如何创建SqlSessionFactory,这里先直接给出配置文件。

dao层的配置文件 applicationContext-dao.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: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-3.0.xsd  
     http://www.springframework.org/schema/context  
   http://www.springframework.org/schema/context/spring-context-3.0.xsd  
     http://www.springframework.org/schema/mvc  
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
    <context:component-scan base-package="com.tiantian.mybatis"/>  
    <context:property-placeholder location="classpath:config/jdbc.properties"/>  
    
    <!--整合第一步,配置数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
       destroy-method="close">  
       <property name="driverClassName" value="${jdbc.driver}" />  
       <property name="url" value="${jdbc.url}" />  
       <property name="username" value="${jdbc.username}" />  
       <property name="password" value="${jdbc.password}" />  
    </bean>  
   
    <!--整合第二步,配置sqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
       <property name="dataSource" ref="dataSource" />  
       <property name="mapperLocations" value="classpath:com/tiantian/mybatis/mapper/*.xml"/>  
       <property name="typeAliasesPackage" value="com.tiantian.mybatis.model" />  
    </bean>  
   
    <!--整合第三步,配置MapperBean-->
    <bean id="blogMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
       <property name="mapperInterface"  
           value="com.tiantian.mybatis.mapper.BlogMapper" />  
       <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
    </bean>  
   
</beans>

mybatis的整理本身比较简单,原理比较复杂,可以参考我的另一篇博客:深度分析:spring整合mybatis

 

发布了101 篇原创文章 · 获赞 15 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/epitomizelu/article/details/104726819