前面几篇文章已经讲到了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学习(6)与Spring MVC 的集成的更多相关文章

  1. 。。。在学习新框架Spring MVC的感受。。。

    已经学习一遍Spring MVC了,感觉还是懵懵懂懂的,特别是重定向,路径,参数的这些问题,心好乱,不过,这,都不是问题!!! 继续努力,努力到会为止!!!加油!!!

  2. MyBatis学习七:spring和MyBatis整合

    <\mybatis\day02\16mybatis和spring整合-sqlSessionFactory配置.avi;> MyBatis学习七:spring和MyBatis整合.逆向工程 ...

  3. mybatis实战教程(mybatis in action)之六:与Spring MVC 的集成

    前面几篇文章已经讲到了mybatis与spring 的集成.但这个时候,所有的工程还不是web工程,虽然我一直是创建的web 工程.今天将直接用mybatis与Spring mvc 的方式集成起来,源 ...

  4. mybatis :与Spring MVC 的集成

    用mybatis与Spring mvc 的方式集成起来,源码在本文结尾处下载.主要有以下几个方面的配置1. web.xml 配置 spring dispatchservlet ,比如为:mvc-dis ...

  5. (原创)mybatis学习二,spring和mybatis的融合

    mybatis学习一夯实基础 上文介绍了mybatis的相关知识,这一节主要来介绍mybaits和spring的融合 一,环境搭建 1,jar包下载,下载路径为jar包 2,将包导入到java工程中 ...

  6. springboot学习章节代码-Spring MVC基础

    1.项目搭建. <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  7. Spring学习笔记之五----Spring MVC

    Spring MVC通常的执行流程是:当一个Web请求被发送给Spring MVC Application,Dispatcher Servlet接收到这个请求,通过HandlerMapping找到Co ...

  8. Java Spring MVC项目搭建(一)——Spring MVC框架集成

    1.Java JDK及Tomcat安装 我这里安装的是JDK 1.8 及 Tomcat 8,安装步骤详见:http://www.cnblogs.com/eczhou/p/6285248.html 2. ...

  9. Spring学习日志之Spring MVC启动配置

    对DispatcherServlet进行配置 Spring MVC的配置实际上就是对DispatcherServlet的配置 public class DispatcherServletConfig ...

随机推荐

  1. 计划任务 at & crond tbc

    一次性任务 at 工具 由包 at 提供 依赖与atd服务,需要启动才能实现at任务 at队列存放在/var/spool/at目录中 执行任务时PATH变量的值和当前定义任务的用户身份一致 作业执行命 ...

  2. 揭秘有状态服务上 Kubernetes 的核心技术

    背景 随着 Kubernetes 成为云原生的最热门的解决方案,越来越多的传统服务从虚拟机.物理机迁移到 Kubernetes,各云厂商如腾讯自研上云也主推业务通过Kubernetes来部署服务,享受 ...

  3. openresty 学习笔记番外篇:python访问RabbitMQ消息队列

    openresty 学习笔记番外篇:python访问RabbitMQ消息队列 python使用pika扩展库操作RabbitMQ的流程梳理. 客户端连接到消息队列服务器,打开一个channel. 客户 ...

  4. TensorRT 3:更快的TensorFlow推理和Volta支持

    TensorRT 3:更快的TensorFlow推理和Volta支持 TensorRT 3: Faster TensorFlow Inference and Volta Support 英伟达Tens ...

  5. Technology Document Guide of TensorRT

    Technology Document Guide of TensorRT Abstract 本示例支持指南概述了GitHub和产品包中包含的所有受支持的TensorRT 7.2.1示例.Tensor ...

  6. 多尺度目标检测 Multiscale Object Detection

    多尺度目标检测 Multiscale Object Detection 我们在输入图像的每个像素上生成多个锚框.这些定位框用于对输入图像的不同区域进行采样.但是,如果锚定框是以图像的每个像素为中心生成 ...

  7. Python分析离散心率信号(中)

    Python分析离散心率信号(中) 一些理论和背景 心率信号不仅包含有关心脏的信息,还包含有关呼吸,短期血压调节,体温调节和荷尔蒙血压调节(长期)的信息.也(尽管不总是始终如一)与精神努力相关联,这并 ...

  8. dataguard日志损坏处理

    ===== 问题 ===== 日志损坏无法应用日志(开启MRP应用系统会因无法应用日志而关闭) Completed: ALTER DATABASE RECOVER MANAGED STANDBY DA ...

  9. Paxos 图解 (秒懂)

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  10. Visual Studio 2019本地不能运行Azure Functions

    最近一个项目,需要维护同事写得代码,主要是一堆基于 .net core 3.1 的 Azure Functions.想起2年前第一次接触 Azure Functions(那次是基于.net frame ...