mybatis的分页查询处理

1.StudentDao

package cn.itcast.app03;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import cn.itcast.util.MybatisUtil;

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();
		}
				
	}
	//根据id查询数据
	public Student findById(int id) throws Exception{
		SqlSession sqlSession=null;
		try{
			sqlSession=MybatisUtil.getSqlSession();
			Student student=sqlSession.selectOne(Student.class.getName()+".findById",id);
			sqlSession.commit();
			return student;
		}catch(Exception e){
			e.printStackTrace();
			//出现异常事物回滚
			sqlSession.rollback();
			throw e;
		}finally{
			MybatisUtil.closeSqlSession();
		}
				
	}
	
	//根据id查询数据
	public List<Student> findAll() throws Exception{
		SqlSession sqlSession=null;
		try{
			sqlSession=MybatisUtil.getSqlSession();
			List<Student> studentList=sqlSession.selectList(Student.class.getName()+".findAll");
			sqlSession.commit();
			return studentList;
		}catch(Exception e){
			e.printStackTrace();
			//出现异常事物回滚
			sqlSession.rollback();
			throw e;
		}finally{
			MybatisUtil.closeSqlSession();
		}
					
	}
	
	//根据id查询数据
	public void update(Student student) throws Exception{
		SqlSession sqlSession=null;
		try{
			sqlSession=MybatisUtil.getSqlSession();
			int i=sqlSession.update(Student.class.getName()+".update",student);
			sqlSession.commit();
		}catch(Exception e){
			e.printStackTrace();
			//出现异常事物回滚
			sqlSession.rollback();
			throw e;
		}finally{
			MybatisUtil.closeSqlSession();
		}
				
	}
	//根据id查询数据
	public void delete(Student student) throws Exception{
		SqlSession sqlSession=null;
		try{
			sqlSession=MybatisUtil.getSqlSession();
			sqlSession.delete(Student.class.getName()+".delete",student);
			sqlSession.commit();
		}catch(Exception e){
			e.printStackTrace();
			//出现异常事物回滚
			sqlSession.rollback();
			throw e;
		}finally{
			MybatisUtil.closeSqlSession();
		}
				
	}
	
	//分页查询(无条件查询)
	public List<Student> findAllWithFy(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()+".findAllWithFy", map);
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			MybatisUtil.closeSqlSession();
		}
	}
	//有条件的分页查询
	public List<Student> findAllByNameWithFy(String name,int start,int size) throws Exception{
		SqlSession sqlSession=null;
		try{
			sqlSession=MybatisUtil.getSqlSession();
			Map<String,Object> 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();
		System.out.println("-----第一页-----");
		List<Student> studentList1=dao.findAllByNameWithFy("方",0,3);
		for(Student student:studentList1){
			System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getSal());
		}
		
		System.out.println("-----第二页-----");
		List<Student> studentList2=dao.findAllByNameWithFy("方",3,3);
		for(Student student:studentList2){
			System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getSal());
		}
		
		System.out.println("-----第三页-----");
		List<Student> studentList3=dao.findAllByNameWithFy("方",6,3);
		for(Student student:studentList3){
			System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getSal());
		}
	}
}

2.StudentMapper.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="cn.itcast.app03.Student">

    <resultMap type="cn.itcast.app03.Student" id="studentMap">
       <id property="id" column="id"></id>
       <result property="name" column="name"/>
       <result property="sal" column="sal"/> 
    </resultMap>
    
    <insert id="add" parameterType="cn.itcast.app03.Student">
        insert into students(id,name,sal) values(#{id},#{name},#{sal});
    </insert>
    
    <select id="findAllWithFy" parameterType="map"  resultMap="studentMap">
         select id,name,sal from students limit #{pstart},#{psize};
    </select>
    
    <select id="findAllByNameWithFy" parameterType="map" resultType="cn.itcast.app03.Student">
         select id,name,sal from students where name like #{pname} limit #{pstart},#{psize};
    </select>

</mapper>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值