项目需要,把以前分页模板拿出来,改改了,可支持原生的sql进行分页
MybatisPageServiceTemplate分页模板类
package com.key.win.base.page; import com.baomidou.mybatisplus.core.conditions.AbstractWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.segments.OrderBySegmentList; import com.baomidou.mybatisplus.core.mapper.baseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.key.win.base.auth.AuthenticationUtil; import com.key.win.base.mapper.KeyWinMapper; import com.key.win.base.util.SingleSoldierConstantUtils; import com.key.win.base.web.CodeEnum; import com.key.win.base.web.OrderDir; import com.key.win.base.web.PageRequest; import com.key.win.base.web.PageResult; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public abstract class MybatisPageServiceTemplate{ private static final Logger logger = LoggerFactory.getLogger(MybatisPageServiceTemplate.class); private final baseMapper baseMapper; public MybatisPageServiceTemplate(baseMapper baseMapper) { super(); this.baseMapper = baseMapper; } public PageResult
MybatisOderByVo 排序VO
package com.key.win.base.page; import com.key.win.base.web.OrderDir; import io.swagger.annotations.ApiModel; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @ApiModel("Mybatis排序VO") @Data @NoArgsConstructor @AllArgsConstructor public class MybatisOderByVo implements Serializable { private String sortName; private OrderDir sortDir; }
分页输入对象
package com.key.win.base.web; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @Builder @NoArgsConstructor @AllArgsConstructor public class PageRequestimplements Serializable { private static final long serialVersionUID = -7320421323343652634L; @ApiModelProperty("当前页") private int pageNo = 1; @ApiModelProperty("每页条数") private int pageSize = 10; @ApiModelProperty("排序字段") private String sortName = ""; @ApiModelProperty("排序方向[ ASC-升序, DESC-降序 ]") private OrderDir sortDir = OrderDir.DESC; @ApiModelProperty("对应实体对象") private T t; @ApiModelProperty("每页条数") public int getPageSize() { return (0 == pageSize) ? 10 : pageSize; } @ApiModelProperty("获取条数") public int getFirstResult() { return (pageNo - 1) * pageSize; } }
分页输出对象
package com.key.win.base.web; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @EqualsAndHashCode(callSuper = false) @NoArgsConstructor @AllArgsConstructor public class baseResult implements Serializable { @ApiModelProperty("code") protected Integer code; @ApiModelProperty("提示信息") protected String msg; }
package com.key.win.base.web; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.util.ArrayList; import java.util.List; @Data @Builder @NoArgsConstructor @AllArgsConstructor public class PageResultextends baseResult { @ApiModelProperty("当前页数") @Builder.Default private int pageNo = 1; @ApiModelProperty("每页条数") @Builder.Default private int pageSize = 10; @ApiModelProperty("总条数") @Builder.Default private long count = 0; //总页数 //private int totalPage; @ApiModelProperty("分页数据列表") private List data = new ArrayList (); @ApiModelProperty("总页数数") public long getTotalPage() { return (count + pageSize - 1) / pageSize; } }
如果要原生sql进行分页,对应的mapper需要继承KeyWinMapper
package com.key.win.base.mapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.mapper.baseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.util.ArrayList; public interface KeyWinMapperextends baseMapper { @Select({"${sql} ${ew.customSqlSegment}"}) IPage selectPageForNativeSql(IPage page, @Param("sql") String sql, @Param("ew") QueryWrapper queryWrapper); @Select({"${sql} ${ew.customSqlSegment}"}) ArrayList selectListForNativeSql(@Param("sql") String sql, @Param("ew") QueryWrapper queryWrapper); }
分页的模板的使用
使用LambdaQueryWrapper来构建查询分页
public PageResultfindMybatiesTemplateByPaged(PageRequest pageRequest) { MybatisPageServiceTemplate page = new MybatisPageServiceTemplate (this.baseMapper) { @Override protected AbstractWrapper constructWrapper(MybatiesTemplate mybatiesTemplate) { LambdaQueryWrapper lqw = new LambdaQueryWrapper (); if (mybatiesTemplate != null && StringUtils.isNotBlank(mybatiesTemplate.getName())) { lqw.like(MybatiesTemplate::getName, mybatiesTemplate.getName() == null ? "" : mybatiesTemplate.getName()); } if (mybatiesTemplate != null && StringUtils.isNotBlank(mybatiesTemplate.getCode())) { lqw.like(MybatiesTemplate::getCode, mybatiesTemplate.getCode() == null ? "" : mybatiesTemplate.getCode().toUpperCase()); } lqw.orderByDesc(MybatiesTemplate::getCreateDate); return lqw; } }; return page.doPagingQuery(pageRequest); }
使用原生sql来构建查询分页
public PageResultgetUserOrganByPaged(PageRequest pageRequest) { MybatisPageServiceTemplate query = new MybatisPageServiceTemplate (mybatiesSqlTemplateDao) { @Override protected AbstractWrapper constructWrapper(UserOrganVo userOrganVo) { QueryWrapper queryWrapper = new QueryWrapper (); if (userOrganVo != null) { if (StringUtils.isNotBlank(userOrganVo.getUserName())) { queryWrapper.eq("u.user_name", userOrganVo.getUserName()); } if (StringUtils.isNotBlank(userOrganVo.getOrganName())) { queryWrapper.eq("o.name", userOrganVo.getOrganName()); } } return queryWrapper; } @Override protected String constructNativeSql() { return "SELECT u.user_name ,o.`name` as organ_name FROM sys_user u INNER JOIN sys_user_organ uo on u.id = uo.user_id INNER JOIN sys_organ o on uo.organ_id = o.id"; } }; return query.doPagingQuery(pageRequest); }
最后源码地址:https://gitee.com/fengkuangshl_admin/single-soldier-wireless
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)