spring MVC(十)---spring MVC整合mybatis
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的更多相关文章
- Spring Boot入门系列(十八)整合mybatis,使用注解的方式实现增删改查
之前介绍了Spring Boot 整合mybatis 使用xml配置的方式实现增删改查,还介绍了自定义mapper 实现复杂多表关联查询.虽然目前 mybatis 使用xml 配置的方式 已经极大减轻 ...
- Spring Boot入门系列(十九)整合mybatis,使用注解实现动态Sql、参数传递等常用操作!
前面介绍了Spring Boot 整合mybatis 使用注解的方式实现数据库操作,介绍了如何自动生成注解版的mapper 和pojo类. 接下来介绍使用mybatis 常用注解以及如何传参数等数据库 ...
- Spring Boot2 系列教程 (十三) | 整合 MyBatis (XML 版)
前言 如题,今天介绍 SpringBoot 与 Mybatis 的整合以及 Mybatis 的使用,之前介绍过了 SpringBoot 整合MyBatis 注解版的使用,上一篇介绍过 MyBatis ...
- Spring (六):整合Mybatis
本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接 https://space.bilibili.com/95256449?spm_id_from=33 ...
- spring boot(二)整合mybatis plus+ 分页插件 + 代码生成
先创建spring boot项目,不知道怎么创建项目的 可以看我上一篇文章 用到的环境 JDK8 .maven.lombok.mysql 5.7 swagger 是为了方便接口测试 一.Spring ...
- Java开发学习(三十九)----SpringBoot整合mybatis
一.回顾Spring整合Mybatis Spring 整合 Mybatis 需要定义很多配置类 SpringConfig 配置类 导入 JdbcConfig 配置类 导入 MybatisConfig ...
- Spring Boot 知识笔记(整合Mybatis)
一.pom.xml中添加相关依赖 <!-- 引入starter--> <dependency> <groupId>org.mybatis.spring.boot&l ...
- Spring入门(四)——整合Mybatis
1. 准备jar包及目录结构 2. 配置db.properties driver = com.mysql.jdbc.Driver url = jdbc:mysql://127.0.0.1:3306/H ...
- Spring Boot数据访问之整合Mybatis
在Mybatis整合Spring - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中谈到了Spring和Mybatis整合需要整合的点在哪些方面,需要将Mybatis中数据库连接池等相关对 ...
随机推荐
- 2010-01-20 12:09 ubuntu下minicom的安装及使用
转http://hi.baidu.com/npugtawqdnbgqrq/item/106f805409b42813db163527 ubuntu下minicom的安装及使用 安装: sudo apt ...
- ROS探索总结(十二)——坐标系统
在机器人的控制中,坐标系统是非常重要的,在ROS使用tf软件库进行坐标转换. 相关链接:http://www.ros.org/wiki/tf/Tutorials#Learning_tf 一.tf简介 ...
- git rebase之前需要commit才行
更新好本地代码后,git fetch, 接着合并,但是git rebase 不行, git status一看,有很多更新的文件. 于是 git add --后,再rebase,还是不行. 注意,reb ...
- coco2dx添加类报错
最近刚开始学习2dx,用的vs编辑器,现在说说我使用时碰到的一点小问题: 我使用的类添加向导,但是添加的类在win32目录下,而且编译的时候总是提示找不到 .h 文件 其实,这样添加类不是很好,可以在 ...
- 熊猫猪新系统测试之三:iOS 8.0.2
本来本猫要等到8.1版本出来后再做测试的,结果等来等去就是迟迟不推送更新呀!说好10月20号的iOS 8.1呢?为了一鼓作气写完,就先不等了.先拿手头的iOS 8.0.2系统做一下测试吧! 8.x系统 ...
- IIS服务器如何抗住高并发的客户端访问
今天被问到一个问题,如果你在阿里云上部署了一个IIS服务器,此时如果有成千上万的客户端来访问,你将如何设计?我东扯扯西谈谈,说加个线程池来处理,在加个请求队列.当时觉得说的没有问题,现在想想,服务器自 ...
- jQuery之select的option怎样绑定事件
HTML: <select id='select'> <option value='0'>上海</option> <option value='1'>北 ...
- Dapper.SimpleCRUD mysql 插入数据时出现的小插曲
最近想玩一下.net dapper,然后在nuget包中搜索看到了 Dapper.SimpleCRUD ,然后我等好奇心重的小骚年,内心又开始跃跃欲试. 使用sqlserver数据库时没有遇到问题,既 ...
- Python--简单接口测试实例(一)
适用人员:初学python的测试人员,若对抓包不太清楚的可先学习抓包的知识 接口测试流程:发送请求-->返回响应-->结果判定-->生成报告 案例:下面以[今目标]新建客户为例来进行 ...
- python---购物车---更新
购物车程序更新: 更新商家入口,实现以下功能: 1. 商家能够修改商品价格: 2. 商家能够下线商品: 3. 商家能够增加商品: 4. 商品信息存在文件中 # -*- coding:utf-8 -*- ...