当前位置:编程学习 > JAVA >>

Spring3 整合 Mybatis3

Spring3 整合 Mybatis3
这两天,项目需要使用spring+ibatis,于是去网上下载了,结果发现和之前我用的版本变化不小,整了两天才将功能实现,不敢怠慢,赶紧写份博客记录一下。

首先是依赖的库:

 

接着是web.xml的配置,这里,我使用的是Spring3 MVC

[html] 
<?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:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
  <display-name>Log</display-name> 
   
  <welcome-file-list> 
    <welcome-file>welcome.html</welcome-file> 
  </welcome-file-list> 
   
    <!--日志配置文件的载入 -->  
    <context-param> 
        <param-name>log4jConfigLocation</param-name> 
        <param-value>/WEB-INF/classes/log4j.properties</param-value> 
    </context-param> 
    <!--Spring配置文件的载入 -->  
    <context-param>     
        <param-name>contextConfigLocation</param-name>     
        <param-value>classpath:applicationContext*.xml</param-value>     
    </context-param>  
 
    <!--Spring的ApplicationContext 载入 -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener> 
     
    <!-- Spring字符集过滤 --> 
    <filter> 
        <filter-name>encodingFilter</filter-name> 
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class> 
        <init-param> 
            <param-name>encoding</param-name> 
            <param-value>utf8</param-value> 
        </init-param> 
    </filter> 
  
    <filter-mapping> 
        <filter-name>encodingFilter</filter-name > 
        <url-pattern>*.do</url-pattern> 
    </filter-mapping> 
 
<!-- Server启动时加载的应用 --> 
 
<!-- Spring过滤器 --> 
<servlet> 
    <servlet-name>dispatchServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>classpath:servlet-config.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
 
<servlet-mapping> 
    <servlet-name>dispatchServlet</servlet-name> 
    <url-pattern>*.do</url-pattern> 
</servlet-mapping> 
   
</web-app> 

然后是spring的servlet配置servlet-config.xml:
[html] view plaincopy
<?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-3.0.xsd 
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd 
   http://www.springframework.org/schema/mvc   
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
    
   <!-- 自动扫描bean,把作了注解的类转换为bean -->   
    <context:component-scan base-package="com.log.bean.dao, com.log.report.controller" />   
     
    <!-- 默认的注解映射的支持 -->   
    <mvc:annotation-driven/> 
    <!-- 支持JSON数据格式 --> 
    <bean  class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />   
    <bean  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >   
        <property name="messageConverters"> 
            <list> 
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" > 
                    <property name="supportedMediaTypes">   
                        <list>   
                            <value>text/html;charset=UTF-8</value>   
                        </list>   
                    </property>  
           &

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,