package com.YU.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author YU
* @create 2023-09-07 14:41
*/
@Controller
public class PageController {
@RequestMapping("/page/{page}")
public String toPage(@PathVariable("page") String page){
return page;
}
@RequestMapping("/page/{dir}{page}")
public String toDirPage(
@PathVariable("dir") String dir,
@PathVariable("page") String page){
return dir + "/" + page;
}
}
在生成后的HBookMapper接口中,需要加上@Repository给Spring进行托管
2.1切面类:
package com.YU.aspect;
import com.YU.utils.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author YU
* @create 2023-08-25 17:17
*/
@Aspect //代表当前类为切面类
@Component //代表当前类交给Spring进行管理
public class PagerAspect {
@Around("execution(* *..*biz.*Pager(..))")
public Object invoke(ProceedingJoinPoint args) throws Throwable {
PageBean pageBean = null;
//获取目标方法的所有参数
Object[] ars = args.getArgs();
for (Object param : ars) {
if(param instanceof PageBean){
pageBean = (PageBean)param;
break;
}
}
if(pageBean != null && pageBean.isPagination())
PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
Object list = args.proceed();
if(null != pageBean && pageBean.isPagination()){
PageInfo pageInfo = new PageInfo((List) list);
pageBean.setTotal((int) pageInfo.getTotal());
}
return list;
}
}
2.2 工具类
package com.YU.utils;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Map;
public class PageBean implements Serializable {
private static final long serialVersionUID = 2422581023658455731L;
//页码
private int page=1;
//每页显示记录数
private int rows=10;
//总记录数
private int total=0;
//是否分页
private boolean isPagination=true;
//上一次的请求路径
private String url;
//获取所有的请求参数
private Map map;
public PageBean() {
super();
}
//设置请求参数
public void setRequest(HttpServletRequest req) {
String page=req.getParameter("page");
String rows=req.getParameter("rows");
String pagination=req.getParameter("pagination");
this.setPage(page);
this.setRows(rows);
this.setPagination(pagination);
this.url=req.getContextPath()+req.getServletPath();
this.map=req.getParameterMap();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public void setPage(String page) {
if(null!=page&&!"".equals(page.trim()))
this.page = Integer.parseInt(page);
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public void setRows(String rows) {
if(null!=rows&&!"".equals(rows.trim()))
this.rows = Integer.parseInt(rows);
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal(String total) {
this.total = Integer.parseInt(total);
}
public boolean isPagination() {
return isPagination;
}
public void setPagination(boolean isPagination) {
this.isPagination = isPagination;
}
public void setPagination(String isPagination) {
if(null!=isPagination&&!"".equals(isPagination.trim()))
this.isPagination = Boolean.parseBoolean(isPagination);
}
/**
* 获取分页起始标记位置
* @return
*/
public int getStartIndex() {
//(当前页码-1)*显示记录数
return (this.getPage()-1)*this.rows;
}
/**
* 末页
* @return
*/
public int getMaxPage() {
int totalpage=this.total/this.rows;
if(this.total%this.rows!=0)
totalpage++;
return totalpage;
}
/**
* 下一页
* @return
*/
public int getNextPage() {
int nextPage=this.page+1;
if(this.page>=this.getMaxPage())
nextPage=this.getMaxPage();
return nextPage;
}
/**
* 上一页
* @return
*/
public int getPreivousPage() {
int previousPage=this.page-1;
if(previousPage<1)
previousPage=1;
return previousPage;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", isPagination=" + isPagination
+ "]";
}
}
package com.YU.biz;
import com.YU.model.HBook;
import com.YU.utils.PageBean;
import java.util.List;
public interface HBookbiz {
int deleteByPrimaryKey(Integer bid);
int insert(HBook record);
int insertSelective(HBook record);
HBook selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(HBook record);
int updateByPrimaryKey(HBook record);
List listPager(HBook hBook, PageBean pageBean);
}
注:
这个方法为查询返回列表方法,包含分页,因为切面类对其进行切面,所以在命名时,必须以Pager结尾
List selectBycon(HBook hBook);
package com.YU.tag;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.YU.utils.PageBean;
public class PageTag extends BodyTagSupport{
private PageBean pageBean;// 包含了所有分页相关的元素
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
@Override
public int doStartTag() throws JspException {
// 没有标签体,要输出内容
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() {
StringBuffer sb = new StringBuffer();
// 隐藏的form表单---这个就是上一次请求下次重新发的奥义所在
// 上一次请求的URL
sb.append("");
// 分页条
sb.append("");
// 分页执行的JS代码
sb.append("");
return sb.toString();
}
}
YU 1.1 core library
YU core
1.1
YU
http://jsp.veryedu.cn
page
com.YU.tag.PageTag
JSP
pageBean
true
true
package com.YU.web;
import com.YU.biz.HBookbiz;
import com.YU.model.HBook;
import com.YU.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* @author YU
* @create 2023-09-07 16:07
*/
@Controller
@RequestMapping("book")
public class HbookController {
@Autowired
private HBookbiz hBookbiz;
//增加
@RequestMapping("/add")
public String add(HBook hBook){
hBookbiz.insertSelective(hBook);
return "redirect:/book/list";
}
//删除
@RequestMapping("/del/{bid}")
public String del(@PathVariable("bid")Integer bid){
hBookbiz.deleteByPrimaryKey(bid);
return "redirect:/book/list";
}
//修改
@RequestMapping("/edit")
public String edit(HBook hBook){
hBookbiz.updateByPrimaryKeySelective(hBook);
return "redirect:list";
}
//查询
@RequestMapping("/list")
public String list(HBook hBook, HttpServletRequest request){
// hbook用来接收前台传递后台的参数
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
List hBooks = hBookbiz.listPager(hBook, pageBean);
request.setAttribute("lst",hBooks);
request.setAttribute("pageBean",pageBean);
return "book/list";
}
//数据回显
@RequestMapping("/preSave")
public String preSave(HBook hBook, Model model){
if(hBook !=null && hBook.getBid()!=null && hBook.getBid()!=0){
HBook b = hBookbiz.selectByPrimaryKey(hBook.getBid());
model.addAttribute("b",b);
}
return "book/edit";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
博客列表
书籍ID
书籍名称
书籍价格
<%-- 班级图片 --%>
操作
${b.bid }
${b.bname }
${b.price }
修改
删除
${pageBean }
感谢各位大大的收看,各位的支持和三连是博主更新的动力,感谢谢谢谢!!!!