前面几篇文章已经讲到了mybatis与spring 的集成。但这个时候,所有的工程还不是web工程,虽然我一直是创建的web 工程。今天将直接用mybatis与Spring mvc 的方式集成起来,源码在本文结尾处下载.主要有以下几个方面的配置
1. web.xml 配置 spring dispatchservlet ,比如为:mvc-dispatcher
2. mvc-dispatcher-servlet.xml 文件配置
3. spring applicationContext.XML文件配置(与数据库相关,与mybatis sqlSessionFaction 整合,扫描所有mybatis mapper 文件等.)
4. 编写controller 类
5. 编写页面代码.
先有个大概映像,整个工程图如下:
[/code]

1. web.xml 配置 spring dispatchservlet ,比如为:mvc-dispatcher

程序代码

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:config/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>
            org.springframework.web.context.ContextCleanupListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

2. 在web.xml 同目录下配置 mvc-dispatcher-servlet.xml 文件,这个文件名前面部分必须与你在web.xml里面配置的DispatcherServlet 的servlet名字对应.其内容为:

程序代码

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">

    <context:component-scan base-package="com.yihaomen.controller" />
    <mvc:annotation-driven />
    
    <mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>  
    <mvc:default-servlet-handler/>  
     
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

3. 在源码目录 config 目录下配置 spring 配置文件 applicationContext.xml

程序代码

<!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。 -->   
    <context:property-placeholder    location="classpath:/config/database.properties" />
        
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://127.0.0.1:3306/mybatis?characterEncoding=utf8" 
        p:username="root" p:password="password"
        p:maxActive="10" p:maxIdle="10">
    </bean>
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource" />
    </bean>
    
     
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
     <!--dataSource属性指定要用到的连接池--> 
     <property name="dataSource" ref="dataSource"/> 
     <!--configLocation属性指定mybatis的核心配置文件--> 
     <property name="configLocation" value="classpath:config/Configuration.xml" /> 
     <!-- 所有配置的mapper文件 -->
     <property name="mapperLocations" value="classpath*:com/yihaomen/mapper/*.xml" />
  </bean> 
  
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     <property name="basePackage" value="com.yihaomen.inter" />     
  </bean>

不知道为什么,一旦我用了 MapperScannerConfigurer 去扫描所有的mapper 接口时,数据库配置datasource 就不能用读取database.properties文件了。报错: Cannot load JDBC driver class '${jdbc.driverClassName}',网上有人说在spring 3.1.1 下用 sqlSessionFactionBean 注入可以解决,但我用 spring 3.1.3 还是有问题,所以只好把数据库连接信息直接配置在了XML 文件里面。

4. 编写 controller 层

程序代码

package com.yihaomen.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.yihaomen.inter.IUserOperation;
import com.yihaomen.model.Article;

@Controller
@RequestMapping("/article")
public class UserController {
    @Autowired
    IUserOperation userMapper;

    @RequestMapping("/list")
    public ModelAndView listall(HttpServletRequest request,HttpServletResponse response){
        List<Article> articles=userMapper.getUserArticles(1); 
        ModelAndView mav=new ModelAndView("list");
        mav.addObject("articles",articles);
        return mav;
    }
}

5. 页面文件:
[code]
<c:forEach items="${articles}" var="item">  
        ${item.id }--${item.title }--${item.content }<br />
    </c:forEach>

运行结果:


当然还有 mybatis 的Configure.xml  配置文件,与上一讲的差不多,唯一不同的就是不用再配置类似如下的:   <mapper resource="com/yihaomen/mapper/User.xml"/> ,所有这些都交给 在配置 sqlSessionFactory 的时候,由  <property name="mapperLocations" value="classpath*:com/yihaomen/mapper/*.xml" /> 去导入了。
    

mybatis实战教程(mybatis in action)之六:与Spring MVC 的集成的更多相关文章

  1. 【转】mybatis实战教程(mybatis in action),mybatis入门到精通

    MyBatis 目录(?)[-] mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis ...

  2. mybatis实战教程(mybatis in action),mybatis入门到精通

    转自:http://www.yihaomen.com/article/java/302.htm (读者注:其实这个应该叫做很基础的入门一下下,如果你看过hibernate了那这个就非常的简单) (再加 ...

  3. mybatis实战教程(mybatis in action),mybatis入门到精通(转)

    转自:http://www.yihaomen.com/article/java/302.htm (读者注:其实这个应该叫做很基础的入门一下下,如果你看过Hibernate了那这个就非常的简单) (再加 ...

  4. 转)mybatis实战教程(mybatis in action),mybatis入门到精通

    mybatis实战教程(mybatis in action),mybatis入门到精通 http://limingnihao.iteye.com/blog/781671 http://blog.csd ...

  5. 1.mybatis实战教程mybatis in action之一开发环境搭建

    转自:https://www.cnblogs.com/shanheyongmu/p/5652471.html 什么是mybatis MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框 ...

  6. mybatis实战教程(mybatis in action)之七:实现mybatis分页

    上一篇文章里已经讲到了mybatis与spring MVC的集成,并且做了一个列表展示,显示出所有article 列表,但没有用到分页,在实际的项目中,分页是肯定需要的.而且是物理分页,不是内存分页. ...

  7. 8. mybatis实战教程(mybatis in action)之七:实现mybatis分页(源码下载)

    转自:https://blog.csdn.net/tangruyi1992/article/details/52584012 上 一篇文章里已经讲到了mybatis与spring MVC的集成,并且做 ...

  8. mybatis实战教程(mybatis in action)之九:mybatis 代码生成工具的使用

    mybatis 应用程序,需要大量的配置文件,对于一个成百上千的数据库表来说,完全手工配置,这是一个很恐怖的工作量. 所以mybatis 官方也推出了一个mybatis代码生成工具的jar包. 今天花 ...

  9. mybatis实战教程(mybatis in action)之九:mybatis 代码生成工具的使用

    mybatis 应用程序,需要大量的配置文件,对于一个成百上千的数据库表来说,完全手工配置,这是一个很恐怖的工作量. 所以mybatis 官方也推出了一个mybatis代码生成工具的jar包. 今天花 ...

随机推荐

  1. 全国大学列表文件(较新)+ nodejs导入mongodb数据库

    直接上代码 'use strict' var fs=require('fs'), mongodb=require('mongodb').MongoClient, assert=require('ass ...

  2. awk(2)-模式(pattern)

    在上文 awk(1)-简述我们将简要描述了awk的主要使用方向和构成(由一个或多个模式-动作组成),本小节主要讲述awk的各种模式. ps:例子中使用的输入文件(如countries)内容可由awk( ...

  3. linux sed和awk的区别

    awkawk是一种程序语言,对文档资料的处理具有很强的功能.awk擅长从格式化报文或从一个大的文本文件中抽取数据.awk的命令格式为:awk [-F filed-separator] “command ...

  4. Unity C# XmlDoc.LoadXml() il2cpp not implemented exception

    Stuck with this for a couple of hours, turned out it's just a setting thing, Answer from this post ( ...

  5. 遍历 Input检测是否有重复的值

    在项目中需要遍历某个Table中的Input输入是否有重复的值,为此基于Jquery写了两种实现方式(关键在于取值方式): 方法1: function CheckGoodsNo() { var ish ...

  6. C# 之泛型详解

    转自牛人博客:http://www.blogjava.net/Jack2007/archive/2008/05/05/198566.html 鄙人才疏学浅,经常遇到泛型,一知半解,用的时候也是模糊不清 ...

  7. LintCode StrStr

    1. 讨论目标字符串若为空, 则返回-1: 资源字符串若为空, 则返回-1. 2.讨论目标字符串个数为零, 则返回0: 资源字符串个数为零, 则返回-1. 3. 插入旗帜来使第二循环的结束为有条件地返 ...

  8. 【转载】Android 自动化测试 Emmagee

    Emmagee 是一个性能测试小工具 用来监控指定被测应用在使用过程中占用机器的CPU, 内存,流量资源的性能小工具 Emmagee 介绍 Emmagee是网易杭州研究院QA团队开发的一个简单易上手的 ...

  9. Android下载更新代码

    其实是昨天反编译一个apk,给它添加一个自动更新的功能用到的.为了在smali下方便查看,代码写的不规范,反正到了smali都一个吊样~~~~ 权限: <uses-permission andr ...

  10. 在AndroidStudio不能找到so文件问题:couldn't find libweibosdkcore.so

    解决步骤已经写到我的公众号,二维码在下面. 欢迎观看我的CSDN学院课程,地址:http://edu.csdn.net/course/detail/2877 本人联系方式: 更多精彩分享,可关注我的微 ...