Java IPage分页操作 附加自定义sql

引入依赖

   
            com.baomidou
            mybatis-plus-boot-starter
            3.1.2
        

User实体类

@ApiModel(description = "用户实体")
public class User {

    /**
     * serialVersionUID
     */
    @ApiModelProperty(value = "主键")
    private Integer id;

    @ApiModelProperty(value = "用户名")
    private String username;

    @ApiModelProperty(value = "真实昵称")
    private String realname;

    @ApiModelProperty(value = "性别")
    private String sex;

    @ApiModelProperty(value = "工号")
    private String jobNum;

    @ApiModelProperty(value = "状态 0 启用 1 禁用")
    private Integer isDel;

    @ApiModelProperty(value = "部门")
    private Integer departmentId;

    private String departmentName;

    @ApiModelProperty(value = "密码")
    private String password;// 用户密码

    @ApiModelProperty(value = "手机")
    private String mobile;// 手机

    private Integer createUser;

    private Integer editUser;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date gmtCreate;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date gmtUpdate;
    }

controller

   @PostMapping("/likeUser")
    @ApiOperation("模糊查询—分页")
    public ApiResponse getLikeUser(@RequestBody Map queryMap) {
        try {
            return ApiResponse.ofSuccess(userService.getLikeUser(queryMap));
        } catch (Exception e) {
            log.error("模糊查询—分页失败", e);
            return ApiResponse.ofError("模糊查询—分页失败");
        }
    }

service

 IPage getLikeUser(Map queryMap);

impl

impl常规操作

 @Override
    public IPage getLikeUser(Map params) {
        long size = Long.parseLong(StringUtils.defaultIfBlank(String.valueOf(params.get("limit")), "10"));
        long current = Long.parseLong(StringUtils.defaultIfBlank(String.valueOf(params.get("page")), "1"));


        QueryWrapper q = new QueryWrapper<>();

        Object username= params.get("username");

        Object realname= params.get("realname");

        Object sex= params.get("sex");

        Object jobNum= params.get("jobNum");

        Object mobile= params.get("mobile");
        // 排序
        Object sort = params.get("sort");
        if (null != username && username.toString().trim().length() > 0) {
            q.lambda().like(User::getUsername, username);
        }
        if (null != realname) {
            q.lambda().like(User::getRealname, realname);
        }
        if (null != jobNum) {
            q.lambda().eq(User::getJobNum, jobNum);
        }
        if (null != startTime && null != endTime) {
            // GE 就是 GREATER THAN OR EQUAL 大于等于
            // LE 就是 LESS THAN OR EQUAL 小于等于
            q.lambda().ge(User::getCreateTime, startTime);
            q.lambda().le(User::getCreateTime, endTime);
        }
        if (sort.equals("ASC")) {
            // 升
            q.lambda().orderByAsc(User::getCreateTime);
        } else if (sort.equals("DESC")) {
            q.lambda().orderByDesc(User::getCreateTime);
        }
          IPage page = this.page(
                new Page<>(current, size), q);
        return page;
    }

impl自定义参数重写Sql

  • 思路:正常使用 Page page = new Page<>(current, size);分页插件
  • -返回 IPage iPage = baseMapper.selectUser(page,username); 传参时 传入自定义的参数和 page
  • sql 正常接入即可
@Override
public IPage selectUSer(Map params) {
    long size = Long.parseLong(StringUtils.defaultIfBlank(String.valueOf(params.get("limit")), "10"));
    long current = Long.parseLong(StringUtils.defaultIfBlank(String.valueOf(params.get("page")), "1"));
    String id= params.get("devId").toString();
    String username= params.get("username").toString();
    // 分页的使用
    Page page = new Page<>(current, size);
    // 自定义sql
    IPage iPage = baseMapper.selectUser(page, username);
    return iPage;
}
// mapper
IPage selectUser(Page page, @Param("username") String username ;
// sql

直接在impl 中写好 Sql 当作参数传入xml

// 复杂拼接  这种写法最简单,直接Joiner.on 拼接 “,” “#” “、”_" “-” 之类的
String UNION = "UNION ALL ";
 // TB_SQL sql 片段
String join = Joiner.on(UNION).join(TB_SQL);
Page page = new Page<>(current, size);
        IPage iPage = tcmcAlarmMapper.selectUser(page, join);
// mapper
IPage selectUser(Page page, @Param("data") String data);
// xml
 

到此这篇关于Java IPage分页 加自定义sql的文章就介绍到这了,更多相关Java IPage分页内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(Java IPage分页操作 附加自定义sql)