Day-02-01

内容管理模块项目开发

Swagger的使用

1. 导入依赖
<!-- Spring Boot 集成 swagger -->
<dependency>
    <groupId>com.spring4all</groupId>
    <artifactId>swagger-spring-boot-starter</artifactId>
</dependency>
2. 配置信息
# 在application.yml中添加swagger配置信息(资料上所给是在boostrap.yml文件中)
swagger:
  title: "学成在线内容管理系统"
  description: "内容系统管理系统对课程相关信息进行管理"
  base-package: com.xuecheng.content
  enabled: true
  version: 1.0.0
3. 在启动类上添加注解
@EnableSwagger2Doc   // Swagger生成接口文档
@SpringBootApplication
public class ContentApplication {

    public static void main(String[] args) {
        SpringApplication.run(ContentApplication.class, args);
    }
}
4. 访问Swagger页面

通过 http://localhost:63040/content/swagger-ui.html 访问。

5. Swagger常见注解
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiModelProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数
6. 其他API文档生成工具

对于生成Api文档,也可以使用knife4j,注解使用和Swagger相同。使用方法:https://blog.csdn.net/qq_63665506/article/details/131588113

MybatisPlus分页插件的使用

1. 导入MybatisPlus依赖
<!-- mybatis plus的依赖 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
2. MybatisPlus配置
@Configuration
@MapperScan("com.xuecheng.content.mapper")           // 扫描指定包
// MybatisPlus配置类
public class MybatisPlusConfig {

   // 定义分页拦截器
   @Bean
   public MybatisPlusInterceptor mybatisPlusInterceptor() {
      // 1. 初始化核心插件
      MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
      // 2. 添加分页插件
      interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
      return interceptor;
   }
}
3. 分页插件的使用
/**
 * 课程信息管理接口实现类
 */
@Service
public class CourseBaseInfoServiceImpl implements CourseBaseInfoService {

    @Resource
    private CourseBaseMapper courseBaseMapper;

    /**
     * 课程查询接口
     *
     * @param pageParams          分页参数
     * @param queryCourseParamDto 查询条件
     * @return com.xuecheng.base.model.PageResult<com.xuecheng.content.model.po.CourseBase>
     */
    @Override
    public PageResult<CourseBase> queryCourseBaseList(PageParams pageParams, QueryCourseParamDto queryCourseParamDto) {

        // 构建查询条件对象
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();

        // 根据课程名称模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(queryCourseParamDto.getCourseName()), CourseBase::getName, queryCourseParamDto.getCourseName());
        // 根据课程审核状态查询
        queryWrapper.eq(StringUtils.isNotEmpty(queryCourseParamDto.getAuditStatus()), CourseBase::getAuditStatus, queryCourseParamDto.getAuditStatus());
        // 根据课程发布状态查询
        queryWrapper.eq(StringUtils.isNotEmpty(queryCourseParamDto.getPublishStatus()), CourseBase::getStatus, queryCourseParamDto.getPublishStatus());

        // 分页对象
        Page<CourseBase> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());

        // 查询数据内容获得结果
        Page<CourseBase> pageResult = courseBaseMapper.selectPage(page, queryWrapper);

        // 获取数据列表
        List<CourseBase> list = pageResult.getRecords();
        // 获取数据总数
        long total = pageResult.getTotal();
        // 构建结果集
        PageResult<CourseBase> courseBasePageResult = new PageResult<>(
                list, total, pageParams.getPageNo(), pageParams.getPageSize()
        );

        // 返回结果集
        return courseBasePageResult;
    }
}
4. 分页插件原理简述

首先分页参数放到ThreadLocal中,拦截执行的sql,根据数据库类型添加对应的分页语句重写sql,例如:(select * from table where a) 转换为 (select count(*) from table where a)和(select * from table where a limit , )

执行过程

计算出了total总条数、pageNum当前第几页、pageSize每页大小和当前页的数据,是否为首页,是否为尾页,总页数等。

相关推荐

  1. <span style='color:red;'>Day</span>-<span style='color:red;'>02</span>-<span style='color:red;'>01</span>

    Day-02-01

    2024-02-17 17:26:02      38 阅读
  2. <span style='color:red;'>Day</span>-<span style='color:red;'>01</span>-<span style='color:red;'>02</span>

    Day-01-02

    2024-02-17 17:26:02      22 阅读
  3. Day02

    2024-02-17 17:26:02       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-17 17:26:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-17 17:26:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-17 17:26:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-17 17:26:02       18 阅读

热门阅读

  1. 物理机安装kali

    2024-02-17 17:26:02       35 阅读
  2. B3668 [语言月赛202210] 应急食品

    2024-02-17 17:26:02       30 阅读
  3. re:从0开始的CSS之旅 17. 定位

    2024-02-17 17:26:02       28 阅读
  4. 病理WSI配准库valis教程

    2024-02-17 17:26:02       31 阅读
  5. ESP8266 常用AT指令

    2024-02-17 17:26:02       29 阅读
  6. 求最小生成树相关例题题解

    2024-02-17 17:26:02       27 阅读
  7. 正则表达式

    2024-02-17 17:26:02       20 阅读
  8. LinkedList数据结构链表

    2024-02-17 17:26:02       32 阅读
  9. 牛客 数星星 Stars

    2024-02-17 17:26:02       36 阅读
  10. vue实现多个下拉框联动(一)

    2024-02-17 17:26:02       27 阅读