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

前面几篇文章已经讲到了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"
/> 去导入了。

时间: 2024-07-29 08:02:21

Mybatis学习(6)与Spring MVC 的集成的相关文章

mybatis学习笔记(14)-spring和mybatis整合

mybatis学习笔记(14)-spring和mybatis整合 mybatis学习笔记14-spring和mybatis整合 整合思路 整合环境 sqlSessionFactory 原始dao开发和spring整合后 mapper代理开发 遇到的问题 本文主要将如何将spring和mybatis整合,只是作简单的示例,没有使用Maven构建.并展示mybatis与spring整合后如何进行原始dao开发和mapper代理开发. 整合思路 需要spring通过单例方式管理SqlSessionFa

[email&#160;protected] 深入学习之——初探spring mvc

一.简介 Spring MVC是Spring框架的最重要的模块之一,它构建于Spring IoC容器之上,大量使用容器的特性简化其配置.MVC模式消除了业务逻辑与UI的耦合.模式负责封装视图展示的应用数据:视图只显示数据,不包含任何业务逻辑:控制器负责接收用户请求并调用后端服务进行业务处理,处理之后,后端服务可能返回某些数据供视图显示.其核心思想是分离业务逻辑与UI,使系统能够独立修改,互不影响. Spring MVC应用,模式通常由服务层处理和持续层存储的领域对象组成.视图通常是用Java标准

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

前面几篇文章已经讲到了mybatis与spring 的集成.但这个时候,所有的工程还不是web工程,虽然我一直是创建的web 工程.今天将直接用mybatis与Spring mvc 的方式集成起来,源码在本文结尾处下载.主要有以下几个方面的配置1. web.xml 配置 spring dispatchservlet ,比如为:mvc-dispatcher2. mvc-dispatcher-servlet.xml 文件配置3. spring applicationContext.XML文件配置(与

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

1.Java JDK及Tomcat安装 我这里安装的是JDK 1.8 及 Tomcat 8,安装步骤详见:http://www.cnblogs.com/eczhou/p/6285248.html 2.下载Eclipse并安装 我这里安装的是Java EE neon 64位版本. 3.建立Spring MVC项目 3.1.打开安装好的eclipse ,选择File->new->other.在弹出的框内选择Web->Dynamic Web Project , 选择Next. 3.2.给项目起

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

Spring MVC通常的执行流程是:当一个Web请求被发送给Spring MVC Application,Dispatcher Servlet接收到这个请求,通过HandlerMapping找到Controller,将这个请求委派给Controller的某个Handler Method处理,这个Handler Method处理完这个请求,返回一个ModelAndView给Dispatcher Servlet,Dispatcher Servlet利用View Name,请求View Resolv

Spring学习记录(2) Spring MVC+Mybatis 注解配置

所有XML的配置如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springfr

MyBatis学习24-mybatis和spring整合

1.整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spring和mybatis整合自动完成) 持久层的mapper都需要由spring进行管理. 2.整合环境的搭建 a.加入mybatis的jar包 b.加入spring的jar包 c.mybatis和Spring的整合包mybatis-spring-1.2.3 http://mvnrepository

Mybatis学习(5)与spring3集成

在这一系列文章中,前面讲到纯粹用mybatis 连接数据库,然后 进行增删改查,以及多表联合查询的的例子,但实际项目中,通常会用 spring 这个沾合剂来管理 datasource 等.充分利用spring 基于接口的编程,以及aop ,ioc 带来的方便.用spring 来管理 mybatis 与管理hibernate 有很多类似的地方.今天的重点就是数据源管理以及 bean的配置. 你可以下载源码后,对比着看,源代码没有带jar包,太大了,空间有限. 有截图,你可以看到用到哪些jar包,源

Mybatis学习之与Spring整合

bean对象定义: package com.mybatis.bean; public class Person { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName