MyBatis入门——分页

MyBatis入门——分页

Tips:请先阅读:
一、MyBatis入门——第一个MyBatis项目搭建文章
https://blog.csdn.net/Gauss7/article/details/115264612
二、MyBatis入门——基本CRUD代码编写
https://blog.csdn.net/Gauss7/article/details/115280533
三、MyBatis入门——配置解析
https://blog.csdn.net/Gauss7/article/details/115309923
四、MyBatis入门——解决属性名和字段名不一致的问题
https://blog.csdn.net/Gauss7/article/details/115325116
五、MyBatis入门——日志
https://blog.csdn.net/Gauss7/article/details/115351129


一、使用limit分页

语法:SELECT * from user limit startIndex.pageSize;
select * from user limit 3;

使用Mybatis实现分页,核心SQL

  1. 接口

    //分页
    List<User> getUserByLimit(Map<String,Integer> map);
    
  2. Mapper.xml

    <select id="getUserByLimit" parameterType="map" resultMap="UserMap">
        select * from user limit #{startIndex},#{pageSize};
    </select>
    
  3. 测试

    @Test
    public void getUserByLimit(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("startIndex",0);
        map.put("pageSize",2);
    
        List<User> userList = mapper.getUserByLimit(map);
        for (User user : userList) {
            System.out.println(user);
        }
    
        sqlSession.close();
    }
    

二、RowBounds

不再使用SQL实现分页

  1. 接口

    //RowBounds分页
    List<User> getUserByRowBounds();
    
  2. mapper.xml

    <select id="getUserByRowBounds" resultMap="UserMap">
        select * from user;
    </select>
    
  3. 测试

    @Test
    public void getUserByRowBounds(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
    
        //RowBounds实现
        RowBounds rowBounds = new RowBounds(1,2);
    
        //通过Java层面实现分页
        List<User> userList = sqlSession.selectList("com.twy.dao.UserMapper.getUserByRowBounds",null,rowBounds);
    
        for (User user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值