Spring+SpringMVC+MyBatis整合配置

前端控制器 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 前端控制器 -->
  <servlet>
      <servlet-name>dispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring.xml</param-value>
      </init-param>
      
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>dispatcherServlet</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <!-- 中文乱码过滤器 -->
  <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>
  
  
  
</web-app>

spring核心配置文件

<?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="db" 
        location="classpath:db.properties"/>
        
    <!-- 定义数据源 -->
    <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="#{db.driver}"/>
        <property name="url" value="#{db.url}"/>
        <property name="username" value="#{db.user}"/>
        <property name="password" value="#{db.pwd}"/>
    </bean>    
    
    <!-- 定义SQLSessionFactoryBean组件 MyBatis连接数据库1-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <!-- 没有了MyBatis的主配置文件SqlMapConfig.xml -->
        <!-- 需要指定连接资源 -->
        <property name="dataSource" ref="ds"></property>
        <!-- 需要指定映射文件 -->
        <property name="mapperLocations" value="classpath:com/xms/entity/mapper/*.xml"></property>
    </bean>
    
    <!-- 定义MapperScannerrConfigurer扫描组件MyBatis扫描dao包2 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <!-- 指定Mapper接口扫描包 -->
        <property name="basePackage" value="com.xms.dao" ></property>
        <!-- 手动指定SqlSessionFactory对象 -->  <!-- sqlSessionFactory属性可以不用指定,它会以Autowired方式自动注入 -->
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean" ></property>
        
        <!-- 推荐使用注解方法 -->
      <property name="annotationClass" value="com.xms.common.MyAnnontation" ></property> 
    
    <!-- 接口方法 -->
    <!--    <property name="markerInterface" value="com.xms.common.Myinterface" />   -->    
    </bean>
    
    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.xms"/>
    
    <!-- 开启RequestMapping注解扫描 -->
    <mvc:annotation-driven/>
    
    <!-- 定义视图解析器ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <!-- 全局异常处理 -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="java.lang.Exception">error</prop>
            </props>
        </property>
    </bean>
    
        
    
</beans>

 

posted @ 2018-07-11 16:40  阴翳公子  阅读(145)  评论(0编辑  收藏  举报