SSM的超详细整合

Spring和SpringMvc的整合

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

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


<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
<!--  配置spring.xml-->
 
<!--  配置中文编码-->
  <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的监听器和spring产生关联-->
  

  <servlet>
    <servlet-name>dispacherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.dispatcherServlet</servlet-class>

<!--    加载springmvc.xml配置文件-->
    <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>dispacherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

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

加上配置

<!--  配置spring的监听器和spring产生关联-->
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--  配置spring.xml-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>

springmvc.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: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">


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

<!--    使springmvc.xml文件的相关功能生效-->
   <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文件

<?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>
<!-- 配置别名-->
    <typeAliases>
<!--     通过包扫描的方式进行配置-->
        <package name="com.hou.po"/>
    </typeAliases>

</configuration>
  

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

spring.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: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">

<!--    扫描所有的Service层,Dao层。,不扫描Controller层-->
    <context:component-scan base-package="com.hou">
<!--不扫描的注解Contoller层-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


<!--   加载数据库的db文件-->
    <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>

<!-- 配置Mybatis的session工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.sqlSessionfactorybean">
<!--引用数据源-->
        <property name="dataSource" ref="dataSource"/>

        <!--        引入Mybatis的核心配置文件-->
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
<!--        引入mapper配置文件-->
     <property name="mapperLocations" value="classpath:mapper/*mapper.xml"/>

    </bean>

<!--    配置Mapper相关的扫描器 使dao层生效-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

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





</beans>   

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念