Mybatis的分页功能可不可以通过数据库中的存储过程动态执行查询来帮助实现?
Spring、Mybatis、Mysql 通过存储过程实现分页博客一共有3部分
第一部分:存储过程动态分页之存储过程实现
第二部分:存储过程动态分页之Mybatis实现
第三部分:存储过程动态分页之实际工程demo
目前这篇讲的是
第三部分:存储过程动态分页之实际工程demo
项目介绍
eclipse,maven,spring4,mybatis3,c3p0,mysql
用到的mybatis插件:
MyBatis Velocity 链接:http://www.mybatis.org/velocity-scripting/index.html
Mybatis spring 链接:http://www.mybatis.org/spring/
MyBatis Generator 链接:http://www.mybatis.org/generator/
已上传至GitHub。
链接:
https://github.com/noobthinker/spring4_mybatis3_mysql_dynamic_paging
简单的在test库中建立了2个测试表
#user 表
CREATE TABLE `test`.`user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(50) DEFAULT NULL,
`userAge` int(11) DEFAULT NULL,
`userAddress` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
#fun_log 表
CREATE TABLE `test`.`fun_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`info` text DEFAULT NULL,
`user_id` bigint(20) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8;
插入了一些数据
user表
@Test
public void testAddUser(){
for(int i=1;i<=100;i++){
User user = new User();
user.setId(i);
user.setUserAddress(i+"--this is a test b.");
user.setUserAge(10+i);
user.setUserName(i+"--xkorey");
userService.addUser(user);
System.out.println(user.getId());
}
}
user 表内容
id userName userAge userAddress
1, '1--xkorey', 11, '1--this is a test b.'
2, '2--xkorey', 12, '2--this is a test b.'
3, '3--xkorey', 13, '3--this is a test b.'
4, '4--xkorey', 14, '4--this is a test b.'
5, '5--xkorey', 15, '5--this is a test b.'
6, '6--xkorey', 16, '6--this is a test b.'
7, '7--xkorey', 17, '7--this is a test b.'
8, '8--xkorey', 18, '8--this is a test b.'
9, '9--xkorey', 19, '9--this is a test b.'
10, '10--xkorey', 20, '10--this is a test b.'
fun_log表内容
id info user_id
1, '1--log info', 1
2, '2--log info', 1
3, '3--log info', 1
4, '4--log info', 1
5, '5--log info', 1
6, '6--log info', 1
7, '7--log info', 1
8, '8--log info', 1
9, '9--log info', 1
10, '10--log info', 1
fun_log表
@Test
public void testAddFunLog(){
for(long i=1;i<=100;i++){
FunLog log = new FunLog();
log.setId(i);
log.setUser_id(i);
log.setInfo(i+"--log info");
logService.addLog(log);
}
}
userservice
public interface UserService {
/**
* 分页获取用户
* @param begin 开始位置
* @param size 获取数量
* @return
*/
public List<User> getUserByList(Integer begin,Integer size);
/**
* 分页获取用户log信息
* @param userId 用户id
* @param begin 开始位置
* @param size 获取数量
* @return
*/
public List<FunLog> getUserLogsList(Integer userId,Integer begin,Integer size);
}
Test 执行结果
获取3条user记录
@Test
public void testGetUserByPage(){
List<User> users = userService.getUserByList(0,3);
System.out.println(users.size());
for(User user:users){
System.out.println(user.getUserName());
}
}
log 打印出的Mybatis 执行日志
==> Preparing: CALL dynamic_paging('select * from user',?,?)
==> Parameters: 0(Integer), 3(Integer)
<== Columns: id, userName, userAge, userAddress
<== Row: 1, 1--xkorey, 11, 1--this is a test b.
<== Row: 2, 2--xkorey, 12, 2--this is a test b.
<== Row: 3, 3--xkorey, 13, 3--this is a test b.
<== Total: 3
<== Updates: 0
console 输出获取到的记录
3
1--xkorey
2--xkorey
3--xkorey
例如:获取user表从第4条开始,获取6条记录
修改Test类
#List<User> users = userService.getUserByList(0,3);
List<User> users = userService.getUserByList(4,6);
Mybatis 执行日志
==> Preparing: CALL dynamic_paging('select * from user',?,?)
==> Parameters: 4(Integer), 6(Integer)
<== Columns: id, userName, userAge, userAddress
<== Row: 5, 5--xkorey, 15, 5--this is a test b.
<== Row: 6, 6--xkorey, 16, 6--this is a test b.
<== Row: 7, 7--xkorey, 17, 7--this is a test b.
<== Row: 8, 8--xkorey, 18, 8--this is a test b.
<== Row: 9, 9--xkorey, 19, 9--this is a test b.
<== Row: 10, 10--xkorey, 20, 10--this is a test b.
<== Total: 6
<== Updates: 0
控制台输出
6
5--xkorey
6--xkorey
7--xkorey
8--xkorey
9--xkorey
10--xkorey
刚才演示了单个表的数据分页情况。
下面演示2个表数据分页情况。
先演示获取用户日志的sql 和执行情况
SELECT * FROM user,fun_log f where f.user_id=user.id and user.id=1;
执行结果
user.id userName userAge userAddress fun_log.id info fun_log.user_id
1, '1--xkorey', 11, '1--this is a test b.', 1, '1--log info', 1
1, '1--xkorey', 11, '1--this is a test b.', 2, '2--log info', 1
1, '1--xkorey', 11, '1--this is a test b.', 3, '3--log info', 1
1, '1--xkorey', 11, '1--this is a test b.', 4, '4--log info', 1
1, '1--xkorey', 11, '1--this is a test b.', 5, '5--log info', 1
1, '1--xkorey', 11, '1--this is a test b.', 6, '6--log info', 1
1, '1--xkorey', 11, '1--this is a test b.', 7, '7--log info', 1
1, '1--xkorey', 11, '1--this is a test b.', 8, '8--log info', 1
1, '1--xkorey', 11, '1--this is a test b.', 9, '9--log info', 1
1, '1--xkorey', 11, '1--this is a test b.', 10, '10--log info', 1
Test 方法
@Test
public void testGetUserLogsByPage(){
List<FunLog> logs = userService.getUserLogsList(1,0,2);
System.out.println(logs.size());
for(FunLog log:logs){
System.out.println(log.getInfo());
}
}
Mybatis 执行日志
==> Preparing: CALL dynamic_paging(?,?,?)
==> Parameters: SELECT * FROM user,fun_log f where f.user_id=user.id and user.id=1(String), 0(Integer), 2(Integer)
<== Columns: id, userName, userAge, userAddress, id, info, user_id
<== Row: 1, 1--xkorey, 11, 1--this is a test b., 1, <<BLOB>>, 1
<== Row: 1, 1--xkorey, 11, 1--this is a test b., 2, <<BLOB>>, 1
<== Total: 2
<== Updates: 0
console 输出
2
1--log info
2--log info
可以看到关键部分:
SELECT * FROM user,fun_log f where f.user_id=user.id and user.id=1(String), 0(Integer), 2(Integer)
是Mybatis实现动态查询的关键部分。
再次修改下测试类,获取从user id 是1 的log 第4条开始,获取6条记录
#List<FunLog> logs = userService.getUserLogsList(1,0,2);
List<FunLog> logs = userService.getUserLogsList(1,4,6);
Mybatis 执行日志
Preparing: CALL dynamic_paging(?,?,?)
==> Parameters: SELECT * FROM user,fun_log f where f.user_id=user.id and user.id=1(String), 4(Integer), 6(Integer)
<== Columns: id, userName, userAge, userAddress, id, info, user_id
<== Row: 1, 1--xkorey, 11, 1--this is a test b., 5, <<BLOB>>, 1
<== Row: 1, 1--xkorey, 11, 1--this is a test b., 6, <<BLOB>>, 1
<== Row: 1, 1--xkorey, 11, 1--this is a test b., 7, <<BLOB>>, 1
<== Row: 1, 1--xkorey, 11, 1--this is a test b., 8, <<BLOB>>, 1
<== Row: 1, 1--xkorey, 11, 1--this is a test b., 9, <<BLOB>>, 1
<== Row: 1, 1--xkorey, 11, 1--this is a test b., 10, <<BLOB>>, 1
<== Total: 6
<== Updates: 0
console输出
6
5--log info
6--log info
7--log info
8--log info
9--log info
10--log info
- 大小: 89.8 KB
分享到:
相关推荐
前后端分离后端框架:springcloud集成mysql+mybatis+分页可运行demo
本项目前端采用jQuery-bootstrap-bootstraptable,后台采用Spring-SpringMVC-Mybatis-Mysql架构。由bootstraptable的设置实现数据库查询分页,页面显示上采用分页显示。并实现了数据的增删改查的弹窗以及操作。是...
一个很简单的mybatis分页demo,数据库是MySQL,页面是Bootstrap
Java Spring boot框架下mybatis +分页的简单接口demo 数据库为mysql 数据库 表 user 字段userId userName password phone
spring、springMVC、mybatis整合demo以及分页demo,内含mysql数据库文件
springboot项目,基于SpringBoot+Vue实现增删改查分页DEMO(源码+数据库) 技术栈 数据库:MySQL 后端框架:SpringBoot+Spring Data JPA 前端框架:Vue-ElementUI
springboot创建集成的demo,pagehelper分页插件,mybatis集成mysql的一对多,多对多,Swagger的API分页插件,下载即用非常方便
利用springboot搭建项目,用mybatis的xml文件写sql语句连接mysql数据库,前端页面利用thymeleaf绑定数据。实现分页展示学生数据信息,删除学生,查看详情,添加学生,编辑学生的基本功能。适合小白学习使用。
使用spring集成的mybatis3垂直和水平分库mysql。使用模版编程,采用代理模式,采集变化的细分,自动完成拼写SQL,降低数据库落地缺陷集成Mybatis-PageHelper分页,大数据量可以分批查询,提升查询速度 将同步存储可...
2. MyBatis采用目前流行的通用Mapper方式进行DAO开发,避免单表在xml写sql语句 3、采用PageHelper分页插件 4、spring boot包含 dev、prod两个环境配置文件 5、集成 swagger-ui,方便Controller层接口展示和调试 6、...
spring boot的demo,实现分页模糊匹配查询等等,数据库持久层使用mybatis,数据库使用mysql,访问外部静态文件配置,h5视频显示
myeclipse搭建Spring+Spring Mvc+Mybatis+Maven+数据库Mysql项目demo,实现ajax分页
springboot-restful-demoSpring Boot 构建一个Restful架构风格的web项目,springboot + mybatis 整合 jersey框架实现微服务架构运行环境JDK8, spring boot 2.0.3, apache-maven-3.0.4, mysql数据库, redis服务...
Spring Boot Demo 是一个基于Spring Boot & MyBatis的种子项目,用于快速构建中小型项目。 特性&提供 集成SpringFox-Swagger2,自动生成swagger注释 统一响应结果封装及生成工具 统一异常处理 常用基础方法抽象...
这里给大家介绍的是SSM_CRUD的demo项目整合,CRUD(create 创建、retrieve 查询、update 更新、delete 删除)也是SSM框架中最为常用的工程功能。 具体工程的搭建可参考博客: ssm_crud 项目的功能点: 1、分页 2、数据...
基于SSM(Sping+Spring MVC+MyBatis)框架的校园宽带业务管理系统.zip # GZHMTJavaEE课设项目——校园宽带业务管理系统 本项目内容JavaEE期末课设,将保持持续更新; ## 目录 * [GZHMTJavaEE课设项目——校园宽带...
使用SpringMVC+Spring+Mybatis以及maven的后台管理系统. 程序的结构分为表现层(Controller)、业务层(Service)、数据层(Dao),包括权限授权、分页查询、日志记录,订单及产品查询,授权管理等功能 入门级DEMO 博客...
Spring+SpringMVC+Mybatis框架集成公共模块,包括公共配置、MybatisGenerator扩展插件、通用BaseService、工具类等。 > zheng-admin 基于bootstrap实现的响应式Material Design风格的通用后台管理系统,`zheng`...
项目技术: maven spring spring boot mysql mybatis druid redis lettuce swagger2 项目结构: spring-boot-study 父项目 web web项目继承自spring-boot-...项目提供了分页查询的demo 项目提供了token登录的实现基础
spring boot的demo2,实现分页模糊匹配查询等等,数据库持久层使用mybatis,数据库使用mysql,访问外部静态文件配置,h5视频显示