您好,欢迎访问一九零五行业门户网

SpringBoot MP简单的分页查询测试怎么实现

导入最新的mp依赖是第一步不然太低的版本什么都做不了,3,1以下的好像连分页插件都没有加进去,所以我们用最新的3.5的,保证啥都有:
<dependency> <groupid>com.baomidou</groupid> <artifactid>mybatis-plus-boot-starter</artifactid> <version>3.5.2</version> </dependency>
这里我们需要认识两个插件:mp的核心插件mybatisplusinterceptor与自动分页插件paginationinnerinterceptor。
mybatisplusinterceptor的源码(去掉中间的处理代码):
public class mybatisplusinterceptor implements interceptor { private list<innerinterceptor> interceptors = new arraylist(); public mybatisplusinterceptor() {} public object intercept(invocation invocation) throws throwable {} public object plugin(object target) {} public void addinnerinterceptor(innerinterceptor innerinterceptor) {} public list<innerinterceptor> getinterceptors() {} public void setproperties(properties properties) {} public void setinterceptors(final list<innerinterceptor> interceptors) {}}
我们可以发现它有一个私有的属性列表 list<innerinterceptor> 而这个链表中的元素类型是innerinterceptor。
innerinterceptor源码:
public interface innerinterceptor { default boolean willdoquery(executor executor, mappedstatement ms, object parameter, rowbounds rowbounds, resulthandler resulthandler, boundsql boundsql) throws sqlexception { return true; } default void beforequery(executor executor, mappedstatement ms, object parameter, rowbounds rowbounds, resulthandler resulthandler, boundsql boundsql) throws sqlexception { } default boolean willdoupdate(executor executor, mappedstatement ms, object parameter) throws sqlexception { return true; } default void beforeupdate(executor executor, mappedstatement ms, object parameter) throws sqlexception { } default void beforeprepare(statementhandler sh, connection connection, integer transactiontimeout) { } default void beforegetboundsql(statementhandler sh) { } default void setproperties(properties properties) { }}
不难发现这个接口的内容大致就是设置默认的属性,从代码的意思上就是提供默认的数据库操作执行时期前后执行的一些逻辑,谁实现它的方法会得到新的功能?
再看看paginationinnerinterceptor插件的源码:
public class paginationinnerinterceptor implements innerinterceptor { protected static final list<selectitem> count_select_item = collections.singletonlist((new selectexpressionitem((new column()).withcolumnname("count(*)"))).withalias(new alias("total"))); protected static final map<string, mappedstatement> countmscache = new concurrenthashmap(); protected final log logger = logfactory.getlog(this.getclass()); protected boolean overflow; protected long maxlimit; private dbtype dbtype; private idialect dialect; protected boolean optimizejoin = true; public paginationinnerinterceptor(dbtype dbtype) { this.dbtype = dbtype; } public paginationinnerinterceptor(idialect dialect) { this.dialect = dialect; } public boolean willdoquery(executor executor, mappedstatement ms, object parameter, rowbounds rowbounds, resulthandler resulthandler, boundsql boundsql) throws sqlexception { ipage<?> page = (ipage)parameterutils.findpage(parameter).orelse((object)null); if (page != null && page.getsize() >= 0l && page.searchcount()) { mappedstatement countms = this.buildcountmappedstatement(ms, page.countid()); boundsql countsql; if (countms != null) { countsql = countms.getboundsql(parameter); } else { countms = this.buildautocountmappedstatement(ms); string countsqlstr = this.autocountsql(page, boundsql.getsql()); mpboundsql mpboundsql = pluginutils.mpboundsql(boundsql); countsql = new boundsql(countms.getconfiguration(), countsqlstr, mpboundsql.parametermappings(), parameter); pluginutils.setadditionalparameter(countsql, mpboundsql.additionalparameters()); } cachekey cachekey = executor.createcachekey(countms, parameter, rowbounds, countsql); list<object> result = executor.query(countms, parameter, rowbounds, resulthandler, cachekey, countsql); long total = 0l; if (collectionutils.isnotempty(result)) { object o = result.get(0); if (o != null) { total = long.parselong(o.tostring()); } } page.settotal(total); return this.continuepage(page); } else { return true; } } public void beforequery(executor executor, mappedstatement ms, object parameter, rowbounds rowbounds, resulthandler resulthandler, boundsql boundsql) throws sqlexception {...........省略之后全部的内容........}
我们可以轻易地发现这个方法实现了innerinterceptor接口中的方法,所以我们需要好好审查它的源代码来理清逻辑。
我们知道了分页插件和核心插件的关系,也就是我们可以将分页插件添加入核心插件内部的插件链表中去,从而实现多功能插件的使用。
配置mp插件,并将插件交由spring管理(我们用的是springboot进行测试所以不需要使用xml文件):
import com.baomidou.mybatisplus.annotation.dbtype;import com.baomidou.mybatisplus.extension.plugins.mybatisplusinterceptor;import com.baomidou.mybatisplus.extension.plugins.inner.paginationinnerinterceptor;import org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;@configurationpublic class mpconfig { /*分页插件的配置*/ @bean public mybatisplusinterceptor mybatisplusinterceptor() { /*创建mp拦截器*/ mybatisplusinterceptor interceptor = new mybatisplusinterceptor(); /*创建分页插件*/ paginationinnerinterceptor paginterceptor = new paginationinnerinterceptor(); /*设置请求的页面大于最大页容量后的请求操作,true回调第一页,false继续翻页,默认翻页*/ paginterceptor.setoverflow(false); /*设置单页分页的条数限制*/ paginterceptor.setmaxlimit(500l); /*设置数据库类型*/ paginterceptor.setdbtype(dbtype.mysql); /*将分页拦截器添加到mp拦截器中*/ interceptor.addinnerinterceptor(paginterceptor); return interceptor; }}
配置完之后写一个mapper接口:
import com.baomidou.mybatisplus.core.mapper.basemapper;import com.hlc.mp.entity.product;import org.apache.ibatis.annotations.mapper;@mapperpublic interface productmapper extends basemapper<product> {}
为接口创建一个服务类(一定按照mp编码的风格来):
import com.baomidou.mybatisplus.core.conditions.query.querywrapper;import com.baomidou.mybatisplus.extension.plugins.pagination.page;import com.baomidou.mybatisplus.extension.service.iservice;import com.baomidou.mybatisplus.extension.service.impl.serviceimpl;import com.hlc.mp.entity.product;import com.hlc.mp.mapper.productmapper;import org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.service;import java.util.list;@service(value = "productservice")public class productserviceimpl extends serviceimpl<productmapper, product> implements iservice<product> { @autowired productmapper productmapper; /** * 根据传入的页码进行翻页 * * @param current 当前页码(已经约定每页数据量是1条) * @return 分页对象 */ public page<product> page(long current) { /*current首页位置,写1就是第一页,没有0页之说,size每页显示的数据量*/ page<product> productpage = new page<>(current, 1); /*条件查询分页*/ querywrapper<product> querywrapper = new querywrapper<>(); querywrapper.eq("status", 0); productmapper.selectpage(productpage, querywrapper); return productpage; }}
到这里我们可以看到分页的具体方法就是,先创建一个分页对象,规定页码和每一页的数据量的大小,其次确定查询操作的范围,并使用basemapper<t>给予我们的查询分页方法selectpage(e page,wapper<t> querywapper)进行查询分页的操作。
测试类:
@test public void testpage(){ ipage<product> productipage = productservice.page(2l); productipage.getrecords().foreach(system.out::println); system.out.println("当前页码"+productipage.getcurrent()); system.out.println("每页显示数量"+productipage.getsize()); system.out.println("总页数"+productipage.getpages()); system.out.println("数据总量"+productipage.gettotal()); }
运行查看分页结果:
我们可以发现都正常的按照我们传入的页码去查询对应的页数据了,因为我设置的每页只展示一条数据,所以id如果对应页码就说明分页成功。
以上就是springboot mp简单的分页查询测试怎么实现的详细内容。
其它类似信息

推荐信息