mybatis(3)分页查询和条件分页查询
2017-09-25 21:23
417 查看
mybatis.xml,db.properties,MybatisUtil.java,student.java和第一篇文章一样,所建的表也是一样的
StudentDao.java
StudentDao.java
/** * 持久层 * @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<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<String, Object>(); 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<Student> studentList1 = dao.findAllByNameWithFy("哈",0,3); for(Student s : studentList1){ System.out.println(s.getId()+":"+s.getName()+":"+s.getSal()); } System.out.println("--------------------第二页"); List<Student> studentList2 = dao.findAllByNameWithFy("哈",3,3); for(Student s : studentList2){ System.out.println(s.getId()+":"+s.getName()+":"+s.getSal()); } System.out.println("--------------------第三页"); List<Student> studentList3 = dao.findAllByNameWithFy("哈",6,3); for(Student s : studentList3){ System.out.println(s.getId()+":"+s.getName()+":"+s.getSal()); } } }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.javaee.mybatis.app10.Student"> <resultMap type="cn.itcast.javaee.mybatis.app10.Student" id="studentMap"> <id property="id" column="students_id"/> <result property="name" column="students_name"/> <result property="sal" column="students_sal"/> </resultMap> <insert id="add" parameterType="cn.itcast.javaee.mybatis.app10.Student"> insert into students(students_id,students_name,students_sal) values(#{id},#{name},#{sal}); </insert> <select id="findAllWithFy" parameterType="map" resultMap="studentMap"> select students_id,students_name,students_sal from students limit #{pstart},#{psize} </select> <select id="findAllByNameWithFy" parameterType="map" resultMap="studentMap"> select students_id,students_name,students_sal from students where students_name like #{pname} limit #{pstart},#{psize} </select> </mapper>
相关文章推荐
- 资料 mybatis 一条语句 分页查询(同时获得符合条件的总记录数)
- Mybatis+Oracle实现按条件分页查询
- 解决Mybatis 分页插件pagehelper不支持逆向工程Example条件查询
- 带查询条件的Mybatis分页的实现
- Mybatis+mysql动态分页查询数据案例——条件类(HouseCondition)
- Spring+SpringMVC+Mybatis 带查询条件的Ajax+Paginator插件分页
- 基于mybatis的动态条件分页查询
- Mybatis+Oracle实现按条件分页查询
- MyBatis级联多条件分页查询
- Mybatis的条件分页查询
- mybatis分页条件查询动态sql
- MyBatis多条件分页查询,返回总记录数
- mybatis分页多条件查询指定时间段数据的sql语句
- 由淘宝,京东,凡客站点的多条件分页查询细节想到的
- 分页查询,二级条件
- Lucene代码备份之分页条件查询
- mybatis+mysql用中文做查询条件返回值无结果
- Hibernate查询,返回new对象(注意这个新定义的类要有构造函数),使用sql带条件分页查询并且把结果显示到一个对象的集里面的解决方案
- TableStore表格存储(阿里云OTS)多行数据操作查询,支持倒序,过滤条件和分页