【SSM 5】Mybatis分页插件的使用
来源:互联网 发布:java api手机安卓版 编辑:程序博客网 时间:2024/06/08 17:10
一、添加maven依赖项
<span style="font-family:KaiTi_GB2312;font-size:18px;"><dependency><groupId>com.github.miemiedev</groupId><artifactId>mybatis-paginator</artifactId></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId></dependency></span>
版本号:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><pagehelper.version>3.4.2-fix</pagehelper.version><mybatis.paginator.version>1.2.15</mybatis.paginator.version></span>
二、Mybatis配置文件(SqlMapConfig.xml)增加分页插件
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis3 config.dtd"><configuration><!-- 配置分页插件 --><plugins><plugin interceptor="com.github.pagehelper.PageHelper"><!-- 设置数据库类型Oracle,MySQL,MarinDBName,SQLite,PostareSQL六种数据库 --><property name="dialect" value="mysql"/></plugin></plugins></configuration></span>
三、spring整合
<span style="font-family:KaiTi_GB2312;font-size:18px;"></bean><!-- 让spring管理sqlsessionfactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 数据库连接池 --><property name="dataSource" ref="dataSource" /><!-- 加载mybatis的全局配置文件 --><property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /></bean><!-- 配置扫描包,加载mapper代理对象 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="Angel.mapper" /></bean></span>
四、service分页实现
<span style="font-family:KaiTi_GB2312;font-size:18px;">@Overridepublic EUDataGridResult selectAll(int pageNum,int pageSize) {//设置分页的参数PageHelper.startPage(pageNum, pageSize);//查询数据List<TbUser> list=userMapper.selectAll();//创建一个返回值对象EUDataGridResult result=new EUDataGridResult();result.setRows(list);//取记录总条数PageInfo<TbUser> pageInfo=new PageInfo<>(list);result.setTotal(pageInfo.getTotal());return result;}</span>
附:EUDataGridResult 类
<span style="font-family:KaiTi_GB2312;font-size:18px;">package Angel.pojo;import java.util.List;public class EUDataGridResult {private long total;private List<?> rows;public long getTotal() {return total;}public void setTotal(long total) {this.total = total;}public List<?> getRows() {return rows;}public void setRows(List<?> rows) {this.rows = rows;}}</span>
五、controller实现
<span style="font-family:KaiTi_GB2312;font-size:18px;">@RequestMapping("/user/findAll")@ResponseBodypublic EUDataGridResult getItemList(@RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="10")Integer rows,HttpServletResponse response) throws IOException{EUDataGridResult result=userService.selectAll(page, rows);return result;}</span>
六、JSP页面
<span style="font-family:KaiTi_GB2312;font-size:18px;"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!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><link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.4.1/themes/default/easyui.css" /><link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.4.1/themes/icon.css" /><script type="text/javascript" src="js/jquery-easyui-1.4.1/jquery.min.js"></script><script type="text/javascript" src="js/jquery-easyui-1.4.1/jquery.easyui.min.js"></script><script type="text/javascript" src="js/jquery-easyui-1.4.1/locale/easyui-lang-zh_CN.js"></script></head><body><table class="easyui-datagrid" id="itemList" title="用户列表" data-options="url:'<%=basePath %>user/findAll',pageSize:10,pagination:true"> <thead> <tr> <th data-options="field:'id',width:70">ID</th> <th data-options="field:'username',width:60">名称</th> <th data-options="field:'phone',width:70">电话</th> <th data-options="field:'email',width:70">邮箱</th> <th data-options="field:'created',width:130,align:'center'">创建日期</th> <th data-options="field:'updated',width:130,align:'center'">更新日期</th> </tr> </thead></table></body></html></span>
七、总结
在最开始的时候,想自己写一个分页实现,但是,后来就发现有Mybatis已经封装好的分页插件,就简单的配置一下就可以使用了。到现在还是感觉,自己对已有资源的使用还不够,每次都闹着自己创新创造,但是,先向别人学习,这一步也很重要。
然后在实现的时候,先是和王高高弄这个分页插件,不知道为什么,她那儿弄了好久都没有成功,后来自己回来写demon,发现一下子就成功了。我想,肯定是少配置文件了!
1 0
- 【SSM 5】Mybatis分页插件的使用
- 【SSM 5】Mybatis分页插件的使用
- 基于ssm框架的mybatis pagehelper分页插件的使用
- mybatis分页插件的使用
- 【MyBatis】MyBatis分页插件PageHelper的使用
- 【MyBatis】MyBatis分页插件PageHelper的使用
- 【MyBatis】MyBatis分页插件PageHelper的使用
- ssm 使用 PageHelper 插件分页
- 在 SSM 中基于 MyBatis-PageHelper 分页插件的分页功能实现
- Mybatis中分页插件PageHelper的使用
- Mybatis分页插件-PageHepler的使用
- Mybatis分页插件-PageHelper的使用
- mybatis分页插件PageHelper的使用
- Mybatis分页插件PageHelper的使用
- MyBatis分页插件PageHelper的使用
- Mybatis分页插件PageHelper的使用
- Mybatis分页插件PageHelper的简单使用
- MyBatis分页插件PageHelper的使用
- 1107. Social Clusters (30) 并查集
- Divisors
- OpenCV 系列---交互(二)鼠标
- Linux下的文件操作命令(五、文件内容统计命令)
- 前端面试的思路
- 【SSM 5】Mybatis分页插件的使用
- react开发思维方式入门
- hash
- C++智能指针简单剖析
- webpack学习笔记
- 前端面试基础题
- springMVC框架下如何实现移动端接口调用——代码实例
- callee、caller、call、apply、bind这些方法的含义和使用
- 个人总结