Mybatis学习笔记六:集成SpringMVC

在上一章中实现了如何将Mybatis集成到Spring当中,本章更进一步,说一下如何将Mybatis集成到SpringMVC中。

1、修改Spring配置文件

<?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:context="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <!-- 使用常见的c3p0数据连接池进行连接 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="root"/>
        <property name="password" value="zsfarther"/>
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/mybatis-chapter1"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    </bean>

    <!-- 配置sessionfactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 指定mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 配置所有mapper文件 -->
        <property name="mapperLocations" value="classpath*:com/dfz/mapper/*.xml"/>
    </bean>

    <!-- 配置事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 扫描mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dfz.mapper"/>
    </bean>
</beans>

根据上一章中所述,mybatis与Spring集成主要有三个配置点:
1)配置数据源;
2)配置sessionfactory;
3)配置mapper;
在本章中,集成SpringMVC再加入一个配置事务即可。

2、创建web.xml

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Mybatis-Chapter6-IntegrateSpringMVC</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring-config.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>
</web-app>

web.xml中的内容与mybatis并无太大关系,按照springmvc进行配置即可;

3、创建mvc-dispatcher-servlet.xml

<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.dfz.controller" />

    <mvc:annotation-driven />

    <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>


代码也同样很简单,自动装配@Controller注解,启动注解,配置ViewResolver。

4、创建Controller

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

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

用一个get方法来获取用户对应的文章;

5、创建页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Insert title here</title>
</head>
<body>
<c:forEach items="${articles}" var="item">
    ${item.id }--${item.title }--${item.content }<br />
</c:forEach>
<div style="padding:20px;">${pageStr}</div>
</body>
</html>

输出结果如下:
这里写图片描述

6、总结

Mybatis与SpringMVC的集成,主要还是在于配置文件上的配置,需要注意的是:
1)在配置配置文件以及mapper文件时,路径的写法有所不同;

<!-- 指定mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 配置所有mapper文件 -->
        <property name="mapperLocations" value="classpath*:com/dfz/mapper/*.xml"/>

2)配置一个扫描器来扫描所有配置文件,比一个个配置方便很多。

<!-- 扫描mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dfz.mapper"/>
    </bean>

未尽之处后期再补,代码下载地址:https://github.com/EdwardEricZhang/MybatisFun

没有更多推荐了,返回首页