Mybatis pagination plug PageHelper ORACLE database query example

In short, the configuration can be manually, may not be manually configured, PageHelper automatically distinguish the type of database (the range supported)

Mybatis after the first count and select, that is count the data will continue to select, this avoids the invalid queries, this is easy to pit Sometimes just look at the log.

 

 And the data source is the same mysql, PageHelper can be detected automatically and to paging through various proxy method -

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

 

The written serivice 


A parameter passed in the vehicle number, parameters passed in two pages, the number of parameters passed three per page

Explain: String start = (pagenum-1) * pagesize + 1

E.g. incoming pages 1, (1-1) * 5 + 1 = 1 for the initial data is 1, the number of page 5, the beginning of the first row is started

E.g. incoming pages 5, (5-1) * 5 + 1 = 21 as the initial data is 5, the number of page 5, the first row is 21

Explained: String end = pagenum * pagesize

Such as the pages pass 1, the number of his end is Article 5

Such as the pages pass 5, the number of his article is the first end 25

The written MapperDao

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

Nothing too much attention to the

The written MapperXML

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 ​​this main page, first of all he is to go check all eligible data

Then go get the end point of this pile of data or the end of the line, then these data as a table to query

Go to the table with the number of queries the start line to the end line, as simple as it is completed

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

The use of small sample PageHelp


Use the following as springboot presentation, first add-dependent
 

		<!-- 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分页查询")
	@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);//重点就这一行代码
		
		return ehcachemapper.findpage();
	}

This allows the tab to return the input parameters. . . . But the return value data is not only the total number.

Here is a small Adv

	public Map<String, Object> pagefind(int page, int size) {
		
		PageHelper.startPage(page, size);//重点就这一行代码,下行紧跟查询语句
		
		List<Map<String, Object>> findpage = ehcachemapper.findpage();
		
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(findpage);//传入Pagehelper提供的类获取参数信息
		
		HashMap<String, Object> map = new HashMap<>();//新建一个map装填数据
		
		map.put("list", findpage);
		
		map.put("total", pageInfo.getTotal());//获取数据总数
		
		map.put("size", pageInfo.getPageSize());//获取长度
		
		map.put("page", pageInfo.getPageNum());//获取当前页数
		
		return map;
	}

This perfect friends

I do not test or configure the Bean properties information can be normal pagination queries, if any problem occurs, you can consider the bean configuration settings

 

 

 

Published 61 original articles · won praise 54 · views 60000 +

Guess you like

Origin blog.csdn.net/bibiboyx/article/details/96877356