Spring、Mybatis、Mysql 通过存储过程实现分页--工程demo

[size=large]Mybatis的分页功能可不可以通过数据库中的存储过程动态执行查询来帮助实现?[/size]

[size=large] Spring、Mybatis、Mysql 通过存储过程实现分页博客一共有3部分[/size]
[size=large]第一部分:存储过程动态分页之存储过程实现[/size]
[size=large]第二部分:存储过程动态分页之Mybatis实现[/size]
[size=large]第三部分:存储过程动态分页之实际工程demo[/size]

目前这篇讲的是
[size=large]第三部分:存储过程动态分页之实际工程demo[/size]


项目介绍

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。
链接:[url]https://github.com/noobthinker/spring4_mybatis3_mysql_dynamic_paging[/url]

简单的在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 执行结果

[size=large]获取3条user记录[/size]


@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


[size=large]例如:获取user表从第4条开始,获取6条记录[/size]

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


[size=large]刚才演示了单个表的数据分页情况。

下面演示2个表数据分页情况。[/size]


先演示获取用户日志的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


可以看到关键部分:
[size=large]
SELECT * FROM user,fun_log f where f.user_id=user.id and user.id=1(String), 0(Integer), 2(Integer)[/size]
是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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要在pom.xml中引入相应的依赖,这里以MySQL数据库为例: ```xml <!-- SpringBoot Web模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- SpringBoot Mybatis模块 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> ``` 其次,需要配置Mybatis和数据库连接,可以在application.yml中配置: ```yml spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC&useSSL=false&characterEncoding=utf8 username: root password: root mybatis: config-location: classpath:mybatis-config.xml mapper-locations: classpath:mapper/*.xml ``` 其中,`datasource`为数据源的基本信息,`mybatis`则是Mybatis的配置信息,包括配置文件的位置和Mapper文件的位置。 接下来,需要编写Mapper接口和对应的Mapper XML文件。以User表为例: ```java public interface UserMapper { List<User> findUserByPage(@Param("start") Integer start, @Param("pageSize") Integer pageSize); } ``` ```xml <mapper namespace="com.example.demo.mapper.UserMapper"> <select id="findUserByPage" resultType="com.example.demo.entity.User"> select * from user limit #{start},#{pageSize} </select> </mapper> ``` 其中,`findUserByPage`方法为分页查询方法,`start`为起始位置,`pageSize`为每页数量。 最后,编写Controller层和前端页面。以UserController为例: ```java @Controller public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/user") public String findUserByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, Model model) { Integer start = (pageNum - 1) * pageSize; List<User> userList = userMapper.findUserByPage(start, pageSize); PageInfo pageInfo = new PageInfo(userList); model.addAttribute("pageInfo", pageInfo); return "user"; } } ``` 其中,`findUserByPage`方法接收两个参数:`pageNum`和`pageSize`,表示当前页和每页数量。通过计算获得起始位置,调用Mapper接口进行分页查询,并通过`PageInfo`类将查询结果传递给前端页面。 在前端页面中通过`th:each`循环遍历查询结果,并通过`th:href`生成分页链接。以user.html为例: ```html <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>User List</title> </head> <body> <table border="1"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Age</th> <th>Gender</th> </tr> </thead> <tbody> <tr th:each="user : ${pageInfo.list}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.age}"></td> <td th:text="${user.gender}"></td> </tr> </tbody> </table> <div> <a th:href="@{/user?pageNum=1}">首页</a> <a th:href="@{/user?pageNum=${pageInfo.prePage}}">上一页</a> <a th:href="@{/user?pageNum=${pageInfo.nextPage}}">下一页</a> <a th:href="@{/user?pageNum=${pageInfo.pages}}">尾页</a> </div> </body> </html> ``` 其中,`pageInfo.list`为查询结果列表,通过`th:each`循环遍历生成表格数据。底部的分页链接则通过`th:href`生成相应的链接。 到这里,一个简单的分页查询就完成了。需要注意的是,以上代码仅为示例,具体实现方式可能会有所不同,需要按照实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值