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());
        }
    }
}

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

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="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());

}

}

}

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

原文地址:https://www.cnblogs.com/loaderman/p/10064432.html

时间: 2024-08-04 04:47:41

mybatis之分页查询的相关文章

Mybatis封装分页查询的java公用类

分页----对于数据量很大的查询中,是必不可少的.mybatis底层的分页sql语句由于需要我们自己去手动写.而实现分页显示的时候我们需要根据分页查询条件查询符合条件的总记录数和记录的详细情况.因此,若是不去实现封装一下的话,我们需要写两条SQL语句去实现它.一次用于查询记录数目.一次用于查询分页显示的详细记录.当项目中碰到很多需要分页的时候,我们便对于每一个Mapper.xml文件都需要去写两条SQL语句.极其麻烦,代码重用----必须重用.所以,一个公共方法的分页需求应运而生. 直接上分页公

Mybatis包分页查询java公共类

分页----对于数据量非常大的查询中.是不可缺少的. mybatis底层的分页sql语句因为须要我们自己去手动写.而实现分页显示的时候我们须要依据分页查询条件查询符合条件的总记录数和记录的具体情况.因此,若是不去实现封装一下的话,我们须要写两条SQL语句去实现它.一次用于查询记录数目.一次用于查询分页显示的具体记录. 当项目中碰到非常多须要分页的时候,我们便对于每个Mapper.xml文件都须要去写两条SQL语句. 极其麻烦.代码重用----必须重用.所以,一个公共方法的分页需求应运而生. 直接

spring-boot 集合mybatis 的分页查询

spring-boot 集合mybatis 的github分页查询 一.依赖包 <!-- mysql 数据库驱动. --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- spring-boot mybatis依赖: 请不要使用1.0.0版本,因为还不支持拦截器

mybatis中分页查询

1 如果在查询方法中有多个参数,可以使用map对象将所有数据都存储进去.比如分页查询,需要用到两个参数,可以将这两个参数包装到map中. 例子:分页查询 dao层方法 public List<Student> getStudentPage(int pstart, int pnumber) throws Exception{ SqlSession sqlSession = MybatisUtil.getSqlSession(); Map<String,Integer> map = n

Mybatis的分页查询

示例1:查询业务员的联系记录 1.控制器代码(RelationController.java) //分页列出联系记录 @RequestMapping(value="toPage/customerRecord") public String listRelationRecord(Map map,String beginTime,String endTime, String uname,Long curPage){ Map<String,Object> map1 = new H

springboot多数据源动态切换和自定义mybatis件分页插

1.配置多数据源 增加druid依赖 完整pom文件 数据源配置文件 route.datasource.driver-class-name= com.mysql.jdbc.Driver route.datasource.url= jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 route.datasource.username= root route.datasource.password= 1234

myBatis学习笔记(10)——使用拦截器实现分页查询

1. Page package com.sm.model; import java.util.List; public class Page<T> { public static final int DEFAULT_PAGE_SIZE = 20; protected int pageNo = 1; // 当前页, 默觉得第1页 protected int pageSize = DEFAULT_PAGE_SIZE; // 每页记录数 protected long totalRecord = -1

MyBatis系列:(4)分页查询

Emp.java package com.rk.entity; public class Emp {     private Integer id;     private String name;     private Double sal;     public Emp(){}          public Emp(Integer id, String name, Double sal) {         this.id = id;         this.name = name;

Mybatis+SpringMVC实现分页查询(附源码)

Maven+Mybatis+Spring+SpringMVC实现分页查询(附源码) 一.项目搭建 关于项目搭建,小宝鸽以前写过一篇Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建,这篇文章提供了详细的搭建过程,而且提供了源码下载,接下来的将在这个源码的基础上继续开发.所以建议各位猿友可以把猿友下载一下. 二.分页插件的介绍 博主采用的插件是PageHelper这个插件,使用起来十分方便.该插件支持以下数据库: Oracle Mysql MariaDB SQLite