CURD+分页
在pom.xml中增加对PageHelper的支持
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
在src/main/java下创建com.how2java.springboot.config包,其下创建PageHelperConfig类。注解@Configuration 表示PageHelperConfig 这个类是用来做配置的。
注解@Bean 表示启动PageHelper这个拦截器。
offsetAsPageNum:设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用.
rowBoundsWithCount:设置为true时,使用RowBounds分页会进行count查询.
reasonable:启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页。
RowBounds用于限制查询结果集的起始行和返回行数。实现分页查询。例如,可以使用RowBounds(0, 10)来表示从第1行开始返回10条记录。其中,第一个参数是查询结果集的起始行数,第二个参数是返回的行数
package com.how2java.springboot.config;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.pagehelper.PageHelper;
@Configuration
public class PageHelperConfig {
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
修改CategoryMapper,增加CRUD方法的支持。 实质是调用不同的SQL语句。
为CategoryController添加: 增加、删除、获取、修改映射
修改查询映射,@RequestMapping("/listCategory")下
1. 在参数里接受当前是第几页 start ,以及每页显示多少条数据 size。 默认值分别是0和5。 2. 根据start,size进行分页,并且设置id 倒排序
3. 因为PageHelper的作用,这里就会返回当前分页的集合了
4. 根据返回的集合,创建PageInfo对象
5. 把PageInfo对象扔进model,以供后续显示
6. 跳转到listCategory.jsp
package com.how2java.springboot.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.how2java.springboot.mapper.CategoryMapper;
import com.how2java.springboot.pojo.Category;
@Controller
public class CategoryController {
@Autowired CategoryMapper categoryMapper;
@RequestMapping("/addCategory")
public String listCategory(Category c) throws Exception {
categoryMapper.save(c);
return "redirect:listCategory";
}
@RequestMapping("/deleteCategory")
public String deleteCategory(Category c) throws Exception {
categoryMapper.delete(c.getId());
return "redirect:listCategory";
}
@RequestMapping("/updateCategory")
public String updateCategory(Category c) throws Exception {
categoryMapper.update(c);
return "redirect:listCategory";
}
@RequestMapping("/editCategory")
public String listCategory(int id,Model m) throws Exception {
Category c= categoryMapper.get(id);
m.addAttribute("c", c);
return "editCategory";
}
@RequestMapping("/listCategory")
public String listCategory(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
PageHelper.startPage(start,size,"id desc");
List<Category> cs=categoryMapper.findAll();
PageInfo<Category> page = new PageInfo<>(cs);
m.addAttribute("page", page);
return "listCategory";
}
}
修改listCategory.jsp
通过page.getList遍历当前页面的Category对象。
在分页的时候通过page.pageNum获取当前页面,page.pages获取总页面数。
注:page.getList会返回一个泛型是Category的集合。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div align="center">
</div>
<div style="width:500px;margin:20px auto;text-align: center">
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
<td>编辑</td>
<td>删除</td>
</tr>
<c:forEach items="${page.list}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
<td><a href="editCategory?id=${c.id}">编辑</a></td>
<td><a href="deleteCategory?id=${c.id}">删除</a></td>
</tr>
</c:forEach>
</table>
<br>
<div>
<a href="?start=1">[首 页]</a>
<a href="?start=${page.pageNum-1}">[上一页]</a>
<a href="?start=${page.pageNum+1}">[下一页]</a>
<a href="?start=${page.pages}">[末 页]</a>
</div>
<br>
<form action="addCategory" method="post">
name: <input name="name"> <br>
<button type="submit">提交</button>
</form>
</div>
创建editCategory.jsp,修改分类的页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<div style="margin:0px auto; width:500px">
<form action="updateCategory" method="post">
name: <input name="name" value="${c.name}"> <br>
<input name="id" type="hidden" value="${c.id}">
<button type="submit">提交</button>
</form>
</div>
重启测试访问
因为在pom中增加了新jar的依赖,所以要手动重启,重启后访问测试地址:http://127.0.0.1:8080/listCategory?start=6

mybatis-xml方式
将CategoryMapper中sql 语句的注解
在Mapper类旁边,新增加Category.xml(SQL映射文件)文件,里面就是放的这个sql语句
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.how2java.springboot.mapper.CategoryMapper">
<select id="findAll" resultType="Category">
select * from category_
</select>
</mapper>
修改application.properties, 增加一下代码,指明从哪里去找xml配置文件,同时利用aliases语句指定别名
mybatis.mapper-locations=classpath:com/how2java/springboot/mapper/*.xml
mybatis.type-aliases-package=com.how2java.springboot.pojo
访问测试地址:http://127.0.0.1:8080/listCategory

做本次作业时同时出现500和404错误,保存代码将整个程序删掉,按照自己保存的代码重做一边就没有出现问题,只是我没有完全按照网站提供的顺序来做,而是整个程序按照自己的理解做出了就没有报错。最后改编代码,进行了mybatis-xml的方式显示分页。
文章讲述了如何在SpringBoot项目中集成PageHelper库,通过配置PageHelperConfig类启用拦截器,并设置相关参数实现分页功能。在CategoryMapper中添加CRUD操作,CategoryController中处理增删改查请求并实现分页查询。此外,还展示了如何修改JSP页面显示分页信息,并介绍了将Mapper接口的SQL注解转换为XML映射文件的过程。
944

被折叠的 条评论
为什么被折叠?



