欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis實(shí)現(xiàn)分頁查詢的詳細(xì)流程

 更新時間:2023年08月21日 08:42:04   作者:章魚小丸子duduu  
這篇文章主要給大家介紹了關(guān)于Mybatis實(shí)現(xiàn)分頁查詢的詳細(xì)流程,MyBatis是支持普通SQL查詢,存儲過程和高級映射的優(yōu)秀持久層框架,需要的朋友可以參考下

一. 簡單分頁查詢——limit

使用select查詢時,如果結(jié)果集數(shù)據(jù)量較大,一個頁面難以處理,就會采用分頁查詢。

分頁查詢,就是從結(jié)果集中拿出指定的第n頁到第m頁的數(shù)據(jù)來顯示。

// limit分頁公式 
// currentPage:當(dāng)前頁 
// pageSize:每頁記錄數(shù)
limit (currentPage-1) * pageSize,pageSize

// SQL語句
select * from student limit(currentPage-1)*pageSize,pageSize;

1. 基于注解的簡單分頁查詢

【Mapper接口】

@select("select * from student limit #{pageBegin},#{pageSize}")
List<Student> findByPage(@Param("pageBegin") Integer PageBegin,@Param("PageSize")Integer PageSize);

【Controller類·】

@GetMapping("/findByPage")
public List<Student> findByPage(Integer page,Integer pageSize){
    Integer pageBegin = (page-1) * pageSize;
    return StudentMapper.findByPage(pageBegin,pageSize);
}

二.基于mapper.xml的復(fù)雜分頁

1. 【定義Page類——封裝分頁結(jié)果】

/**
 * 分頁結(jié)果封裝對象
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class PageResult implements Serializable{
    private Long total;//總記錄數(shù)
    private List rows;//當(dāng)前頁結(jié)果
}

2.【定義PageResult類——封裝查詢條件】

封裝查詢條件
 請求參數(shù)包括頁碼、每頁顯示記錄數(shù)、查詢條件。
 請求參數(shù)的json格式為:{currentPage:1,pageSize:10,queryString:''apesource''}

// 分頁請求。
@AllArgsConstructor
@NoArgsConstructor
@Data
public class QueryPageBean implements Serializable{
    private Integer currentPage;//當(dāng)前頁碼
    private Integer pageSize;//每頁記錄數(shù)
    private String queryString;//查詢條件
    public QueryPageBean(Integer currentPage, Integer pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
    }
}

3.【Dao層】

/*** 持久層Dao接口 */
@Mapper
public interface CheckGroupDao {
    public Page<CheckGroup> selectByCondition(String queryString);
}

4.【xxxMapper.xml映射文件】

<!--動態(tài)查詢:分頁查詢-->
    <select id="selectByCondition" parameterType="string" resultType="com.apesource.graduation_project.pojo.CheckGroup">
        select * from t_checkgroup
        <if test="value != null and value.length > 0">
            where code = #{value} or name = #{value} or helpCode = #{value}
        </if>
    </select>

5.【Service層】

@Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString)                     {
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckGroup> page = checkGroupDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

6. 【Controller層】

 //分頁查詢
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = checkGroupService.pageQuery(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return pageResult;
        } catch (Exception e) {
            return null;
        }
    }

總結(jié) 

到此這篇關(guān)于Mybatis實(shí)現(xiàn)分頁查詢的文章就介紹到這了,更多相關(guān)Mybatis分頁查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論