亲宝软件园·资讯

展开

Java IPage分页

杰拉德·皮克·谢 人气:0

引入依赖

   <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.2</version>
        </dependency>

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<User> getLikeUser(@RequestBody Map<String, Object> queryMap) {
        try {
            return ApiResponse.ofSuccess(userService.getLikeUser(queryMap));
        } catch (Exception e) {
            log.error("模糊查询—分页失败", e);
            return ApiResponse.ofError("模糊查询—分页失败");
        }
    }

service

 IPage<User> getLikeUser(Map<String, Object> queryMap);

impl

impl常规操作

 @Override
    public IPage<User> getLikeUser(Map<String, Object> 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<User> 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<USer> page = this.page(
                new Page<>(current, size), q);
        return page;
    }

impl自定义参数重写Sql

@Override
public IPage<TcmcAlarm> selectUSer(Map<String, Object> 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<User> page = new Page<>(current, size);
    // 自定义sql
    IPage<User> iPage = baseMapper.selectUser(page, username);
    return iPage;
}
// mapper
IPage<TcmcAlarm> selectUser(Page page, @Param("username") String username ;
// sql
<select id="selectUser" resultType="com.xxx.entity.User">
        select *
        from user
        where username LIKE CONCAT('%', #{username}, '%')
    </select>

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

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

加载全部内容

相关教程
猜你喜欢
用户评论