标签搜索

目 录CONTENT

文章目录

mybatis-plus分页插件使用

陈铭
2023-04-18 / 0 评论 / 0 点赞 / 126 阅读 / 427 字 / 正在检测是否收录...

pom

        <!-- mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

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

        <!-- Pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
        </dependency>

Java实例代码

DTO

假设这里有个表的映射DTO,映射event这张表

package com.rycem.meta.entity.event;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Date;

/**
 * @ClassName Event
 * @Description 事件表DTO
 * @Author 陈铭
 * @Date 14:08 2023/4/6
 * @Version 1.0
 **/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@TableName("event")
public class Event {

    @TableId(value = "event_id", type = IdType.AUTO)
    private Integer eventId;

    @TableField("app_id")
    private Integer appId;

    @TableField("event_name")
    private String eventName;

    @TableField("alias_name")
    private String aliasName;

    @TableField("create_time")
    private Date createTime;

    @TableField("update_time")
    private Date updateTime;

}

EventMapper

package com.rycem.meta.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rycem.meta.entity.event.Event;

/**
 * @ClassName EventMapper
 * @Description 事件的Mybatis接口
 * @Author 陈铭
 * @Date 11:18 2023/3/28
 * @Version 1.0
 **/
public interface EventMapper extends BaseMapper<Event> {
}

IService

生成对应的IService

package com.rycem.meta.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.rycem.meta.entity.event.Event;

/**
 * @ClassName EventIService
 * @Description 自定义IService接口
 * @Author 陈铭
 * @Date 16:54 2023/3/30
 * @Version 1.0
 **/
public interface EventIService extends IService<Event> {
}

Service层业务实现

@Service
@Slf4j
@RequiredArgsConstructor
public class MetaMysqlServiceImpl implements MetaMysqlService {

    private final EventIService eventIService;
    
    @Override
    public ResultBody<Page<Event>> searchEventByGroupId(Integer pageSize, Integer pageNum, Integer groupId) throws MetaMysqlException {
        ...
        // 分页
        Page<Event> page = eventIService.page(new Page<>(pageNum, pageSize), Wrappers.<Event>lambdaQuery()
                .in(Event::getEventId, eventIds));
        ...
        return ResultBody.success(page, requestId, "");
    }
}
0

评论区