mybatis之分页查询

1)StudentDao.java

/**
 * 持久层*/
public class StudentDao {
    /**
     * 增加学生
     */
    public void add(Student student) throws Exception{
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try{
            sqlSession.insert("mynamespace.add",student);
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw e;
        }finally{
            sqlSession.commit();
            MyBatisUtil.closeSqlSession();
        }
    }
    /**
     * 无条件分页查询学生
     */
    public List<Student> findAllWithFy(int start,int size) throws Exception{
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try{
            Map<String,Integer> map = new LinkedHashMap<String,Integer>();
            map.put("pstart",start);
            map.put("psize",size);
            return sqlSession.selectList("mynamespace.findAllWithFy",map);
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw e;
        }finally{
            sqlSession.commit();
            MyBatisUtil.closeSqlSession();
        }
    }
    /**
     * 有条件分页查询学生
     */
    public List<Student> findAllByNameWithFy(String name,int start,int size) throws Exception{
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try{
            Map<String,Object> map = new LinkedHashMap<String,Object>();
            map.put("pname","%"+name+"%");
            map.put("pstart",start);
            map.put("psize",size);
            return sqlSession.selectList("mynamespace.findAllByNameWithFy",map);
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw e;
        }finally{
            sqlSession.commit();
            MyBatisUtil.closeSqlSession();
        }
    }
    public static void main(String[] args) throws Exception{
        StudentDao dao = new StudentDao();
        System.out.println("-----------Page1");
        for(Student s:dao.findAllByNameWithFy("哈",0,3)){
            System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());
        }
        System.out.println("-----------Page2");
        for(Student s:dao.findAllByNameWithFy("哈",3,3)){
            System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());
        }
        System.out.println("-----------Page3");
        for(Student s:dao.findAllByNameWithFy("哈",6,3)){
            System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());
        }
        System.out.println("-----------Page4");
        for(Student s:dao.findAllByNameWithFy("哈",9,3)){
            System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());
        }
    }
}

2StudentMapper.xml

<?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">

<mapper namespace="mynamespace">
    <insert id="add" parameterType="loaderman.Student">
        insert into students(id,name,sal) values(#{id},#{name},#{sal})
    </insert>
    <select id="findAllWithFy" parameterType="map" resultType="loaderman.Student">
        select id,name,sal from students limit #{pstart},#{psize}
    </select>
    <select id="findAllByNameWithFy" parameterType="map" resultType="loaderman.Student">
        select id,name,sal from students where name like #{pname} limit #{pstart},#{psize}
    </select>
</mapper>

 

/**

 *持久层

 *@authorAdminTC

 */

publicclass StudentDao {

    /**

     *增加学生

     */

    publicvoid add(Student student) throws Exception{

       SqlSession sqlSession = MyBatisUtil.getSqlSession();

       try{

           sqlSession.insert("mynamespace.add",student);

       }catch(Exception e){

           e.printStackTrace();

           sqlSession.rollback();

           throw e;

       }finally{

           sqlSession.commit();

           MyBatisUtil.closeSqlSession();

       }

    }

    /**

     *无条件分页查询学生

     */

    public List<Student> findAllWithFy(int start,int size) throws Exception{

       SqlSession sqlSession = MyBatisUtil.getSqlSession();

       try{

           Map<String,Integer> map = new LinkedHashMap<String,Integer>();

           map.put("pstart",start);

           map.put("psize",size);

           return sqlSession.selectList("mynamespace.findAllWithFy",map);

       }catch(Exception e){

           e.printStackTrace();

           sqlSession.rollback();

           throw e;

       }finally{

           sqlSession.commit();

           MyBatisUtil.closeSqlSession();

       }

    }

    /**

     *有条件分页查询学生

     */

    public List<Student> findAllByNameWithFy(String name,int start,int size) throws Exception{

       SqlSession sqlSession = MyBatisUtil.getSqlSession();

       try{

           Map<String,Object> map = new LinkedHashMap<String,Object>();

           map.put("pname","%"+name+"%");

           map.put("pstart",start);

           map.put("psize",size);

           return sqlSession.selectList("mynamespace.findAllByNameWithFy",map);

       }catch(Exception e){

           e.printStackTrace();

           sqlSession.rollback();

           throw e;

       }finally{

           sqlSession.commit();

           MyBatisUtil.closeSqlSession();

       }

    }

    publicstaticvoid main(String[] args) throws Exception{

       StudentDao dao = new StudentDao();

       System.out.println("-----------Page1");

       for(Student s:dao.findAllByNameWithFy("",0,3)){

           System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());

       }

       System.out.println("-----------Page2");

       for(Student s:dao.findAllByNameWithFy("",3,3)){

           System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());

       }

       System.out.println("-----------Page3");

       for(Student s:dao.findAllByNameWithFy("",6,3)){

           System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());

       }

       System.out.println("-----------Page4");

       for(Student s:dao.findAllByNameWithFy("",9,3)){

           System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());

       }

    }

}

转载于:https://www.cnblogs.com/loaderman/p/10064432.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值