SSM的超详细整合

Spring和SpringMvc的整合

在web.xml文件中已经配置好前端控制器的情况下:

刚开始配置好前端控制器的情况下的web.xml文件:




<web-app>
  <display-name>Archetype Created Web Applicationdisplay-name>

 

  <filter>
    <filter-name>characterEncodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    
    <init-param>
      <param-name>encodingparam-name>
      <param-value>utf-8param-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>characterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
  
  

  <servlet>
    <servlet-name>dispacherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>


    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springmvc.xmlparam-value>


    init-param>


    <load-on-startup>1load-on-startup>
  servlet>


  <servlet-mapping>
    <servlet-name>dispacherServletservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

注意配置的顺序:listener在中文编码(filter过滤器)之后

加上配置



  org.springframework.web.context.ContextLoaderListener



  
    contextConfigLocation
    classpath:spring.xml
  

springmvc.xml配置文件:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       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.1.xsd
                       http://www.springframework.org/schema/mvc
                       http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
                       http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context-4.1.xsd">



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


   <mvc:annotation-driven/>

<mvc:default-servlet-handler/>


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

        <property name="prefix" value="/WEB-INF/pages/"/>

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

    bean>

beans>

Spring和Mybatis框架的整合

SqlMapConfig.xml文件:



<configuration>

    <typeAliases>

        <package name="com.hou.po"/>
    typeAliases>

configuration>
  

相当于把尽量把所有的Mybatis原有的配置文件的内容加入到spring.xml文件中,SqlSessionFactoryBean,db.properties文件引入,dataSource数据源的引入,dao层的扫描,Mybatis的核心配置文件的引入,mapper映射文件的引入。

spring.xml文件:



<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
     http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">


    <context:component-scan base-package="com.hou">

        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>



    <context:property-placeholder location="classpath:db.properties"/>



    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <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>


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

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

        
        <property name="configLocation" value="classpath:SqlMapConfig.xml"/>

     <property name="mapperLocations" value="classpath:mapper/*mapper.xml"/>

    bean>


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

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





beans>   

你可能感兴趣的:(ssm超详细整合)