内容管理模块项目开发
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.youkuaiyun.com/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每页大小和当前页的数据,是否为首页,是否为尾页,总页数等。