Spring+Spring MVC+MyBatis整合文件配置详解

1.web.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"
xmlns:websocket="http://www.springframework.org/schema/websocket"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd
  http://www.springframework.org/schema/websocket 
  http://www.springframework.org/schema/websocket/spring-websocket.xsd"
  version="3.0">
  <display-name>web</display-name>
  <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>
  <listener>  
        <listener-class>zxz.listener.SessionListener</listener-class>          
  </listener> 
<listener>  
        <listener-class>zxz.listener.TomcatListener</listener-class>          
  </listener>
  <listener>  
   <listener-class>net.sf.ehcache.constructs.web.ShutdownListener</listener-class>  
</listener>
<servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet>
   <servlet-name>cash-pre-loan-res</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:springServlet.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>cash-pre-loan-res</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
    <filter-name>SpringEncodingFilter</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>
   <init-param>
     <param-name>forceEncoding</param-name>
     <param-value>true</param-value>
   </init-param>
</filter>
<filter-mapping>
    <filter-name>SpringEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>






2.springServlert


<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
       
    <context:property-placeholder location="classpath:config.properties"/>
    <!--RequestMappingHandlerAdapter-->
<mvc:annotation-driven>
 <mvc:message-converters>
   <ref bean="stringHttpMessageConverter"/>
   <ref bean="marshallingHttpMessageConverter"/>
   <ref bean="mappingJackson2HttpMessageConverter"/>
 </mvc:message-converters>
</mvc:annotation-driven>

<bean id="stringHttpMessageConverter"
     class="org.springframework.http.converter.StringHttpMessageConverter"/>

<bean id="marshallingHttpMessageConverter"
     class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
 <property name="marshaller" ref="castorMarshaller"/>
 <property name="unmarshaller" ref="castorMarshaller"/>
</bean>

<bean id="mappingJackson2HttpMessageConverter"
     class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
 <property name="supportedMediaTypes">
   <list>
     <value>application/json</value>
     <value>application/xml</value>
     <value>text/html</value>
     <value>text/plain</value>
     <value>text/xml</value>
   </list>
 </property>
</bean>

<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"/>
<!--信息转换 - 结束-->

<bean id="contentNegotiationManagerFactoryBean"
     class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
 <property name="defaultContentType" value="text/html"/>
 <property name="ignoreAcceptHeader" value="true"/>
 <property name="favorPathExtension" value="true"/>
 <property name="favorParameter" value="false"/>
 <property name="mediaTypes">
   <map>
     <entry key="atom" value="application/atom+xml"/>
     <entry key="html" value="text/html"/>
     <entry key="json" value="application/json"/>
   </map>
 </property>
</bean>

<!--内容协商-->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
 <property name="contentNegotiationManager">
   <bean class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
     <property name="defaultContentType" value="text/html"/>
     <property name="mediaTypes">
       <map>
         <entry key="json" value="application/json"/>
         <entry key="xml" value="application/xml"/>
         <entry key="html" value="text/html"/>
       </map>
     </property>
   </bean>
 </property>
 <property name="viewResolvers">
   <list>
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/jsp/"/>
       <property name="suffix" value=".jsp"/>
       <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
     </bean>
   </list>
 </property>
 <property name="defaultViews">
   <list>
     <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
     <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
       <property name="marshaller" ref="castorMarshaller"/>
     </bean>
   </list>
 </property>
</bean>

<!--国际化-->
<bean id="messageSource"
     class="org.springframework.context.support.ResourceBundleMessageSource">
 <property name="defaultEncoding" value="UTF-8"/>
 <property name="basenames">
   <list>
     <value>messages.welcome</value>
   </list>
 </property>
</bean>

<mvc:interceptors>
 <mvc:interceptor>
  <!-- 默认所有的请求都进行拦截 -->
  <mvc:mapping path="/**"/>
  <!-- 不拦截 -->
       <mvc:exclude-mapping path="/**.html"/>
       <mvc:exclude-mapping path="/**.js"/>
       <mvc:exclude-mapping path="/**.css"/>
       <mvc:exclude-mapping path="/**.jpg"/>
       <mvc:exclude-mapping path="/**.png"/>
  <bean class="zxz.interceptors.CommonInterceptor"></bean>
 </mvc:interceptor>
</mvc:interceptors>
<!-- aspect实现注解切面 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <context:component-scan base-package="zxz.controller,zxz.websocket" />
    <context:component-scan base-package="zxz.aop"/>
    
    <!-- 上下文初始化 -->
    <bean class="zxz.thread.bean.SpringContextUtil"></bean>
</beans>


3.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"  
    xmlns:jms="http://www.springframework.org/schema/jms"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd     
        http://www.springframework.org/schema/context     
        http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jms  
        http://www.springframework.org/schema/jms/spring-jms-4.0.xsd  
        http://activemq.apache.org/schema/core  
        http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd">  


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

<import resource="applicationContext-dao.xml"/>
<import resource="applicationContext-mybatis.xml"/>
<import resource="applicationContext-service.xml"/>
   
</beans>   


4.applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">




<context:component-scan base-package="zxz.dao.**"/>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>




        <property name="filters" value="stat"/>




        <property name="maxActive" value="20"/>
        <property name="initialSize" value="1"/>
        <property name="maxWait" value="60000"/>
        <property name="minIdle" value="1"/>




        <property name="timeBetweenEvictionRunsMillis" value="60000"/>
        <property name="minEvictableIdleTimeMillis" value="300000"/>




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


5.applicationContext-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">








    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
         <!-- 配置MyBaties全局配置文件:mybatis.xml -->
<property name="configLocation" value="classpath:mybatis.xml" />
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
        <property name="mapperLocations" value="classpath:cn/cash/china/mapper/*.xml" />
        <!-- 扫描entity包 使用别名 -->
        <property name="typeAliasesPackage" value="zxz.entity.**"/>
    </bean>




    <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- 扫描Dao接口包 -->
        <property name="basePackage" value="zxz.dao.**"/>
        <!-- 通用Mapper通过属性注入进行配置,默认不配置时会注册Mapper<T>接口-->
        <property name="properties">
            <value>
                mappers=tk.mybatis.mapper.common.Mapper
            </value>
        </property>
    </bean>




    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
 
</beans>


6.applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">




    <context:component-scan base-package="zxz">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
</context:component-scan>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
    <tx:annotation-driven transaction-manager="transactionManager" />  
    
    <bean id="txTemplate" class="org.springframework.transaction.support.TransactionTemplate">  
   <constructor-arg type="org.springframework.transaction.PlatformTransactionManager" ref="transactionManager" />  
</bean>
    
</beans>


7.mybatis.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>
配置全局属性
<settings>
使用jdbc的getGeneratedKeys获取数据库自增主键值
<setting name="useGeneratedKeys" value="true" />




使用列别名替换列名 默认:true
<setting name="useColumnLabel" value="true" />




开启驼峰命名转换:Table{create_time} -> Entity{createTime}
<setting name="mapUnderscoreToCamelCase" value="true" />
        <setting name="logPrefix" value="dao."/>
</settings>
</configuration> -->
<!-- 配置管理器 -->
<configuration>
<settings>
<setting name="jdbcTypeForNull" value="NULL"/>
</settings>
<plugins>
   <!-- com.github.pagehelper为PageHelper类所在包名 -->
   <plugin interceptor="com.github.pagehelper.PageHelper">
       <!-- 4.0.0以后版本可以不设置该参数 -->
       <property name="dialect" value="oracle"/>
       <!-- 该参数默认为false -->
       <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
       <!-- 和startPage中的pageNum效果一样-->
       <property name="offsetAsPageNum" value="true"/>
       <!-- 该参数默认为false -->
       <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
       <property name="rowBoundsWithCount" value="true"/>
       <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
       <!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->
       <property name="pageSizeZero" value="true"/>
       <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
       <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
       <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
       <property name="reasonable" value="true"/>
       <!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->
       <!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->
       <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值 -->
       <!-- 不理解该含义的前提下,不要随便复制该配置 -->
       <property name="params" value="pageNum=start;pageSize=limit;"/>
       <!-- 支持通过Mapper接口参数来传递分页参数 -->
       <property name="supportMethodsArguments" value="true"/>
       <!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->
       <property name="returnPageInfo" value="check"/>
   </plugin>
</plugins>
</configuration>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值