1. 程式人生 > >關於springboot分頁查詢

關於springboot分頁查詢

1.//配置mybatis的分頁外掛pageHelper
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty(“offsetAsPageNum”,”true”);
properties.setProperty(“rowBoundsWithCount”,”true”);
properties.setProperty(“reasonable”,”true”);
properties.setProperty(“dialect”,”mysql”); //配置mysql資料庫的方言
pageHelper.setProperties(properties);
return pageHelper;
}

2.pom依賴

    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.0.0</version>
    </dependency>

3.控制層
//設定分頁資訊,分別是當前頁數和每頁顯示的總記錄數【記住:必須在mapper介面中的方法執行之前設定該分頁資訊】
PageHelper.startPage(p, 5);

        //景區資料列表
        List<ScenicOrder> scenicOrders = scenicOrderService.findScenicOrder(vo);
        //景區列表總記錄數
        int count = scenicOrderService.findSOCount(vo);

        PageBean<ScenicOrder> pageData = new PageBean<>(p, 5, count);
        pageData.setItems(scenicOrders);
        Map map = new HashMap();
        Map map1 = new HashMap();
        map.put("total", count);
        map.put("numberOfPages", pageData.getTotalPage());
        map.put("page_size", pageData.getPageSize());
        map.put("current_page", pageData.getCurrentPage());

        map1.put("list", pageData.getItems());
        map1.put("pagination", map);

4.pagebean實體類
public class PageBean {
// 當前頁
private Integer p = 1;
// 每頁顯示的總條數
private Integer pageSize = 10;
// 總條數
private Integer totalNum;
// 是否有下一頁
private Integer isMore;
// 總頁數
private Integer totalPage;
// 開始索引
private Integer startIndex;
// 分頁結果
private List items;

public PageBean() {
    super();
}

public PageBean(Integer currentPage, Integer pageSize, Integer totalNum) {
    super();
    this.p = currentPage;
    this.pageSize = pageSize;
    this.totalNum = totalNum;
    this.totalPage = (this.totalNum+this.pageSize-1)/this.pageSize;
    this.startIndex = (this.p-1)*this.pageSize;
    this.isMore = this.p >= this.totalPage?0:1;
}

public Integer getCurrentPage() {
    return p;
}

public void setCurrentPage(Integer currentPage) {
    this.p = currentPage;
}

public Integer getPageSize() {
    return pageSize;
}

public void setPageSize(Integer pageSize) {
    this.pageSize = pageSize;
}

public Integer getTotalNum() {
    return totalNum;
}

public void setTotalNum(Integer totalNum) {
    this.totalNum = totalNum;
}

public Integer getIsMore() {
    return isMore;
}

public void setIsMore(Integer isMore) {
    this.isMore = isMore;
}

public Integer getTotalPage() {
    return totalPage;
}

public void setTotalPage(Integer totalPage) {
    this.totalPage = totalPage;
}

public Integer getStartIndex() {
    return startIndex;
}

public void setStartIndex(Integer startIndex) {
    this.startIndex = startIndex;
}

public List<T> getItems() {
    return items;
}

public void setItems(List<T> items) {
    this.items = items;
}

}

模糊查詢
1、mysql :LIKE CONCAT(‘%’,#{empname},’%’ ) 或者 LIKE CONCAT(‘%’,‘${empname}’,’%’ )

2、oracle:LIKE ‘%’||#{empname}||’%’
3.like ‘%customnameconcat({customname}’,’%’)

sdr.setCheckStatusStr(sdr.getCheckstatus()==0?”未檢”:”已檢”);