spring mvc可以通过整合hibernate来实现与数据库的数据交互,也可以通过mybatis来实现,这篇文章是总结一下怎么在springmvc中整合mybatis.

首先mybatis需要用到的包如图所示:

下面是mybaits的配置文件,写的地方由你决定,在这里我写在mybatis-servlet.xml中,因为我在web.xml中设置了在tomcat启动时会加载所有以servlet.xml结尾的文件。

web.xml中的部分代码(涉及到数据源的东西,在你的基础上加上就行),详细的请查看我spring mvc系列文章的前几篇:

  
 <!-- 
  引用该数据源
   -->
  <resource-ref>
  <span style="white-space:pre"> </span><res-ref-name>jndi_mysql</res-ref-name>
  <span style="white-space:pre"> </span><res-type>javax.sql.DataSource</res-type>
  </resource-ref>

mybatis-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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!--
配置mybatis
在ioc容器中配置sqlSessionFactory
使用SqlSessionFactoryBean工厂bean
1 配置数据源
2 配置映射文件
注意classpath前缀
每在工程中添加一个映射文件,需要在list中添加一个value元素
-->
<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<property name="mapperLocations">
<list>
<value>classpath:<span style="color:#ff0000;">com/etock/dao/MemberDaoIf-mapper.xml</span></value>
</list>
</property>
</bean>
<!--
DataSource
1 实现类 DriverManageDataSource
2 JNDI方式 --> <!-- 第一种方式: -->
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<span style="color:#ff0000;"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/cn"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property></span>
</bean>
<!-- 第二种方式: -->
<!-- <bean id="ds" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/com.mysql.jdbc.Driver"></property>
</bean> -->
<!--
配置接口对应的实例bean对象
spring中为了配置接口实例,提供 MapperFactoryBean的工厂bean
-->
<bean id="dao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="ssf"></property>
<property name="mapperInterface" value="<span style="color:#ff0000;">com.etock.dao.MemberDaoIf"</span>></property>
</bean>
<!--
每在工程中添加一个接口,就需要在ioc容器中添加单独的bean节点使用mapperInterface实例化改接口
--> </beans>

使用时先建立一个bean类 如Member:

package com.etock.bean;

public class Member {
private Integer currentPage;
private Integer pageSize; private String name;
private String email;
private String password;
private String autograph;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
} public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAutograph() {
return autograph;
}
public void setAutograph(String autograph) {
this.autograph = autograph;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
} }

在定义dao层的接口:

package com.etock.dao;

import java.util.List;
import java.util.Map; import com.etock.bean.Member; public interface MemberDaoIf {
public List<Member> <span style="color:#ff0000;">selectMembersByPage</span>(Map map);
public int <span style="color:#ff0000;">selectMemberCount</span>();
}

然后是映射文件MemberDaoIf-mapper.xml:

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper <span style="color:#ff0000;">namespace="com.etock.dao.MemberDaoIf" </span>>  <!-- 
        这里返回的是list,但list里面存放的还是member对象,所以还是member
     --> <select id="<span style="color:#ff0000;">selectMemberCount</span>" resultType="java.lang.Integer">
select count(*) from member;
</select>
<select id="<span style="color:#ff0000;">selectMembersByPage</span>" parameterType="java.util.Map" resultMap="<span style="color:#ff0000;">member</span>">
select * from member limit #{start},#{max};
</select> <!--
返回类型解释
-->
<resultMap type="com.etock.bean.Member" id="<span style="color:#ff0000;">member</span>">
<result property="name" column="name"/>
<result property="email" column="email"/>
<result property="password" column="password"/>
<result property="autograph" column="autograph"/>
</resultMap>
</mapper>

然后是controller层

package com.etock.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.etock.bean.Member;
import com.etock.dao.MemberDaoIf; @Controller
public class MemberController { <span style="color:#ff0000;">@Autowired
private MemberDaoIf memberDao;</span> @RequestMapping("/selectMembersByPage")
@ResponseBody
public Map selectMembersByPage(Member member){ Integer currentPage = member.getCurrentPage();
Integer pageSize = member.getPageSize(); <span style="color:#ff0000;">int totalCount = memberDao.selectMemberCount();</span>
int totalPage = (totalCount+pageSize-1)/pageSize;
<span style="color:#ff0000;">Map map = new HashMap();
map.put("start", (currentPage-1)*pageSize);
map.put("max", pageSize);</span>
<span style="color:#ff0000;"> List<Member> list = memberDao.selectMembersByPage(map); </span>
System.out.println(list.size()+"|||"+totalCount);
/**
* responseBody 将返回值封装成json返回给客户端
*/
Map json = new HashMap();
json.put("list",list);
json.put("totalCount", totalCount);
json.put("totalPage", totalPage); return json;
}
}

下面是我项目文件的结构图:

版权声明:本文为博主原创文章,未经博主允许不得转载。

spring MVC(十)---spring MVC整合mybatis的更多相关文章

  1. Spring Boot入门系列(十八)整合mybatis,使用注解的方式实现增删改查

    之前介绍了Spring Boot 整合mybatis 使用xml配置的方式实现增删改查,还介绍了自定义mapper 实现复杂多表关联查询.虽然目前 mybatis 使用xml 配置的方式 已经极大减轻 ...

  2. Spring Boot入门系列(十九)整合mybatis,使用注解实现动态Sql、参数传递等常用操作!

    前面介绍了Spring Boot 整合mybatis 使用注解的方式实现数据库操作,介绍了如何自动生成注解版的mapper 和pojo类. 接下来介绍使用mybatis 常用注解以及如何传参数等数据库 ...

  3. Spring Boot2 系列教程 (十三) | 整合 MyBatis (XML 版)

    前言 如题,今天介绍 SpringBoot 与 Mybatis 的整合以及 Mybatis 的使用,之前介绍过了 SpringBoot 整合MyBatis 注解版的使用,上一篇介绍过 MyBatis ...

  4. Spring (六):整合Mybatis

    本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接 https://space.bilibili.com/95256449?spm_id_from=33 ...

  5. spring boot(二)整合mybatis plus+ 分页插件 + 代码生成

    先创建spring boot项目,不知道怎么创建项目的 可以看我上一篇文章 用到的环境 JDK8 .maven.lombok.mysql 5.7 swagger 是为了方便接口测试 一.Spring ...

  6. Java开发学习(三十九)----SpringBoot整合mybatis

    一.回顾Spring整合Mybatis Spring 整合 Mybatis 需要定义很多配置类 SpringConfig 配置类 导入 JdbcConfig 配置类 导入 MybatisConfig ...

  7. Spring Boot 知识笔记(整合Mybatis)

    一.pom.xml中添加相关依赖 <!-- 引入starter--> <dependency> <groupId>org.mybatis.spring.boot&l ...

  8. Spring入门(四)——整合Mybatis

    1. 准备jar包及目录结构 2. 配置db.properties driver = com.mysql.jdbc.Driver url = jdbc:mysql://127.0.0.1:3306/H ...

  9. Spring Boot数据访问之整合Mybatis

    在Mybatis整合Spring - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中谈到了Spring和Mybatis整合需要整合的点在哪些方面,需要将Mybatis中数据库连接池等相关对 ...

随机推荐

  1. 【uWSGI】实战之Django配置经验

    uWSGI 是应用服务器,实现了uwsgi, wsgi等协议,可以运行wsgi 应用 uwsgi 是协议名 Django配置 下面是自己经常用的一个配置模板,基于1.9以上的版本使用的, 主要基于dj ...

  2. vimgrep 搜索总结

    vimgrep /匹配模式/[g][j] 要搜索的文件/范围  g:表示是否把每一行的多个匹配结果都加入 j:表示是否搜索完后定位到第一个匹配位置 vimgrep /pattern/ %       ...

  3. 14.QT-QFile文件,QBuffer缓冲区,QDir目录,QFileSystemWatcher文件系统监视

    QFile Qt中所有与IO相关的类都继承于QIODevice,继承图如下所示: 其中QFile类便是用于文件操作的类 在QT中,将文件当做一种特殊的外部设备对待(比如:串口,usb等就是外部设备) ...

  4. Spring Boot 添加jersey-mvc-freemarker依赖后内置tomcat启动不了解决方案

    我在我的Spring Boot 项目的pom.xml中添加了jersey-mvc-freemarker依赖后,内置tomcat启动不了. 报错信息如下: org.springframework.con ...

  5. CALayer的属性和使用

    一.CALayer的常用属性 1.@propertyCGPoint position; 图层中心点的位置,类似与UIView的center:用来设置CALayer在父层中的位置:以父层的左上角为原点( ...

  6. java main方法执行sql语句

    public static void main(String[] args) throws Exception{ String driver = "oracle.jdbc.driver.Or ...

  7. CRM客户关系管理系统(十二)

    十二章.学员报名流程开发 2  12.1.学员报名合同和证件信息上传 功能: 必须勾选报名合同协议 必须上传个人证件信息 最多只能上传三个文件 文件大小2M以内 列出已上传文件 (1)crm/urls ...

  8. 每天几分钟跟小猫学前端之node系列:用node实现最简单的爬虫

    先来段求分小视频: https://www.iesdouyin.com/share/video/6550631947750608142/?region=CN&mid=6550632036246 ...

  9. 使用 JMeter 进行压力测试

    一.前言 压力测试是每一个Web应用程序上线之前都需要做的一个测试,他可以帮助我们发现系统中的瓶颈问题,减少发布到生产环境后出问题的几率:预估系统的承载能力,使我们能根据其做出一些应对措施.所以压力测 ...

  10. 基于Django的独立运行脚本开发

    1.在Django框架下工作时间长了,会对Django的技术设施产生依赖,比如其方便的ORM,如果写基于Django的独立运行脚本,主要在脚本前面加上以下代码: import sys,os,djang ...