springboot整合mybatis-plus

1.依赖

复制代码
     <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.1</version>
    </dependency>
    <!-- 分页插件 -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
    </dependency>
复制代码

2.启动类添加扫描注解

@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")

@MapperScan("com.baomidou.**.mapper")

3.resources目录下 mapper 文件夹放xml文件

4.配置

复制代码
      mybatis-plus:
        mapper-locations: classpath*:mapper/*/*Mapper.xml #xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
        typeAliasesPackage: com.demo.**.model
        # 配置slq打印日志
        configuration:
          log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
        global-config:
          db-config:
            #      logic-delete-field: isDel  #全局逻辑删除字段值 3.3.0开始支持,详情看下面。
            logic-delete-value: 1 # 逻辑已删除值(默认为 1)
            logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
复制代码

5.使用

a、public interface ArticleService extends IService<Article> 

b、public class ArticleServiceImpl extends ServiceImpl<ArticleDao, Article> implements ArticleService 

c、public interface ArticleDao extends BaseMapper<Article>

6.分页

分页工具类

复制代码
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;

import java.util.ArrayList;
import java.util.List;

@Data
public class CommonPage<T> {
    private Integer page = 1;
    private Integer limit = 20;
    private Integer totalPage = 0;
    private Long total = 0L ;
    private List<T> list = new ArrayList<>();


    /**
     * 将PageHelper分页后的list转为分页信息
     */
    public static <T> CommonPage<T> restPage(List<T> list) {
        CommonPage<T> result = new CommonPage<T>();
        PageInfo<T> pageInfo = new PageInfo<T>(list);
        result.setTotalPage(pageInfo.getPages());
        result.setPage(pageInfo.getPageNum());
        result.setLimit(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 将SpringData分页后的list转为分页信息
     */
    public static <T> CommonPage<T> restPage(Page<T> pageInfo) {
        CommonPage<T> result = new CommonPage<T>();
        result.setTotalPage(pageInfo.getTotalPages());
        result.setPage(pageInfo.getNumber());
        result.setLimit(pageInfo.getSize());
        result.setTotal(pageInfo.getTotalElements());
        result.setList(pageInfo.getContent());
        return result;
    }

    /**
     * 将PageHelper分页后的 PageInfo 转为分页信息
     * @return
     */
    public static <T> CommonPage<T> restPage(PageInfo<T> pageInfo) {
        CommonPage<T> result = new CommonPage<T>();
        result.setTotalPage(pageInfo.getPages());
        result.setPage(pageInfo.getPageNum());
        result.setLimit(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 对象A复制对象B的分页信息 // 多次数据查询导致分页数据异常解决办法
     */
    public static <T> PageInfo<T> copyPageInfo(com.github.pagehelper.Page originPageInfo, List<T> list) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        BeanUtils.copyProperties(originPageInfo, pageInfo, "list");
        return pageInfo;
    }

    /**
     * 对象A复制对象B的分页信息 // 多次数据查询导致分页数据异常解决办法
     */
    public static <T> PageInfo<T> copyPageInfo(PageInfo<?> originPageInfo, List<T> list) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        pageInfo.setPages(originPageInfo.getPages());
        pageInfo.setPageNum(originPageInfo.getPageNum());
        pageInfo.setPageSize(originPageInfo.getPageSize());
        pageInfo.setTotal(originPageInfo.getTotal());
        pageInfo.setList(list);
        return pageInfo;
    }
}
复制代码

关键字查询分页方法

复制代码
/**
* 获取文章列表
* @param request 请求参数
* @param pageParamRequest 分页参数
* @return PageInfo
*/
@Override
public PageInfo<ArticleVo> getAdminList(ArticleSearchRequest request, PageParamRequest pageParamRequest) {
Page<Article> articlePage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

LambdaQueryWrapper<Article> lambdaQueryWrapper = Wrappers.lambdaQuery();
if (StringUtils.isNotBlank(request.getCid())) {
lambdaQueryWrapper.eq(Article::getCid, request.getCid());
}
if (!StringUtils.isBlank(request.getKeywords())) {
lambdaQueryWrapper.and(i -> i.or().like(Article::getTitle, request.getKeywords())
.or().like(Article::getAuthor, request.getKeywords())
.or().like(Article::getSynopsis, request.getKeywords()));
}
lambdaQueryWrapper.orderByDesc(Article::getVisit).orderByDesc(Article::getId);
List<Article> articleList = dao.selectList(lambdaQueryWrapper);

ArrayList<ArticleVo> articleVoArrayList = new ArrayList<>();
if (articleList.size() < 1) {
return CommonPage.copyPageInfo(articlePage, articleVoArrayList);
}
for (Article article : articleList) {
ArticleVo articleVo = new ArticleVo();
BeanUtils.copyProperties(article, articleVo);
if (!StrUtil.isBlank(article.getImageInput()) ) {
articleVo.setImageInput(article.getImageInput());
}
articleVoArrayList.add(articleVo);
}
return CommonPage.copyPageInfo(articlePage, articleVoArrayList);
}

/**
* 列表
* @param cid 文章分类id
* @param pageParamRequest 分页类参数
* @return PageInfo<Article>
*/
@Override
public PageInfo<ArticleResponse> getList(String cid, PageParamRequest pageParamRequest) {
Page<Article> articlePage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

LambdaQueryWrapper<Article> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(Article::getCid, cid);
lambdaQueryWrapper.eq(Article::getHide, false);
lambdaQueryWrapper.eq(Article::getStatus, false);
lambdaQueryWrapper.orderByDesc(Article::getSort).orderByDesc(Article::getVisit).orderByDesc(Article::getCreateTime);
List<Article> articleList = dao.selectList(lambdaQueryWrapper);
if (CollUtil.isEmpty(articleList)) {
return CommonPage.copyPageInfo(articlePage, CollUtil.newArrayList());
}
List<ArticleResponse> responseList = articleList.stream().map(e -> {
ArticleResponse articleResponse = new ArticleResponse();
BeanUtils.copyProperties(e, articleResponse);
return articleResponse;
}).collect(Collectors.toList());
return CommonPage.copyPageInfo(articlePage, responseList);
}
复制代码

request参数

 

复制代码
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="ArticleSearchRequest对象", description="文章管理表")
public class ArticleSearchRequest implements Serializable {

    private static final long serialVersionUID=1L;

    @ApiModelProperty(value = "分类id")
    private String cid;

    @ApiModelProperty(value = "搜索关键字")
    private String keywords;

}

@Data
public class PageParamRequest {

@ApiModelProperty(value = "页码", example= Constants.DEFAULT_PAGE + "")
private int page = Constants.DEFAULT_PAGE;

@ApiModelProperty(value = "每页数量", example = Constants.DEFAULT_LIMIT + "")
private int limit = Constants.DEFAULT_LIMIT;

}
 
复制代码

 

 

 

 

posted @   shog808  阅读(68)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示