java mybatis分页查询语句_mybatis-分页查询学习笔记

import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import org.apache.ibatis.session.SqlSession;import cn.itcast.javaee.mybatis.util.MybatisUtil;/** * 持久层  * @author AdminTC */public class StudentDao {    /**     * 增加学生     */    public void add(Student student) throws Exception{        SqlSession sqlSession = null;        try{            sqlSession = MybatisUtil.getSqlSession();            sqlSession.insert(Student.class.getName()+".add",student);            sqlSession.commit();        }catch(Exception e){            e.printStackTrace();            sqlSession.rollback();            throw e;        }finally{            MybatisUtil.closeSqlSession();        }    }    /**     * 无条件分页     * @param start 表示在mysql中从第几条记录的索引号开始显示,索引从0开始     * @param size 表示在mysql中最多显示几条记录     */    public List findAllWithFy(int start,int size) throws Exception{        SqlSession sqlSession = null;        try{            sqlSession = MybatisUtil.getSqlSession();            Map map = new LinkedHashMap();            map.put("pstart",start);            map.put("psize",size);            return sqlSession.selectList(Student.class.getName()+".findAllWithFy",map);        }catch(Exception e){            e.printStackTrace();            throw e;        }finally{            MybatisUtil.closeSqlSession();        }    }    /**     * 有条件分页     */    public List findAllByNameWithFy(String name,int start,int size) throws Exception{        SqlSession sqlSession = null;        try{            sqlSession = MybatisUtil.getSqlSession();            Map map = new LinkedHashMap();            map.put("pname","%"+name+"%");            map.put("pstart",start);            map.put("psize",size);                      return sqlSession.selectList(Student.class.getName()+".findAllByNameWithFy",map);        }catch(Exception e){            e.printStackTrace();            throw e;        }finally{            MybatisUtil.closeSqlSession();        }    }    public static void main(String[] args) throws Exception{        StudentDao dao = new StudentDao();        //for(int i=1;i<=10;i++){        //  dao.add(new Student(i,"哈哈",7000D));        //}        System.out.println("--------------------第一页");        List studentList1 = dao.findAllByNameWithFy("哈",0,3);        for(Student s : studentList1){            System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());        }        System.out.println("--------------------第二页");        List studentList2 = dao.findAllByNameWithFy("哈",3,3);        for(Student s : studentList2){            System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());        }        System.out.println("--------------------第三页");        List studentList3 = dao.findAllByNameWithFy("哈",6,3);        for(Student s : studentList3){            System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());        }    }}<?xml  version="1.0" encoding="UTF-8" ?>mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">                                             insert into students(students_id,students_name,students_sal)         values(#{id},#{name},#{sal});                select students_id,students_name,students_sal         from students         limit #{pstart},#{psize}                select students_id,students_name,students_sal         from students         where students_name like #{pname}        limit #{pstart},#{psize}    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值