通过mybatis-plus来进行物理分页的总结

 

使用mybatis-plus的分页功能需要以下步骤

1.pom.xml添加dependency

2.增加PaginationInterceptor

3.设置sqlSessionFactory

4.自定义mapper方法

直接上代码

1.我用的是springboot结合mybatis-plus,pom.xml如下


	4.0.0
	com.springboot.test2
	springbootTest2
	war
	0.0.1-SNAPSHOT
	springbootTest2 Maven Webapp
	http://maven.apache.org

	
		1.8
	
	
		org.springframework.boot
		spring-boot-starter-parent
		1.4.1.RELEASE
		
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			junit
			junit
			test
		

		
			org.springframework.boot
			spring-boot-configuration-processor
			true
		

		
			org.springframework.boot
			spring-boot-starter-test
		
		
		
			com.alibaba
			fastjson
			1.2.68
		

		


		
			mysql
			mysql-connector-java
		

		
		
			com.baomidou
			mybatis-plus-boot-starter
			3.1.1
		

		
		
			com.alibaba
			druid
			1.0.29
		
	
	
		springbootTest2
		
			
				org.mybatis.generator
				mybatis-generator-maven-plugin
				1.3.2
				
					true
					true
				
			
		
	

2.通过@Configuration标签的方式增加PaginationInterceptor,代码如下

package com.test2.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;

@Configuration
public class MybatisPlusConfig {
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		return new PaginationInterceptor();
	}
}

3.设置sqlSessionFactory,这一步是关键,需要设置plugins属性,不然会导致分页无效 ,我通过xml的方式配置,spring-dao.xml如下




	
	
	
		
	
	
		
		
		
		
	
	
	
	
	
		
		
		
		
		
		
		
			
				
			
		
	

	
	

4.自定义mapper方法,我是通过xml的方式来写查询语句,mapper类定义一个查询方法,listPageVo,关键的地方是这个方法要传入一个Page类型的参数,这个就是分页用到的。

package com.test2.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test2.bean.PjUser;

public interface PjUserMapper extends BaseMapper{
	
	IPage listPageVo(Page page);
}

PjUserMapper.xml里的sql语句不需要写和分页有关的内容,框架自动会帮你拼接上去

  

5.看一下数据库的表pj_user

通过mybatis-plus来进行物理分页的总结_第1张图片

6.写一个测试类来调用这个方法

先新增springboot的启动类App.java

@ImportResource(locations = {"classpath:spring-dao.xml"})
@SpringBootApplication
@MapperScan("com.test2.mapper")
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

写一个测试类 SpringbooTests.java

 
package com.test2.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test2.bean.Person;
import com.test2.bean.PjUser;
import com.test2.mapper.PjUserMapper;

@RunWith(SpringRunner.class)
@SpringBootTest//(classes = App.class)
public class SpringbooTests {

	@Autowired
	private PjUserMapper pjUserMapper;

	@Test
	public void testPjUserMapper() {
		Page page = new Page<>();
		page.setCurrent(1);//设置当前页号
		page.setSize(2);//设置每页显示的数量
		IPage listPageVo = pjUserMapper.listPageVo(page);
		System.out.println(JSON.toJSONString(listPageVo));
	}

}

 

7.运行打印结果如下,mybatis-plus会自动帮你把limit拼接上去

2020-05-18 13:52:55.041 DEBUG 15916 --- [           main] c.test2.mapper.PjUserMapper.listPageVo   : ==>  Preparing: SELECT COUNT(1) FROM pj_user 
2020-05-18 13:52:55.062 DEBUG 15916 --- [           main] c.test2.mapper.PjUserMapper.listPageVo   : ==> Parameters: 
2020-05-18 13:52:55.079 DEBUG 15916 --- [           main] c.test2.mapper.PjUserMapper.listPageVo   : ==>  Preparing: SELECT * FROM pj_user LIMIT ?,? 
2020-05-18 13:52:55.081 DEBUG 15916 --- [           main] c.test2.mapper.PjUserMapper.listPageVo   : ==> Parameters: 0(Long), 2(Long)
2020-05-18 13:52:55.086 DEBUG 15916 --- [           main] c.test2.mapper.PjUserMapper.listPageVo   : <==      Total: 2
{"current":1,"pages":2,"records":[{"age":"11","id":1,"name":"testname"},{"age":"2","id":2,"name":"2"}],"searchCount":true,"size":2,"total":4}


 

你可能感兴趣的:(mybatis-plus)