MyBatis的分页操作(MySQL)

1.无条件分页:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--namespace写成类的全限定名有好处,在Dao中方便-->
<mapper namespace="com.winner.entity.Student">

    <!--type是类的全限定名,因为mybatis.xml中有别名的设置,所以用别名,短,方便-->
    <resultMap id="studentMap" type="Student">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sal" column="sal"/>
    </resultMap>

    <!--这里做一个约定,返回值类型以后都写resultMap的id-->
    <!--SQL语句这样写易读性更好-->
    <select id="findAllWithPage" parameterType="map" resultMap="studentMap">
        SELECT id,name,sal
        FROM student
        LIMIT #{pstart},#{psize}
    </select>
</mapper>
public class StudentDao {

    /**
     * 无条件分页
     * @param start 表示在mysql中从第几条记录的索引号开始显示,索引从0开始
     * @param size 表示在mysql中最多显示几条记录
     */
    public List<Student> findAllWithPage(int start,int size) throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            Map<String,Object> map = new LinkedHashMap<String,Object>();
            map.put("pstart",start);
            map.put("psize",size);
            return sqlSession.selectList(Student.class.getName() + ".findAllWithPage", map);
        }catch(Exception ex){
            ex.printStackTrace();
            throw ex;
        }finally{
            MybatisUtil.closeSqlSession();
        }
    }

    public static void main(String[] args) throws Exception{
        StudentDao dao = new StudentDao();
        System.out.println("--------------------第一页");
        List<Student> studentList1 = dao.findAllWithPage(0,3);
        for(Student s : studentList1){
            System.out.println(s.getId() + " : " + s.getName() + " : " + s.getSal());
        }
        System.out.println("--------------------第二页");
        List<Student> studentList2 = dao.findAllWithPage(3,3);
        for(Student s : studentList2){
            System.out.println(s.getId() + " : " + s.getName() + " : " + s.getSal());
        }
        System.out.println("--------------------第三页");
        List<Student> studentList3 = dao.findAllWithPage(6,3);
        for(Student s : studentList3){
            System.out.println(s.getId() + " : " + s.getName() + " : " + s.getSal());
        }
        System.out.println("--------------------第四页");
        List<Student> studentList4 = dao.findAllWithPage(9,3);
        for(Student s : studentList4){
            System.out.println(s.getId() + " : " + s.getName() + " : " + s.getSal());
        }

    }
}

 

2.带条件的分页

<select id="findAllByNameWithPage" parameterType="map" resultMap="studentMap">
    SELECT id,name,sal
    FROM student
    WHERE name LIKE #{pname}
    limit #{pstart},#{psize}
</select>
 /**
 * 有条件分页
 */
public List<Student> findAllByNameWithPage(String name,int start,int size) throws Exception{
    SqlSession sqlSession = null;
    try{
        sqlSession = MybatisUtil.getSqlSession();
        Map<String,Object> map = new LinkedHashMap<String, Object>();
        map.put("pname","%"+name+"%");
        map.put("pstart",start);
        map.put("psize",size);
        return sqlSession.selectList(Student.class.getName()+".findAllByNameWithPage",map);
    }catch(Exception ex){
        ex.printStackTrace();
        throw ex;
    }finally{
       MybatisUtil.closeSqlSession();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值