Mybatis系列(三)之分页查询及特殊字符的处理

简介: Mybatis系列(三)之分页查询及特殊字符的处理

一. Mybatis分页查询

分页是我们在开发中绕不过去的一个坎!当你的数据量大了的时候,一次性将所有数据查出来不现实,所以我们一般都是分页查询的,减轻服务端的压力,提升了速度和效率!也减轻了前端渲染的压力!

传统的limit语句实现分页太过繁琐,这里使用一个Mybatis_PageHelper分页插件来完成分页查询的实现。

官方GitHub地址:

1. 导入pom依赖

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>

2. Mybatis核心文件配置拦截器

这里由于DTD约束,plugins标签需要放在environments标签之前。

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

3.添加PageBean工具类

package com.xissl.util;
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
        + "]";
  }
}

4. 案例实现

Mapper.xml

<select id="selectLike04" resultType="com.xissl.model.Book" parameterType="java.util.List" >
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bname like concat('%',#{bname},'%')
  </select>

Mapper层

List<Book> selectLike04(@Param("bname") String bname);

Service层

List<Book> selectLike04(String bname, PageBean pageBean);
@Override
    public List<Book> selectLike04(String bname, PageBean pageBean) {
        if(pageBean !=null && pageBean.isPagination()){
            PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        }
        List<Book> books = bookMapper.selectLike04(bname);
        if(pageBean !=null && pageBean.isPagination()){
            PageInfo<Book> info = new PageInfo<>(books);
            System.out.println("页码:" + info.getPageNum());
            System.out.println("页大小:" + info.getPageSize());
            System.out.println("总记录:" + info.getTotal());
            pageBean.setTotal(info.getTotal()+"");
        }
        return books;
    }

测试结果:

二. Mybatis特殊字符处理

2.1 用转义字符替换特殊字符

特殊字符 转义字符
< &lt;
> &gt;
& &amp;
' &apos;
" &quot;

示例代码:

<select id="query" resultType="com.xissl.model.Book" parameterType="com.xissl.dto.BookDto" >
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
    where
    price &gt; #{min} and price &lt; #{max}
  </select>

测试结果:

2.2 使用xml的![CDATA[ ]]语法

解析器不对CDATA区中的内容进行解析,而是将这些数据原封不动地交给下游程序处理。

示例代码:

<select id="query" resultType="com.xissl.model.Book" parameterType="com.xissl.dto.BookDto" >
    select
    <include refid="Base_Column_List" />
    from t_mvc_book
    where <![CDATA[
        price > #{min} and price < #{max}
    ]]>
  </select>

测试结果:

相关文章
|
8天前
|
Java 数据库连接 数据库
mybatis查询数据,返回的对象少了一个字段
mybatis查询数据,返回的对象少了一个字段
35 8
|
9天前
|
SQL XML Java
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
文章介绍了MyBatis中高级查询的一对多和多对一映射处理,包括创建数据库表、抽象对应的实体类、使用resultMap中的association和collection标签进行映射处理,以及如何实现级联查询和分步查询。此外,还补充了延迟加载的设置和用法。
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
|
5月前
|
SQL
MyBatis-Plus-Join关联查询
MyBatis-Plus-Join关联查询
234 2
|
5月前
|
SQL Java 关系型数据库
Mybatis多表关联查询与动态SQL(下)
Mybatis多表关联查询与动态SQL
114 0
|
5月前
|
SQL Java 数据库连接
Mybatis多表关联查询与动态SQL(上)
Mybatis多表关联查询与动态SQL
133 0
|
5月前
|
SQL 缓存 Java
mybatis 一对多查询
mybatis 一对多查询
82 0
|
5月前
|
SQL XML Java
MyBatis-Plus多表关联查询
MyBatis-Plus多表关联查询
456 0
|
3月前
|
Java 数据库连接 mybatis
Mybatis查询传递单个参数和传递多个参数用法
Mybatis查询传递单个参数和传递多个参数用法
53 11
MybatisPlus-标准CRUD制作,新增boolean save(T t),删除 ~ delete(int id),修改 ~ update(T t),根据id查询,T getById....
MybatisPlus-标准CRUD制作,新增boolean save(T t),删除 ~ delete(int id),修改 ~ update(T t),根据id查询,T getById....
MyBatisPlus如何根据id批量查询?Required request parameter ‘id‘ for method 解决方法是看青戈大佬MybatisPlus的教程
MyBatisPlus如何根据id批量查询?Required request parameter ‘id‘ for method 解决方法是看青戈大佬MybatisPlus的教程