MyBatis的核心技术掌握---分页功能,详细易懂(下)

news/2024/7/16 8:20:21 标签: mybatis, spring, java, ide, intellij idea

目录

一.前言

二.MyBatis 的分页

三.MyBatis 的特殊字符处理


一.前言

        继上篇MyBatis 的文章,我们继续来学习MyBatis吧!!! 上篇的博客链接:           http://t.csdn.cn/5iUEDhttp://t.csdn.cn/5iUED        接下来进入我们的正文吧!!!

二.MyBatis 的分页

         导入pom文件,分页插件,需要借助这些来完成分页

pom.xml配置文件:

  <!--分页-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>

Mybatis.cfg.xml配置拦截器:

<plugins>
    <!-- 配置分页插件PageHelper, 4.0.0以后的版本支持自动识别使用的数据库 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
    </plugin>
</plugins>

配置分页工具类:

package com.zking.Utils;

import java.io.Serializable;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

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<String,String[]> 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<String, String[]> getMap() {
		return map;
	}

	public void setMap(Map<String, String[]> 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
				+ "]";
	}
}

        Mapper.xml文件:        

         测试:

三.MyBatis 的特殊字符处理

        要创建一个Dto包,这个是用来传参数的,比如还有一些像:pojo/entity/model 是用来放实体类的VO--view object 是视图对象,是用来展示 java.util.Map还有就是Dto 是用来接受参数的。

Dto 包,来接受分页的开始条目和结束条目的参数:

package com.zking.dto;

import com.zking.model.Book;

/**
 * @author yinzi
 * @create 2023-08-24 15:11
 */
public class BookDto extends Book {

    private float min;
    private float max;

    public float getMin() {
        return min;
    }

    public void setMin(float min) {
        this.min = min;
    }

    public float getMax() {
        return max;
    }

    public void setMax(float max) {
        this.max = max;
    }


}

        xml文件:

 测试:

 总结:

        如果在sql语句中碰到大于,小于符号,就需要像上面那样用<![CDATA[  逻辑代码   ]]> 包裹,或者上面那样也可这样替换:price &gt; #{min} and price &lt; #{max}


http://www.niftyadmin.cn/n/4965944.html

相关文章

win11 设置小任务栏

设置后效果 以下两种工具均可 1、StartAllBack 2、Start11

自动化测试平台seldom-platform部署及使用

介绍 seldom-platform是一个基于seldom测试框架的测试平台 项目地址&#xff1a;https://github.com/SeldomQA 文档&#xff1a;seldom 语雀 首先&#xff0c;专门为seldom测试框架提供平台化支持。其次&#xff0c;只负责自动化测试项目的解析、执行用例&#xff0c;当然…

开悟Optimization guide for intermediate tracks

目录 认识模型 参考方案&#xff08;按模块拆解&#xff09; 认识模型 模型控制1名英雄进行镜像1 v 1对战 Actor集群资源为64核CPU 问题特点&#xff1a;单一公平对抗场景&#xff08;同英雄镜像对赛&#xff09;&#xff0c;单位时间样本产能低&#xff0c;累计训练资源相…

前端常用js库总结

一、js常用工具类 1、lodash 一个一致性、模块化、高性能的JavaScript实用工具库&#xff1b; 2、ramda 提供了许多有用的方法&#xff0c;每一个JavaScript程序员应该掌握的工具&#xff1b; 3、day.js 一个轻量级处理时间和日期的javaScript库&#xff0c;和moment.js的…

openGauss学习笔记-49 openGauss 高级特性-索引推荐

文章目录 openGauss学习笔记-49 openGauss 高级特性-索引推荐49.1 单query索引推荐49.2 虚拟索引49.3 workload级别索引推荐 openGauss学习笔记-49 openGauss 高级特性-索引推荐 openGauss的索引推荐的功能&#xff0c;共包含三个子功能&#xff1a;单query索引推荐、虚拟索引…

十一、内部类(2)

本章概要 为什么需要内部类 闭包与回调内部类与控制框架 继承内部类内部类可以被重写么&#xff1f;局部内部类内部类标识符 为什么需要内部类 至此&#xff0c;我们已经看到了许多描述内部类的语法和语义&#xff0c;但是这并不能同答“为什么需要内部类”这个问题。那么&a…

29、简单通过git把项目远程提交到gitee

简单通过git把项目远程提交到gitee 1、在gitee上创建一个仓库 2、在要提交的项目文件夹打开git 输入 git init 初始化git 然后设置下用户名和邮箱 git config --global user.name “username” git config --global user.email “yourEmail” 因为我是要把文件简单提交到…

2023 ccpc 网络赛 L 题解

Problem L. Partially Free Meal 题面 官方题解 官方题解解读 w(k,x)计算部分 主席树常规做法&#xff0c;在一般主席树中多维护一个这个区间的总和就ok了 根据单调性分治求解部分 接下来我们来推导一下单调性&#xff0c;也就是题解中的 f ( 1 ) < f ( 2 ) < f ( …