SSM整合



一、各框架所需要的属性

1. web.xml

  1. 启动Spring容器:<context-param>

    • <param-name>contextConfigLocation</param-name>
    • <param-value>classpath:Spring配置文件</param-value>
  2. 配置监听器:<listener>

    • <listener-class>ContextLoaderListener</listener-class>
  3. 字符编码过滤器
    设置字符编码:encoding
    设置请求字符编码:forceRequestEncoding
    设置响应字符编码:forceResponseEncoding

  4. 使用Rest风格的URI:HiddenHttpMethodFilter

  5. 前端发送PUT请求(如:AJAX),需对发送的数据进行包装:HttpPutFormContentFilter

  6. springmvc的前端控制器:DispatcherServlet

    • 指定SpringMVC的配置文件
    • 随着服务器的启动而启动

2. Spring

  • Spring管理业务层和持久层的对象(Service和dao),这些对象放在Spring的配置文件中,交给Spring的容器管理。
  1. 组件扫描 :<context:component-scan/>Controller
  2. properties文件引入: <context:property-placeholder/>
  3. 数据源:<bean>
    • C3P0:ComboPooledDataSource
    • 德鲁伊:DruidDataSource
  4. 与MyBatis整合
    • 注册SqlSessionFactoryBean,目的是创建SqlSessionFactory
      • 指定mybatis全局配置文件的位置:<property name="configLocation" value=>
      • 注入数据源:<property name="dataSource" ref=>
      • 指定mapper文件位置:<property name="mapperLocations" value=>
    • 注册SqlSessionTemplate,目的是创建sqlSession
      • 以下操作为有参构造 使用:<constructor-arg>
      • 注入sqlSessionFactory
      • 设置executorType属性为 BATCH
    • 配置扫描器,将mybatis接口的实现加入到ioc容器中:MapperScannerConfigurer
      • 扫描所有dao接口的实现:<property name="basePackage" value=>
  5. 事务控制
    • 注册DataSourceTransactionManager,目的是创建transactionManager(事务管理器)
      • 注入数据源
    • 使用xml配置形式的事务: <aop:config>
      • 切入点表达式:<aop:pointcut id="txPoint" expression="">
      • 配置事务增强:<aop:advisor advice-ref="事务增强id" pointcut-ref="txPoint"/>
    • 配置事务增强:<tx:advice id="txAdvice" transaction-manager="事务管理器id">
      • 配置事务增强属性:<tx:attributes>
        • 所有方法都是事务方法:<tx:method name="*"/>
        • 以get为头的方法都是只读:<tx:method name="get*" read-only="true"/>


3. SpringMVC

  • SpringMVC:web层,相当于controller(相当于struts的action)主要进行页面的;请求接受与响应。
  • SpringMVC是视图层(UI)的框架,把视图使用的对象交给SpringMVC容器管理,放在SpringMVC的配置文件中。
  1. 组件扫描,只扫描控制器Controller
    • 注意关掉自动扫描:use-default-filters="false"
  2. 配置视图解析器
  3. 配置默认Servlet
  4. mvc注解驱动

以上是必须配置,下面可以根据需求配置

  1. 拦截器
  2. 视图控制器:view-controller
  3. 文件上传:HiddenHttpMethodFilter
  4. 异常处理器


4. MyBatis

  1. 全局配置:<settings>
    • 驼峰命名映射:<setting name="mapUnderscoreToCamelCase" value="true"/>
  2. 别名设置:<typeAliases>
  3. 分页查询:<plugins>



二、对应配置文件

1. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--指定Spring配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application.xml</param-value>
    </context-param>

    <!--该监听器将根据contextConfigLocation参数加载Spring配置文件, 初始化Spring应用上下文-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--过滤器-->
    <!--配置字符编码过滤器,一定要放在所有过滤器之前,/* 表示拦截所有请求-->
    <filter>
        <filter-name>CharacterEncodingFilter</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>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 配置HTTP方法过滤器, 实现REST风格的增删改查 -->
    <!-- 该过滤器根据指定的请求参数(默认是"_method")来修改请求的类型 -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!--HttpPutFormContentFilter过滤器会自动的封装前台传递过来的PUT请求的参数-->
    <filter>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--2、配置 Spring MVC 的前端控制器,拦截所有请求-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <!--指定SpringMVC的配置文件-->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!--随着tomcat的启动-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

2. application.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: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">


    <!--1、配置扫描包,不扫描controller-->
    <context:component-scan base-package="com.study.ssm">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--2、引入数据库驱动的properties文件-->
    <context:property-placeholder location="classpath:dbconfig.properties"/>

    <!--3、配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${jdbc.url}"></property>
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--4、与 MyBatis 的整合配置-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--指定数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--指定 MyBatis 全局配置文件的位置-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!--指定 MyBatis 映射文件的位置-->
        <property name="mapperLocations" value="classpath:com/study/ssm/dao/*.xml"></property>
    </bean>

    <!--配置一个可以执行批量操作的 sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
        <constructor-arg name="executorType" value="BATCH"></constructor-arg>
    </bean>

    <!--配置扫描器,将 MyBatis 接口的实现加入到 IOC 容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--扫描所有的 DAO 接口的实现,加入到 IOC 容器中-->
        <property name="basePackage" value="com.study.ssm.dao"></property>
    </bean>

    <!--4、事务控制-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--指定数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--开启基于注解的事务,使用 XML 配置形式的事务(比较主要的事务都用配置式)-->
    <aop:config>
        <!--切入点表达式-->
        <aop:pointcut id="txPoint" expression="execution(* com.study.ssm.service..*(..))"/>
        <!--配置事务增强-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"></aop:advisor>
    </aop:config>

    <!--配置事务增强,事务如何增强-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">

        <tx:attributes>
            <!--
                 * 代表所有方法都是事务方法
                 get* 代表以get开始的所有方法
            -->
            <tx:method name="*"/>
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
</beans>

3. spring-mvc.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.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Spring MVC 的配置文件,主要包含网站跳转逻辑的控制、配置-->
    <context:component-scan base-package="com.study.ssm" use-default-filters="false">
        <!--只扫描控制器-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

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

    <!--将 Spring MVC 不能处理的请求交给 tomcat 处理-->
    <mvc:default-servlet-handler/>

    <!-- 开启mvc注解驱动 -->
    <!--能支持 Spring MVC 更高级的一些功能,比如 JSR 303 校验、快捷的 Ajax 映射动态请求等-->
    <mvc:annotation-driven/>

</beans>

4. mybatis-config.xml

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

    <settings>
        <!--驼峰命名映射-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>


    <typeAliases>
        <!--给Java Bean起别名-->
        <package name="com.study.ssm.bean"/>
    </typeAliases>


    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!--分页查询合理化-->
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>
</configuration>

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇
下一篇>>