SSM整合

ssm整合主要有3个配置文件,web.xml、springmvc.xml和applicationContext.xml。

1.web.xml主要功能概述:
  • 指定spring的配置文件为classpath下的applicationContext.xml
  • 设置中文过滤器
  • 指定spring mvc配置文件为classpath下的springMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

  <!-- web.xml主要功能概述 -->
  <!-- 1.指定spring的配置文件为classpath下的applicationContext.xml -->
  <!-- 2.设置中文过滤器 -->
  <!-- 3.指定spring mvc配置文件为classpath下的springMVC.xml -->

  <!-- spring的配置文件 -->
  <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>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>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- spring mvc核心:分发servlet -->
  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- spring mvc的配置文件 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springMVC.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
    <!-- /* 可以拦截一切路径,包括index、index.jsp、idnex.html等都可以被拦截 -->
    <!-- / 只能拦截url请求路径,也就是index这种,带有后缀名的就不能被拦截  -->
  </servlet-mapping>

</web-app>

2.springmvc.xml主要功能概述:
  • 启动注解识别
  • 自动扫描controller包
  • 配置映射器与适配器
  • 开通静态资源访问
  • 视图定位
  • 文件上传
<?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.2.xsd">

    <!-- 启动注解的识别 -->
    <context:annotation-config/>

    <!--自动扫描该包,使SpringMVC认为包下用了@Controller注解的类是控制器-->
    <context:component-scan base-package="com.ljf.tmall.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!--配置映射器与适配器-->

    <!--因为<mvc:annotation-driven/>是用来配置RequestMappingHandlerMapping和RequestMappingHandlerAdapter两个bean,
    前者用于将URL映射到@RequestMapping注解的方法上,后者用于处理@Controller;
    但是不进行配置也没关系,不进行配置的话springMVC会自动加载DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter,也能处理*.do请求,
    但是现在这两个组件已经被官方标记为过时了,不推荐使用
    -->
    <mvc:annotation-driven />

    <!-- 开通静态资源的访问 -->
    <!-- html是静态文件,需在springMVC.xml中添加如下静态解析方法才能访问到-->
    <mvc:default-servlet-handler />

    <!-- 视图定位 -->
    <!--定义跳转的文件的前后缀,视图模式配置-这里的配置个人理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个可用的url地址-->
    <!-- 方法参数能够被ViewResolver修改 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView" />
        <!-- 把视图约定在 /WEB-INF/jsp/*.jsp 位置 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 对上传文件的解析 :开放上传功能的支持 -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
</beans>

3.applicationContext.xml主要功能概述:
  • 启动注解识别
  • 自动扫描service包
  • 导入数据库配置文件
  • 配置数据库连接池
  • 整合Spring和MyBatis
  • Mybatis的Mapper文件识别
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- 启动对注解的识别-->
    <context:annotation-config />

    <!-- 自动扫描service包-->
    <context:component-scan base-package="com.ljf.tmall.service" />

    <!-- 导入数据库配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <!-- 配置数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 基本属性 url、user、password -->
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!--druid连接池中配置driver的属性叫"driverClassName"-->
        <!--配置成<property name="driverClassName" value="${jdbc.driver}"/>即可,不配置也可以是因为druid会根据url自动识别数据库类型-->

        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />
        <property name="maxActive" value="20" />

        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="60000" />

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />

        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000" />

        <property name="validationQuery" value="SELECT 1" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />

        <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize"
                  value="20" />
    </bean>

    <!-- Mybatis的SessionFactory配置 -->
    <!--Spring和MyBatis完美整合,不需要mybatis的配置映射文件-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.ljf.tmall.pojo" />
        <property name="dataSource" ref="dataSource" />
        <!--自动扫描mapping.xml文件-->
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
        <!-- 分页插件,目前先注释,后面重构的时候才会用到 -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>

                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <!--Mybatis的Mapper文件识别-->
    <!--DAO接口所在的包名,Spring会自动查找其下的类-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ljf.tmall.mapper"/>
    </bean>

</beans>
ssm项目目录结构.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述 此代码是在matlab平台上搭建了一个简单的toolbox,用来实现生成对抗性网络(Generative A...
    JzCh阅读 15,049评论 27赞 3
  • 在来青岛之前,也就前三天,做的攻略都是重庆、成都,无奈在友人的提醒下,翻看了天气预报,每天都是雨雨雨,翻了一圈想去...
    图洼里的歪in阅读 276评论 0赞 0
  • 烈日下 我看见赤焰灼灼 狰狞的灵魂在咆哮 一丝不挂的躯体在狂欢 世界瞬间天昏地暗 坟墓中的躯壳抖了又抖 挣扎着试图...
    落荒岛阅读 355评论 2赞 5
  • 早上我正在写作业,突然听到一阵急促的敲门声,我打开门一看一个包裹正中我的怀里,一个黑色的背影一溜烟的跑了...
    邬文敬阅读 386评论 0赞 1

友情链接更多精彩内容