用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. 编写页面代码.
先有个大概映像,整个工程图如下:

 

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 :与Spring MVC 的集成的更多相关文章

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

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

  2. Mybatis学习(6)与Spring MVC 的集成

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

  3. MyBatis整合Spring MVC

    前面几篇文章已经讲到了mybatis与spring 的集成.目前主流的Web MVC框架,除了Struts这个主力外,还有Spring MVC,主要是由于 Spring MVC 配置比较简单,使用起来 ...

  4. MyBatis整合Spring MVC(易百教程)

    MyBatis是ibatis的升级版,作为hibernate的老对手,它是一个可以自定义SQL.存储过程和高级映射的持久层框架.与Hibernate 的主要区别就是 Mybatis 是半自动化的,而 ...

  5. Mybatis 与 spring mvc

    本文是用来小结一下自己mybatis 和spring mvc 学习过程. 在写的过程中发现 http://www.phperz.com/article/15/0127/48684.html 这篇文章里 ...

  6. OSGI企业应用开发(十五)基于Spring、Mybatis、Spring MVC实现一个登录应用

    前面文章中,我们已经完成了OSGI应用中Spring.Mybatis.Spring MVC的整合,本篇文章我们就在这个基础上来完成一个简单的登录应用,其中用户名和密码需要从数据库中查询. 前面文章中, ...

  7. OSGI企业应用开发(十四)整合Spring、Mybatis、Spring MVC

    作为一个企业级的Web应用,MVC框架是必不可少的.Spring MVC目前使用也比较广泛,本文就来介绍一下如何在OSGI应用中实现Spring.Mybatis.Spring MVC框架的整合,其中S ...

  8. Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例

    Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能看到这篇文章的小伙伴,详细你已经有一定的Java ...

  9. 由“单独搭建Mybatis”到“Mybatis与Spring的整合/集成”

    在J2EE领域,Hibernate与Mybatis是大家常用的持久层框架,它们各有特点,在持久层框架中处于领导地位. 本文主要介绍Mybatis(对于较小型的系统,特别是报表较多的系统,个人偏向Myb ...

随机推荐

  1. 写入~/.bashrc 文件

    1.进入~/.bashrc 文件 vim ~/.bashrc 2.按下I键,然后按Enter键 加入路径 3.按ESC键退出,再按:wq! 保存即可.

  2. C语言手册-read

    名称: pread,read-从文件读 语法: #include <unistd.h> ssize_t pread(int fildes, void *buf, size_t nbyte, ...

  3. python基础9 (迭代器、生成器)

    1.可迭代对象 迭代:将某个数据集内的数据“一个挨着一个的取出来” 可迭代协议:可以被迭代要满足的要求,即内部含有__iter__()方法 可迭代的类型:字符串.列表.元组.字典.集合特点:惰性运算 ...

  4. 洛谷P5087 数学

    DP. 设f[i][j]为前j个数中选i个数的所有组合的分数之和 决策: 不选这个数,得分为f[i][j - 1] 选这个数,得分为f[i - 1][j - 1] * a[j] 可以得到状态转移方程为 ...

  5. thymeleaf 常用标签

    1.th:field th:field="*{user.sex}" 此标签会自动填充数据,比如用户的性别 user.sex 如果不为空,则会自动勾选上  2.th:each=&qu ...

  6. python中的try...except...finally函数

    异常Error 我们在写代码的时候,经常会遇见程序抛出Error无法执行的情况 一般情况下,在Python无法正常处理程序时就会发生一个异常.异常是Python对象,表示一个错误.当Python脚本发 ...

  7. 【Educational Codeforces Round 41 (Rated for Div. 2) D】Pair Of Lines

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果点的个数<=3 那么直接输出有解. 否则. 假设1,2最后会在一条直线上,则把这条直线上的点都删掉. 看看剩余的点是否在同 ...

  8. Qt之自定义布局管理器(QBorderLayout)

    简述 QBorderLayout,顾名思义-边框布局,实现了排列子控件包围中央区域的布局. 具体实现要求不再赘述,请参考前几节内容. 简述 实现 效果 源码 使用 实现 QBorderLayout主要 ...

  9. Beat &#39;Em Up Game Starter Kit (横版格斗游戏) cocos2d-x游戏源代码

    浓缩精华.专注战斗! 游戏的本质是什么?界面?养成?NoNo!    游戏来源于对实战和比赛的模拟,所以它的本源就是对抗.就是战斗! 是挥洒热血的一种方式! 一个游戏最复杂最难做的是什么?UI?商城? ...

  10. 小贝_mysql select5种子句介绍

    mysql select5种子句介绍 简要 一.五种字句 二.具体解释五种字句 一.五种字句 where.group by.having.order by.limit 二.具体解释五种字句 2.1.理 ...