package com.example.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* MybatisPlus插件配置
*/
@Configuration
@Slf4j
public class DemoConfiguration implements WebMvcConfigurer {
/**
* MybatisPlus插件配置
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
package com.example.demo.controller;
import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.api.CommonResult;
import com.example.demo.model.dto.BdPersonDto;
import com.example.demo.model.entity.BdPerson;
import com.example.demo.service.BdPersonService;
import com.example.mybatisplus.MybatisPlusController;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
/**
*
* 前端控制器
*
*
* @author system
* @since 2024-01-16
*/
@RestController
@RequestMapping("/sys/person")
@Slf4j
public class BdPersonController extends MybatisPlusController{
@Autowired
private BdPersonService bdPersonService;
@ApiOperation(value = "查询所有人员")
@GetMapping("/getAll")
public CommonResult> page(){
IPage convert = bdPersonService.lambdaQuery().orderByDesc(BdPerson::getFnumber)
.page(this.getPage()).convert(t -> t.convert(BdPersonDto.class));
return CommonResult.success(convert);
}
}
package com.example.mybatisplus;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* mybatisplus分页参数属性配置
*/
@Data
@Component
@ConfigurationProperties(prefix = "demo.mybatis-plus.page")
public class PageProperties {
//当前页数
private String pageNum = "pageNum";
//每页分页条数
private String pageSize = "pageSize";
//是否进行count查询
private String searchCount = "searchCount";
//升序字段
private String ascs = "ascs";
//降序字段
private String descs = "descs";
//默认分页条数
private Integer defaultPageSize = 10;
//最大的分页条数
private Integer maxPageSize = 500;
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof PageProperties)) {
return false;
} else {
PageProperties other = (PageProperties)o;
if (!other.canEqual(this)) {
return false;
} else {
label95: {
Object this$pageNum = this.getPageNum();
Object other$pageNum = other.getPageNum();
if (this$pageNum == null) {
if (other$pageNum == null) {
break label95;
}
} else if (this$pageNum.equals(other$pageNum)) {
break label95;
}
return false;
}
Object this$pageSize = this.getPageSize();
Object other$pageSize = other.getPageSize();
if (this$pageSize == null) {
if (other$pageSize != null) {
return false;
}
} else if (!this$pageSize.equals(other$pageSize)) {
return false;
}
Object this$searchCount = this.getSearchCount();
Object other$searchCount = other.getSearchCount();
if (this$searchCount == null) {
if (other$searchCount != null) {
return false;
}
} else if (!this$searchCount.equals(other$searchCount)) {
return false;
}
label74: {
Object this$ascs = this.getAscs();
Object other$ascs = other.getAscs();
if (this$ascs == null) {
if (other$ascs == null) {
break label74;
}
} else if (this$ascs.equals(other$ascs)) {
break label74;
}
return false;
}
label67: {
Object this$descs = this.getDescs();
Object other$descs = other.getDescs();
if (this$descs == null) {
if (other$descs == null) {
break label67;
}
} else if (this$descs.equals(other$descs)) {
break label67;
}
return false;
}
Object this$defaultPageSize = this.getDefaultPageSize();
Object other$defaultPageSize = other.getDefaultPageSize();
if (this$defaultPageSize == null) {
if (other$defaultPageSize != null) {
return false;
}
} else if (!this$defaultPageSize.equals(other$defaultPageSize)) {
return false;
}
Object this$maxPageSize = this.getMaxPageSize();
Object other$maxPageSize = other.getMaxPageSize();
if (this$maxPageSize == null) {
if (other$maxPageSize != null) {
return false;
}
} else if (!this$maxPageSize.equals(other$maxPageSize)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof PageProperties;
}
public int hashCode() {
int result = 1;
Object $pageNum = this.getPageNum();
result = result * 59 + ($pageNum == null ? 43 : $pageNum.hashCode());
Object $pageSize = this.getPageSize();
result = result * 59 + ($pageSize == null ? 43 : $pageSize.hashCode());
Object $searchCount = this.getSearchCount();
result = result * 59 + ($searchCount == null ? 43 : $searchCount.hashCode());
Object $ascs = this.getAscs();
result = result * 59 + ($ascs == null ? 43 : $ascs.hashCode());
Object $descs = this.getDescs();
result = result * 59 + ($descs == null ? 43 : $descs.hashCode());
Object $defaultPageSize = this.getDefaultPageSize();
result = result * 59 + ($defaultPageSize == null ? 43 : $defaultPageSize.hashCode());
Object $maxPageSize = this.getMaxPageSize();
result = result * 59 + ($maxPageSize == null ? 43 : $maxPageSize.hashCode());
return result;
}
public String toString() {
return "PageProperties(pageNum=" + this.getPageNum() + ", pageSize=" + this.getPageSize() + ", searchCount=" + this.getSearchCount() + ", ascs=" + this.getAscs() + ", descs=" + this.getDescs() + ", defaultPageSize=" + this.getDefaultPageSize() + ", maxPageSize=" + this.getMaxPageSize() + ")";
}
}
package com.example.mybatisplus;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Splitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
/**
* MybatisPlus的Controller父类
* @param
*/
@Component
public class MybatisPlusController {
@Autowired
protected HttpServletRequest request;
@Autowired
protected PageProperties pageConfig;
public Page getPage() {
return this.getPage(false);
}
public Page getPage(boolean openSort) {
//第几页
Integer pageNum = castToInt(request.getParameter(pageConfig.getPageNum()), 1);
Integer pageSize = castToInt(request.getParameter(pageConfig.getPageSize()), pageConfig.getDefaultPageSize());
Boolean searchCount = castToBoolean(request.getParameter(pageConfig.getSearchCount()), true);
//如果分页条数超过了最大限制则直接返回最大限制
pageSize = pageSize > this.pageConfig.getMaxPageSize() ? this.pageConfig.getMaxPageSize() : pageSize;
//是否进行 count 查询
Page page = new Page((long)pageNum, (long)pageSize, searchCount);
if (openSort) {
page.setOrders(this.getOrderList());
}
return page;
}
/**
* 判断是否为空,为空则返回默认值
* @param value
* @param defaults
* @return
*/
public static Boolean castToBoolean(Object value, Boolean defaults) {
Boolean castToBoolean = TypeUtils.castToBoolean(value);
return Objects.isNull(castToBoolean) ? defaults : castToBoolean;
}
/**
* 判断是否为空,为空则返回默认值第一页
* @param value
* @param defaults
* @return
*/
public static Integer castToInt(Object value, Integer defaults) {
Integer castToInt = TypeUtils.castToInt(value);
return Objects.isNull(castToInt) ? defaults : castToInt;
}
private List getOrderList() {
List orderItemList = new ArrayList();
String ascsStr = this.request.getParameter(this.pageConfig.getAscs());
if (StrUtil.isNotBlank(ascsStr)) {
List ascs = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(ascsStr);
Iterator var4 = ascs.iterator();
while(var4.hasNext()) {
String name = (String)var4.next();
orderItemList.add(OrderItem.asc(this.camelToUnderline(name)));
}
}
String descsStr = this.request.getParameter(this.pageConfig.getDescs());
if (StrUtil.isNotBlank(descsStr)) {
List descs = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(descsStr);
Iterator var9 = descs.iterator();
while(var9.hasNext()) {
String name = (String)var9.next();
orderItemList.add(OrderItem.desc(this.camelToUnderline(name)));
}
}
return orderItemList;
}
/**
* 参数转为骆驼下划线
* @param param
* @return
*/
private String camelToUnderline(String param) {
if (StrUtil.isBlank(param)) {
return param;
} else {
int len = param.length();
StringBuilder sb = new StringBuilder(len);
for(int i = 0; i < len; ++i) {
char c = param.charAt(i);
if (Character.isUpperCase(c)) {
sb.append("_");
sb.append(Character.toLowerCase(c));
} else {
sb.append(c);
}
}
return sb.toString();
}
}
}
com.google.guava
guava
30.1.1-jre
demo.mybatis-plus.page.page-num=1
demo.mybatis-plus.page.page-size=20
demo.mybatis-plus.page.search-count=false
demo.mybatis-plus.page.default-pageSize=30
demo.mybatis-plus.page.max-page-size=500