mysql分页,oracle分页以及sqlserver分页

sql

sql分页问题

1:分页主要是在进行表格展示时候进行的

2:分页主要有三点:总条数,每页的数据,以及总页数

列举mysql.oracle,以及sqlserver语法.三种数据库的分页主要涉及方言问题.

mysql:
select * from student limit 0,10;

开发示例

使用mysql进行分页需要创建分页类,重点是编写偏移量(因为mysql第一条是0开始)

@Setter
@Getter
public class Page {
	//public static final int DEF_COUNT = 10;
	protected int count;
	protected int limit;
	protected int page;

	/**
	 * 
	 * @Description: 只要使用给page和limit赋值,那么offset自动计算出来
	 * @return
	 */
    public int getOffset() {
        return (page-1)*limit;
    }
}

mybatis中结合mysql这里使用map,将page放入map中传入xml

    public List<TbFile> fileList(TbFile tbFile, Page page, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("page",page);
        int count = fileDTOMapper.getCount(map);
        page.setCount(count);
        List<TbFile> list = fileDTOMapper.selectUserList(map);
        System.out.println(list);
        return list;
    }
    
List<TbFile> selectUserList(Map<String, Object> map);

对应的xml如下:

<--每页记录-->
<select id="selectUserList" resultMap="BaseResultMap">
		select * from tb_file as u
		where 1=1
		limit #{page.offset},#{page.limit}
select>
<--总条数-->
<select id="getCount" resultType="int">
		select count(1) from tb_file as u
		where 1=1
		limit #{page.offset},#{page.limit}
select>

oracle
 SELECT * FROM (
	SELECT rownum AS rn, t2.*
	    FROM ( SELECT * FROM student
		            WHERE 1 = 1
	            ) t2  
	WHERE rownum <= curentPage * pageSize
)
WHERE rn >= (currentPage - 1) * pageSize + 1
sqlserver
SELECT TOP pageSize *
FROM (
	SELECT ROW_NUMBER() OVER (ORDER BY sno ASC) AS rownumber, *
	FROM student
) temp_row
WHERE rownumber > (pageIndex - 1) * pageSize;

结合mybatis示例

<select id="getMatterHistoryList" parameterType="map" resultMap="ResultListMap">
     SELECT TOP ${pageSize} * FROM (
     	SELECT ROW_NUMBER () OVER (ORDER BY Sno DESC) AS RowNumber, Sname
     	from student
	) AS temp WHERE RowNumber > ${pageSize}*(${pageNo}-1)
  select>
计算页数

分页有时候会用到计算页数问题。

int pages=(counts+pageSize-1)/pageSize
// int 6 =(59+10-1)/10;

你可能感兴趣的:(java,spring,mysql,sql,数据库)