Mybatis Paging Plug-in PageHelper Query ORACLE Database Example

Keywords: Spring github MySQL xml

Like mysql, PageHelper can automatically identify and paginate by different proxy methods~

	public List<Map<String, Object>> getCarByNo(String plateno, int pagenum, int pagesize) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = null;
		
			String start =(pagenum-1)*pagesize+1+"";
			String end = pagenum*pagesize+"";
			
			list = mapper.getCarByNo(plateno,start,end);
	
		return list;
	}

 

Writing serivice


Parameter 1 passes in vehicle number, parameter 2 passes in page number, parameter 3 passes in page number.

Interpretation: String start = pagenum-1) * PageSize + 1

For example, the number of pages passed in to 1, (1-1) * 5 + 1 = 1 is the starting data for 1, and the number of pages per page is 5, so the beginning is the first line.

For example, the number of pages passed in is 5, (5-1)*5+1=21, the starting data is 5, the number of pages is 5, so the first line of the beginning is 21.

Interpretation: String end = pagenum*pagesize

For example, page number 1, the number of his closing entries is Article 5.

For example, page number 5, the number of his closing entries is Article 25.

MapperDao Writing

List<Map<String, Object>> getCarByNo(@Param("plateno")String plateno, @Param("start")String start, @Param("end")String end);

Nothing to pay too much attention to

Mapper XML Writing

select * from(
	select t.*,rownum rn from(
		select *
		  from VRESOURCE_CAR c
		 where c.PLATENO like concat(concat('%',#{plateno,jdbcType=VARCHAR}),'%')) t
		 where rownum &lt; #{end,jdbcType=INTEGER}
		)
		where rn &gt;#{start,jdbcType=INTEGER}

To understand the central idea of paging, he first checks all eligible data.

Then you go to fetch the end point or end row of this pile of data, which is then queried as a table.

Then use this table to query the number of rows from the beginning to the end, and it's as simple as that.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

A small example of using PageHelp


Following is a demonstration using spring boot. First, add dependencies
 

		<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.5</version>
		</dependency>
	</dependencies>

Controller

	@ApiOperation("ehcache Paging query")
	@GetMapping("pagefind")
	public List<Map<String, Object>> pagefind(int page,int size){
		return ehcacheservice.pagefind(page,size);
		
	}

Service

	public List<Map<String, Object>> pagefind(int page, int size) {
		
		PageHelper.startPage(page, size);//Focus on this line of code
		
		return ehcachemapper.findpage();
	}

This allows you to return to the pagination of the input parameters... But the value returned is only the data, not the total.

Here are the small steps

	public Map<String, Object> pagefind(int page, int size) {
		
		PageHelper.startPage(page, size);//Focus on this line of code, down to follow the query statement
		
		List<Map<String, Object>> findpage = ehcachemapper.findpage();
		
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(findpage);//Pass in the class provided by Pagehelper to get parameter information
		
		HashMap<String, Object> map = new HashMap<>();//Create a new map to load data
		
		map.put("list", findpage);
		
		map.put("total", pageInfo.getTotal());//Get the total number of data
		
		map.put("size", pageInfo.getPageSize());//Get Length
		
		map.put("page", pageInfo.getPageNum());//Get the current number of pages
		
		return map;
	}

That's perfect.

I test that I can query page by page without configuring beans or properties information. If there are any problems, I can consider setting configuring beans.

 

 

 

Posted by frist44 on Mon, 22 Jul 2019 03:04:34 -0700