SSM整合

SSM是SpringMVC、Spring和Mybatis的整合,实现了MVC设计模式。整合主要涉及数据源、sessionFactory和session的管理,以及Spring对业务对象和SpringMVC的协调。配置步骤包括Mybatis、Spring和SpringMVC的配置文件设置,如数据源、事务管理、拦截器、视图解析等,以及web.xml的初始化配置。整合的目的是统一管理,提高开发效率。
摘要由CSDN通过智能技术生成

1.SSM整合到底整合什么,整合原理是什么?

目录

1.SSM整合到底整合什么,整合原理是什么?

1.1、SS整合原理M

1.2、SSM到底整合了什么

2、SSM实现步骤

2.1、mybatis的主配置文件

2.2、spring的配置文件

2.3、springMVC的配置文件

2.4、web.xml


1.1、SSM整合原理

SSM框架是spring MVC,spring和mybatis框架的整合,是标准的MVC模式,将整个系统分为表现层,controller(控制)层,service业务层,mapper层四层。

使用spring MVC负责请求的转发和视图管理

spring实现业务对象管理,mybatis作为数据对象的持久引擎

1.2、SSM到底整合了什么

spring和mybatis整合(数据源>sessionFactory>session)

spring和springMVC整合

为什么要整合在一起?

统一指挥、让两个框架中用到的类和对象,都让spring管理。好处:无缝衔接、用到spring提供的很多工具。由谁来指挥?

2、SSM实现步骤

 选择maven-archetype-webapp勾选从原型创建

2.1、mybatis的主配置文件

  Mybatis配置文件。

    这个配置文件中没有必须要添加的内容。因为他最主要的内容:数据源连接信息和子配置文件Mapper信息都会再spring的配置文件中体现。

    但是我们一般会配置以下内容:

<configuration> 
      <settings> 
          <!-- 关闭懒加载模式 --> 
          <setting name="lazyLoadingEnabled" value="false" /> 
      </settings> 
     <typeAliases> 
         <!--这里给实体类取别名,方便在mapper配置文件中使用-->
         <package name="cn.smbms.pojo"/>
     </typeAliases>
</configuration>

2.2、spring的配置文件

     springMVC配置文件

    这个配置文件的内容要怎么记忆?首先我们要清楚springMVC的作用是什么。因为springMVC就是用来做页面之间的跳转和数据传递的。所以他的主配置文件的内容都是和这两个方面相关的。

    另外springMVC是spring的一个子框架,所以springMVC和spring之间不需要整合。

 
    <context:component-scan base-package="cn.smbms.controller"/>
    <mvc:annotation-driven>
        <mvc:message-converters>
            <!-- 这是一个消息转换器 如果返回的值是文本类型的 解决控制层到视图层的中文乱码问题-->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <!-- 这是一个消息转换器 如果返回的是实体类或者集合 解决控制层到视图层的中文乱码问题-->
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json</value>
                    </list>
                </property>
                <property name="features">
                    <list>
                       <!-- Date的日期转换器 -->
                      <value>WriteDateUseDateFormat</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    
    <mvc:resources location="/statics/" mapping="/statics/**"></mvc:resources>
    
    <!-- 配置多视图解析器:允许同样的内容数据呈现不同的view -->
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="favorParameter" value="true"/>
        <property name="defaultContentType" value="text/html"/>
        <property name="mediaTypes">
            <map>
                <entry key="html" value="text/html;charset=UTF-8"/>
                <entry key="json" value="application/json;charset=UTF-8"/>
                <entry key="xml" value="application/xml;charset=UTF-8"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
                    <property name="prefix" value="/WEB-INF/jsp/"/>
                    <property name="suffix" value=".jsp"/>
                </bean>
            </list>
        </property>
    </bean>
     
    <!-- 配置拦截器interceptors -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/sys/**"/>
            <bean class="cn.smbms.interceptor.SysInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
    
    
    <!-- 配置MultipartResolver,用于上传文件,使用spring的CommonsMultipartResolver --> 
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
         <property name="maxUploadSize" value="5000000"/>
         <property name="defaultEncoding" value="UTF-8"/>
    </bean>

2.3、springMVC的配置文件

spring的配置文件

之前我们已经明白了,spirngMVC不需要spring刻意的去整合,所以spirng中最主要的内容就是整合mybatis。那mybatis我们到底是要整合哪些内容呢?

首先我们要明白mybatis的最根本的目的是要执行sql语句。而sql语句写在Mapper子配置文件中,sql语句最终要再数据库中执行,并且有些sql是需要有事务管理的。

所以spring中首先要把数据源管理起来,还需要把mybaits的主配置文件和子配置文件Mapper管理起来,管理这两样用的Ioc。

接下来我们需要使用AOP在业务逻辑层切入自带的事务管理器。

最后就是把业务逻辑层的bean也加入进来。

<context:component-scan base-package="cn.smbms.service"/>
   
    <!-- 读取数据库配置文件 -->
    <context:property-placeholder location="classpath:database.properties"/>
   
    <!-- 获取数据源(使用dbcp连接池) --> 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
            <property name="driverClassName" value="${driver}" /> 
            <property name="url" value="${url}" /> 
            <property name="username" value="${user}" /> 
            <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}"/>
            <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
            <property name="removeAbandoned" value="${removeAbandoned}"/>

            <!-- sql 心跳 -->
            <property name= "testWhileIdle" value="true"/>
            <property name= "testOnBorrow" value="false"/>
            <property name= "testOnReturn" value="false"/>
            <property name= "validationQuery" value="select 1"/>
            <property name= "timeBetweenEvictionRunsMillis" value="60000"/>
            <property name= "numTestsPerEvictionRun" value="${maxActive}"/>
    </bean>
   
    
    
    <!-- 配置mybitas SqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

        
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
         <property name="basePackage" value="cn.smbms.dao" /> 

    </bean>
   
    <!-- AOP 事务处理 开始 -->  
<!-- 数据源管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean> 
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager"> 
        <tx:attributes> 
           <tx:method name="smbms*"  propagation="REQUIRED"  rollback-for="Exception"  />
<tx:method name="select*"  read-only="true"  rollback-for="Exception"  />
<tx:method name="add*"  propagation="REQUIRED"  rollback-for="Exception"  />
<tx:method name="update*"  propagation="REQUIRED"  rollback-for="Exception"  />
<tx:method name="del*"  propagation="REQUIRED"  rollback-for="Exception"  />
        </tx:attributes> 
    </tx:advice>

    <aop:aspectj-autoproxy />
    <aop:config  proxy-target-class="true">
        <aop:pointcut expression="execution(* *cn.smbms.service..*(..))" id="transService"/>
        <aop:advisor pointcut-ref="transService" advice-ref="txAdvice" />
    </aop:config>
    <!-- AOP 事务处理 结束 -->

2.4、web.xml

web.xml。上面我们配置好了三个框架,框架配置的再好,也得有初始化的地方。自然初始化的地方就是在web.xml中。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-*.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>


  <filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
  </context-param>
  <listener>
    <listener-class>
            org.springframework.web.util.Log4jConfigListener
    </listener-class>
  </listener>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值