Springboot集成MybatisPlus以及常用语句、分页

问题:
1.当项目中使用了MybatisPlus框架的时候,service,mapper,model层该怎么写
2.如何使用mybatisplus的分页插件

一、service,mapper,model结构和规范

以实体类WmMaterial为例

  1. spring配置
mybatis-plus:
  mapper-locations: classpath*:mapper/*.xml
  # 设置别名包扫描路径,通过该属性可以给包中的类注册别名
  type-aliases-package: com.xxx.xxx.xxx.pojos
  1. WmMaterialMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.heima.model.wemedia.pojos.WmMaterial;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface WmMaterialMapper extends BaseMapper<WmMaterial> {
}
  1. WmMaterialService
import com.baomidou.mybatisplus.extension.service.IService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmMaterialDto;
import org.springframework.web.multipart.MultipartFile;

public interface WmMaterialService extends IService<WmMaterial> {
   
    ResponseResult uploadPicture(MultipartFile multipartFile);

    PageResponseResult pageList(WmMaterialDto wmMaterialDto);
}
  1. WmMaterialServiceImpl
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
}
  1. WmMaterial实体类
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.Data;

import java.io.Serializable;
import java.util.Date;

@Data
@TableName("wm_material")
public class WmMaterial implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 自媒体用户ID
     */
    @TableField("user_id")
    private Integer userId;

    /**
     * 图片地址
     */
    @TableField("url")
    private String url;

    /**
     * 素材类型
     0 图片
     1 视频
     */
    @TableField("type")
    private Short type;

    /**
     * 是否收藏
     0表示全部
     1表示收藏
     */
    @TableField("is_collection")
    private Short isCollection;

    /**
     * 创建时间
     */
    @TableField("created_time")
    private Date createdTime;

}

二、分页

1. 分页逻辑的步骤

public PageResponseResult pageList(WmMaterialDto dto){
        // 1.生成page
        IPage<WmMaterial> rawPage = new Page<>(dto.getPage(), dto.getSize());

        // 2.封装wrapper 
        Integer userId = RequestContextUtil.get("id");
        LambdaQueryWrapper<WmMaterial> wrapper = Wrappers.<WmMaterial>lambdaQuery()
                .eq(dto.getIsCollection()!=null,
                        WmMaterial::getIsCollection,
                        dto.getIsCollection())
                .eq(WmMaterial::getUserId,userId);
        // 3.分页查询
        IPage<WmMaterial> resultPage = wmMaterialMapper.selectPage(rawPage, wrapper);

        // 4.封装结果对象
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) resultPage.getTotal());
        pageResponseResult.setData(resultPage.getRecords());

        // 5.返回结果
        return pageResponseResult;
    }

如果不配置MybatisPlusInterceptor,resultPage里会有全部的records,并且total为0

2. 配置分页插件

@Bean
    public MybatisPlusInterceptor paginationInnerInterceptor(){
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDbType(DbType.MYSQL);

        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
        return mybatisPlusInterceptor;
    }

三、常用

查询

// 方式一 条件是LearningLesson必须为LearningLessonServiceImpl的T
LearningLesson lesson = lambdaQuery()
        .eq(LearningLesson::getUserId, userId)
        .eq(LearningLesson::getStatus, LessonStatus.LEARNING.getValue())
        .orderByDesc(LearningLesson::getLatestLearnTime)
        .last("limit 1")
        .one();

// 方式二
LambdaQueryWrapper<LearningLesson> wrapper = Wrappers.<LearningLesson>lambdaQuery()
        .eq(LearningLesson::getUserId, userId)
        .eq(LearningLesson::getStatus, LessonStatus.LEARNING.getValue())
        .orderByDesc(LearningLesson::getLatestLearnTime)
        .last("limit 1");

LearningLesson lesson = learningLessonMapper.selectOne(wrapper);

分页

// 方式一 条件是LearningLesson必须为LearningLessonServiceImpl的T
Page<LearningLesson> pageInfo = new Page<>(query.getPageNo(), query.getPageSize());

Page<LearningLesson> resultPage = this.lambdaQuery() //加不加this都行
        .eq(LearningLesson::getUserId, userId)
        .orderByDesc(LearningLesson::getCreateTime);
        .page(pageInfo);

//方式二
Page<LearningLesson> pageInfo = new Page<>(query.getPageNo(), query.getPageSize());

LambdaQueryWrapper<LearningLesson> wrapper= Wrappers.<LearningLesson>lambdaQuery()
        .eq(LearningLesson::getUserId, userId)
        .orderByDesc(LearningLesson::getCreateTime);

Page<LearningLesson> resultPage = learningLessonMapper.selectPage(pageInfo,wrapper);

为了用方式一,可以在service里注入别的service

public LearningLessonDTO queryLearningRecordByCourse(Long courseId){
        // 1.获取登录用户
        Long userId = UserContext.getUser();
        // 2.查询课表 注入别的Service
        LearningLesson lesson = lessonService.queryByUserAndCourseId(userId, courseId);
        // 3.查询学习记录
        // select * from xx where lesson_id = #{lessonId}
        List<LearningRecord> records = lambdaQuery()
                .eq(LearningRecord::getLessonId, lesson.getId()).list();
        // 4.封装结果
        LearningLessonDTO dto = new LearningLessonDTO();
        dto.setId(lesson.getId());
        dto.setLatestSectionId(lesson.getLatestSectionId());
        dto.setRecords(BeanUtils.copyList(records, LearningRecordDTO.class));
        return dto;
    }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值