Mybatis与SpringMVC整合

使用的环境和JAR包依赖:

  • JDK1.8
  • mybatis3.2.1版
  • mybatis-spring 1.2.1版
  • spring-framework 4.3.12版

把jar包导入到项目中的lib文件夹下
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>MyProject</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            /WEB-INF/classes/applicationContext.xml
        </param-value>
  </context-param>
  <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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>ContextConfigLocation</param-name>
      <param-value>/WEB-INF/classes/application-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!-- 开启Druid连接池查看-->
  <servlet>
    <servlet-name>DruidStatusViewer</servlet-name>
    <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DruidStatusViewer</servlet-name>
    <url-pattern>/druid/*</url-pattern>
  </servlet-mapping>
</web-app>

根据web.xml文件里的设置,将对应的配置文件放到相应的位置。
application-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:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
             http://www.springframework.org/schema/aop
             http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="org.qst.joe">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <context:component-scan base-package="org.qst.joe.service"></context:component-scan>
    <context:component-scan base-package="org.qst.joe.serviceimpl"></context:component-scan>

    <!-- 启用CGliB -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- 配置结果页面 前缀和后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="order" value="10"></property>
        <property name="prefix" value="/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 配置哪些是静态资源,缺省Servlet 直接返回 -->
    <mvc:resources location="/static/" mapping="/static/**"/>
    <!-- 文件上传 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置上传文件总大小限制 -->
        <property name="maxUploadSize" value="10000000"></property>
    </bean>
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
           <!--  将StringHttpMessageConverter的默认编码设为UTF-8 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8"/>
            </bean>
                 <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
            <property name="supportedMediaTypes">    
                <list>    
                    <value>text/html;charset=UTF-8</value>  <!-- 避免IE出现下载JSON文件的情况 -->  
                </list>    
            </property>      
        </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
</beans>

applicationContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:cache= "http://www.springframework.org/schema/cache" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/cache 
    http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- 在spring中使用annotation来注册bean -->
    <context:component-scan base-package="org.qst.joe">
         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
     <aop:aspectj-autoproxy proxy-target-class="false"></aop:aspectj-autoproxy> 

      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:config/mybatis/*.xml" />
    </bean>



     <!-- 记载配置文件信息 -->
    <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
    </bean>
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">  
        <property name="properties" ref="configProperties" />
    </bean>


       <bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">      
        <property name="url" value="${jdbc_url}" />
        <property name="username" value="${jdbc_user}" />
        <property name="password" value="${jdbc_password}" />
        <property name="driverClassName" value="${driverClassName}"/>
        <property name="testOnBorrow" value="true" />        
        <property name="testWhileIdle" value="true" /> 
        <property name="testOnReturn" value="true" />     
        <property name="initialSize" value="10" />  
        <property name="maxActive" value="100" />  
        <property name="maxIdle" value="5" />  
        <property name="minIdle" value="10" />  
        <property name="validationQuery" value="select 1 from dual " />
        <property name="removeAbandonedTimeout" value="120" />  
        <property name="removeAbandoned" value="true" />  
        <property name="timeBetweenEvictionRunsMillis" value="3600000" />  
        <property name="minEvictableIdleTimeMillis" value="3600000" />    
</bean> 



  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean> 


    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="edit*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="select*" propagation="REQUIRED" read-only="true"/>  
            <tx:method name="query*" propagation="REQUIRED"  read-only="true"/>   
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:advisor advice-ref="transactionAdvice" pointcut="execution(* org.qst..service.*.*(..))"/>
    </aop:config> 


    <!-- 扫描 basePackage下所有的接口,根据对应的mapper.xml为其生成代理类 -->
    <bean id="mapperScannerConfigurer"  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.qst.joe.mapper" />
    </bean>


</beans>                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值