controller层 前端传入的Map集合
@RequestMapping("/list")
public BaseResponse list(@RequestParam MapparamMap){
BaseResponse response=new BaseResponse(StatusCode.Success);
Map resMap= Maps.newHashMap();
try{
PageUtil page=sysPostService.queryPage(paramMap);
resMap.put("page",page);
}catch (Exception e){
response=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
}
response.setData(resMap);
return response;
}
service接口层:
public interface SysPostService extends IService {}
service实现层:
@Service
public class SysPostServiceImpl extends ServiceImpl implements SysPostService {}
@Override
public PageUtil queryPage(Map params) {
//拿到检索条件
String search=(params.get("search")==null)?"":params.get("search").toString();
//调用自封装的分页查询工具(一页多少,升序降序、当前第几页)返回成Ipage
IPagequeryPage=new QueryUtil().getQueryPage(params);
//写查询的Wrapper
QueryWrapper wrapper=new QueryWrapper()
.like(StringUtils.isNotBlank(search),"post_code",search.trim())
.or(StringUtils.isNotBlank(search))
.like(StringUtils.isNotBlank(search),"post_name",search.trim());
IPage resPage=this.page(queryPage,wrapper);
//返回结果重载了泛型
return new PageUtil(resPage);
}
mapper定义:
@Mapper
public interface SysPostDao extends BaseMapper {}
用到的包装类:
PageUtil类,用于包装分页组件,定义了返回前端的内容
package com.qty.common.utils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import lombok.ToString;
import java.io.Serializable;
import java.util.List;
@Data
@ToString
public class PageUtil implements Serializable {
private static final long serialVersionUID=1L;
//总记录数
private int totalCount;
//每页记录数
private int pageSize;
//总页数
private int totalPage;
//当前第几页
private int currPage;
//列表数据
private List> list;
public PageUtil(int totalCount, int pageSize, int totalPage, int currPage, List> list) {
this.totalCount = totalCount;
this.pageSize = pageSize;
this.totalPage = totalPage;
this.currPage = currPage;
this.list = list;
}
/**
* 分页(使用)
* @param page
*/
public PageUtil(IPage>page) {
this.list=page.getRecords();
this.totalCount= (int) page.getTotal();
this.pageSize = (int)page.getSize();
this.currPage = (int)page.getCurrent();
this.totalPage = (int)page.getPages();
}
}
QueryUtil类,用于对前端传入的参数二次封装
package com.qty.common.utils;
/**
* Created by Administrator on 2019/7/18.
*/
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import java.util.Map;
/**
* 查询统一封装的工具类
* @Author:debug (SteadyJack)
* @Date: 2019/8/1 22:23
**/
public class QueryUtil {
public IPage getQueryPage(Map params) {
//当前第几页、每页显示的条目
long curPage = 1;
long limit = 10;
if (params.get(Constant.PAGE)!=null){
curPage=Long.valueOf(params.get(Constant.PAGE).toString());
}
if (params.get(Constant.LIMIT)!=null){
limit=Long.valueOf(params.get(Constant.LIMIT).toString());
}
//分页对象
Page page = new Page<>(curPage, limit);
//前端请求的字段排序
if(params.get(Constant.ORDER)!=null && params.get(Constant.ORDER_FIELD)!=null){
//SQLFilter.sqlInject((String) params.get(Constant.ORDER_FIELD));
if(Constant.ASC.equalsIgnoreCase(params.get(Constant.ORDER).toString())) {
return page.setAsc(params.get(Constant.ORDER_FIELD).toString());
}else {
return page.setDesc(params.get(Constant.ORDER_FIELD).toString());
}
}
return page;
}
}
MybatisConfig 配置类
package com.qty.server.config;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
* @return
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
/**
* 防Sql注入
* @return
*/
@Bean
public ISqlInjector sqlInjector(){
return new LogicSqlInjector();
}
}
如果觉着我的文章有些帮助,或者还想咨询其他问题欢迎关注我的公众号,私信我,谢谢
扫我进行关注