其他分享
首页 > 其他分享> > MyBatis 分页查询

MyBatis 分页查询

作者:互联网

1. 利用 limit 关键字

接口定义:

public interface AccountMapper {
    // map 类型可以传入多个参数
    List<Account> findPage(Map<String, Integer> param);
}

select 标签:

<select id="findPage" resultMap="pageMap">
    select  * from t_account limit #{offset},#{limit}
</select>

测试:

@Test
public void testFindPage() {
    Map<String, Integer> map = new HashMap<>();
    // 因为 xml 中指定的属性为 offset 和 limit
    // 所以 map 传入的键需与之对应
    map.put("offset", 0);
    map.put("limit", 2);
    List<Account> page = accountMapper.findPage(map);
    System.out.println(page);
}

2. 使用 RowBounds(了解)

接口定义:

public interface AccountMapper {
    // 使用 RowBounds 作为入参
    List<Account> findPage2(RowBounds rowBounds);
}

select 标签:

<select id="findPage2" resultMap="pageMap">
    select * from t_account
</select>

测试:

@Test
public void testFindPage2() {
    RowBounds rowBounds = new RowBounds(0, 2);
    List<Account> accountList = accountMapper.findPage2(rowBounds);
    System.out.println(accountList);
}

3. PageHelper 插件

标签:map,分页,List,查询,limit,select,MyBatis,public,RowBounds
来源: https://blog.csdn.net/qq_44713454/article/details/110328422