我们在使用spring+springMvc+mybatis整合框架做项目的搭建过程:

    首先我们在pom.xml中配置几个常用的配置jar文件

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    2. <modelVersion>4.0.0</modelVersion>
    3. <groupId>com.zhiyou100</groupId>
    4. <artifactId>SpringMybatisWork</artifactId>
    5. <version>0.0.1-SNAPSHOT</version>
    6. <packaging>war</packaging>
    7. <dependencies>
    8. <dependency>
    9. <groupId>org.springframework</groupId>
    10. <artifactId>spring-webmvc</artifactId>
    11. <version>4.1.3.RELEASE</version>
    12. </dependency>
    13. <dependency>
    14. <groupId>org.apache.tomcat</groupId>
    15. <artifactId>dbcp</artifactId>
    16. <version>6.0.29</version>
    17. </dependency>
    18. <dependency>
    19. <groupId>org.apache.tomcat</groupId>
    20. <artifactId>tomcat-dbcp</artifactId>
    21. <version>7.0.47</version>
    22. </dependency>
    23. <dependency>
    24. <groupId>mysql</groupId>
    25. <artifactId>mysql-connector-java</artifactId>
    26. <version>5.1.32</version>
    27. </dependency>
    28. <dependency>
    29. <groupId>org.mybatis</groupId>
    30. <artifactId>mybatis-spring</artifactId>
    31. <version>1.2.2</version>
    32. </dependency>
    33. <dependency>
    34. <groupId>org.apache.tomcat</groupId>
    35. <artifactId>tomcat-jdbc</artifactId>
    36. <version>7.0.47</version>
    37. </dependency>
    38. <dependency>
    39. <groupId>org.springframework</groupId>
    40. <artifactId>spring-jdbc</artifactId>
    41. <version>4.1.3.RELEASE</version>
    42. </dependency>
    43. <dependency>
    44. <groupId>org.springframework</groupId>
    45. <artifactId>spring-context</artifactId>
    46. <version>4.1.3.RELEASE</version>
    47. </dependency>
    48. <dependency>
    49. <groupId>org.mybatis</groupId>
    50. <artifactId>mybatis</artifactId>
    51. <version>3.2.8</version>
    52. </dependency>
    53. <!-- 验证码 -->
    54. <dependency>
    55. <groupId>com.github.bingoohuang</groupId>
    56. <artifactId>patchca</artifactId>
    57. <version>0.0.1</version>
    58. </dependency>
    59. </dependencies>
    60. </project>

    //接着在web.xml中里面配置相关的加载配置文件的

    <!--加载spring容器  -->
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-mybatis.xml</param-value>
      </context-param>
      <servlet>
          <servlet-name>springmvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:spring-mvc.xml</param-value>
          </init-param>
      </servlet>
      <servlet-mapping>
          <servlet-name>springmvc</servlet-name>
          <url-pattern>*.do</url-pattern>
      </servlet-mapping>
      <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>
      </filter>
    
      <filter-mapping>
          <filter-name>encodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
    

    // 接着配置两个配置文件
    SSM整合框架的配置 - 图1

    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:jdbc="http://www.springframework.org/schema/jdbc"  
        xmlns:jee="http://www.springframework.org/schema/jee" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
            <!--开启注解扫描  -->
            <context:component-scan base-package="com.zhiyou100"/>
            <!--开启mvc扫描  -->
            <mvc:annotation-driven/>
    
            <!--视图  -->
            <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/"></property>
            <property name="suffix" value=".jsp"></property>
            </bean>
    
    
            <!-- 处理系统的异常 -->
            <bean id="ExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
             <property name="exceptionMappings">
            <!--  出现任何异常Exception则跳转到错误页面,WEB-INF/ -->
                 <props>
                     <prop key="java.lang.Exception">user/error</prop>
                 </props>
             </property>
            </bean>
        </beans>
    

    spring-mybatis.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:jdbc="http://www.springframework.org/schema/jdbc"  
        xmlns:jee="http://www.springframework.org/schema/jee" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
            <!-- 配置数据源  -->
            <util:properties id="jdbc" location="classpath:jdbc.properties"/>
            <bean id="ds" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
            <property name="url" value="#{jdbc.url}"></property>
            <property name="driverClassName" value="#{jdbc.driverClass}"></property>
            <property name="username" value="#{jdbc.user}"></property>
            <property name="password" value="#{jdbc.password}"></property>
            </bean>
            <!--session工厂  -->
            <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
                <property name="dataSource" ref="ds"></property>
                <property name="mapperLocations" value="classpath:mapper/*.xml"/>
            </bean>
    
            <!--扫描指定包下带有注解的接口  -->
            <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                <property name="basePackage" value="com.zhiyou100.dao"></property>
                 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    
            </bean>
    
            <!--配置事务管理器  -->
            <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <property name="dataSource" ref="ds"></property>
            </bean>
    
            <!--开启注解进行事务管理  -->
            <tx:annotation-driven transaction-manager="transactionManager"/>
    
            <!--Spring注解组件扫描  -->
            <!-- <context:component-scan base-package="com.zhiyou100"/> -->
        </beans>
    

    测试文件:

    package com.zhiyou100.test;
    
    import java.util.List;
    
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    
    public class Test {
    
        @org.junit.Test
        public void test01(){
            String cfg = "spring-mybatis.xml";
            ApplicationContext ac = 
                    new ClassPathXmlApplicationContext(cfg);
            System.out.println(ac);
    
    }
    }
    

    //相应的mapper.xml文件

    <?xml version="1.0" encoding="UTF-8" ?>  
    <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
     "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
     <mapper namespace="com.zhiyou100.dao.UserDao">
     <resultMap type="com.zhiyou100.model.User" id="userMap">
         <result property="id" column="ID"/>
         <result property="userName" column="USERNAME"/>
         <result property="password" column="PASSWORD"/>
         <result property="age" column="AGE"/>
     </resultMap>
         <select id="findByName" resultMap="userMap" parameterType="string">
             select * from user where userName=#{userName}
         </select>
    
         <select id="findAll" resultMap="userMap">
             select *from user
         </select>
    
         <select id="loginUser" resultMap="userMap" parameterType="string" >
             select * from user where userName=#{userName} and password=#{password}
         </select>
    
         <select id="findById" resultMap="userMap" parameterType="integer">
             select *from user where id=#{id}
         </select>
    
         <insert id="addUser" parameterType="com.zhiyou100.model.User">
             insert into user (id,userName,password,age) values
             (null,#{userName},#{password},#{age})
         </insert>
    
         <update id="updateUser" parameterType="com.zhiyou100.model.User" >
             update user set userName={userName},password=#{password},age=#{age}
             where id=#{id}
         </update>
    
         <delete id="deleteUser" parameterType="integer">
             delete from user where id=#{id}
         </delete>
     </mapper>