课程中心相关接口
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -41,7 +41,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.pagehelper</groupId>
|
<groupId>com.github.pagehelper</groupId>
|
||||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||||
<version>1.3.0</version>
|
<version>2.1.0</version> <!-- 2.x 版本完全适配 Spring Boot 3+ 和 JDK 17+ -->
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.xiaoymin</groupId>
|
<groupId>com.github.xiaoymin</groupId>
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package com.bicloud.Interceptor;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
拦截器,请求打印请求状态
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class RequestLogInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(RequestLogInterceptor.class);
|
||||||
|
private static final String START_TIME = "REQ_START_TIME";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||||
|
request.setAttribute(START_TIME, System.currentTimeMillis());
|
||||||
|
|
||||||
|
log.info("[REQ] time={}, method={}, uri={}, ip={}",
|
||||||
|
LocalDateTime.now(),
|
||||||
|
request.getMethod(),
|
||||||
|
request.getRequestURI(),
|
||||||
|
request.getRemoteAddr());
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
|
||||||
|
Object handler, Exception ex) {
|
||||||
|
Long start = (Long) request.getAttribute(START_TIME);
|
||||||
|
long cost = (start == null) ? -1 : (System.currentTimeMillis() - start);
|
||||||
|
|
||||||
|
if (ex == null) {
|
||||||
|
log.info("[RES] status={}, costMs={}, uri={}",
|
||||||
|
response.getStatus(), cost, request.getRequestURI());
|
||||||
|
} else {
|
||||||
|
log.warn("[RES] status={}, costMs={}, uri={}, ex={}",
|
||||||
|
response.getStatus(), cost, request.getRequestURI(), ex.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ public class OpenApiConfig {
|
|||||||
@Bean
|
@Bean
|
||||||
public GroupedOpenApi literatureApi() {
|
public GroupedOpenApi literatureApi() {
|
||||||
return GroupedOpenApi.builder()
|
return GroupedOpenApi.builder()
|
||||||
.group("文献管理接口") // 分组名称
|
.group("bicloud接口") // 分组名称
|
||||||
.packagesToScan("com.bicloud.controller") // 匹配的接口路径
|
.packagesToScan("com.bicloud.controller") // 匹配的接口路径
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|||||||
22
src/main/java/com/bicloud/config/WebMvcConfig.java
Normal file
22
src/main/java/com/bicloud/config/WebMvcConfig.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.bicloud.config;
|
||||||
|
|
||||||
|
import com.bicloud.Interceptor.RequestLogInterceptor;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebMvcConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
private final RequestLogInterceptor requestLogInterceptor;
|
||||||
|
|
||||||
|
public WebMvcConfig(RequestLogInterceptor requestLogInterceptor) {
|
||||||
|
this.requestLogInterceptor = requestLogInterceptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
registry.addInterceptor(requestLogInterceptor)
|
||||||
|
.addPathPatterns("/**");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,15 @@
|
|||||||
package com.bicloud.controller;
|
package com.bicloud.controller;
|
||||||
|
|
||||||
|
import com.bicloud.common.result.PageResult;
|
||||||
import com.bicloud.common.result.Result;
|
import com.bicloud.common.result.Result;
|
||||||
|
import com.bicloud.pojo.dto.CoursePageQueryDto;
|
||||||
|
import com.bicloud.pojo.vo.CourseCardVo;
|
||||||
import com.bicloud.pojo.vo.CourseCatVo;
|
import com.bicloud.pojo.vo.CourseCatVo;
|
||||||
import com.bicloud.service.CourseService;
|
import com.bicloud.service.CourseService;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@CrossOrigin //跨域
|
@CrossOrigin //跨域
|
||||||
@@ -23,9 +26,17 @@ public class CourseController {
|
|||||||
|
|
||||||
// 获取课程分类列表
|
// 获取课程分类列表
|
||||||
@GetMapping("/cats")
|
@GetMapping("/cats")
|
||||||
|
@Operation(summary = "获取分类列表")
|
||||||
public Result<List<CourseCatVo>> listCourseCats() {
|
public Result<List<CourseCatVo>> listCourseCats() {
|
||||||
List<CourseCatVo> cats = courseService.listCourseCats();
|
List<CourseCatVo> cats = courseService.listCourseCats();
|
||||||
return Result.success(cats);
|
return Result.success(cats);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 课程中心分页查询
|
||||||
|
@GetMapping("/page")
|
||||||
|
@Operation(summary = "课程条件查询")
|
||||||
|
public Result<PageResult<CourseCardVo>> pageQuery(CoursePageQueryDto queryDTO) {
|
||||||
|
return Result.success(courseService.pageQuery(queryDTO));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package com.bicloud.mapper;
|
package com.bicloud.mapper;
|
||||||
|
|
||||||
|
import com.bicloud.pojo.dto.CoursePageQueryDto;
|
||||||
|
import com.bicloud.pojo.vo.CourseCardVo;
|
||||||
import com.bicloud.pojo.vo.CourseCatVo;
|
import com.bicloud.pojo.vo.CourseCatVo;
|
||||||
|
import com.github.pagehelper.Page;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -10,4 +13,7 @@ public interface CourseMapper {
|
|||||||
|
|
||||||
// 课程分类列表
|
// 课程分类列表
|
||||||
List<CourseCatVo> listCourseCats();
|
List<CourseCatVo> listCourseCats();
|
||||||
|
|
||||||
|
// 分页查询课程卡片列表
|
||||||
|
Page<CourseCardVo> pageQuery(CoursePageQueryDto queryDTO);
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/main/java/com/bicloud/pojo/dto/CoursePageQueryDto.java
Normal file
16
src/main/java/com/bicloud/pojo/dto/CoursePageQueryDto.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package com.bicloud.pojo.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 课程分页查询参数
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class CoursePageQueryDto {
|
||||||
|
private Integer page; // 页码(从1开始)
|
||||||
|
private Integer pageSize; // 每页条数
|
||||||
|
|
||||||
|
private Integer catId; // 分类ID(可空:全部)
|
||||||
|
private String keyword; // 课程名关键字(可空)
|
||||||
|
private String sort; // latest / hot(可空:默认 latest)
|
||||||
|
}
|
||||||
19
src/main/java/com/bicloud/pojo/vo/CourseCardVo.java
Normal file
19
src/main/java/com/bicloud/pojo/vo/CourseCardVo.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package com.bicloud.pojo.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 课程卡片返回对象(列表页展示用)
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class CourseCardVo {
|
||||||
|
private Integer id; // 课程ID
|
||||||
|
private String courseName; // 课程标题
|
||||||
|
private String courseIcon; // 封面路径
|
||||||
|
private Timestamp addtime; // 发布时间
|
||||||
|
private Integer hot; // 热度(先用course_collect)
|
||||||
|
private Integer catId; // 分类ID
|
||||||
|
private String catName; // 分类名(用于卡片标签)
|
||||||
|
}
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
package com.bicloud.service;
|
package com.bicloud.service;
|
||||||
|
|
||||||
|
import com.bicloud.common.result.PageResult;
|
||||||
|
import com.bicloud.pojo.dto.CoursePageQueryDto;
|
||||||
|
import com.bicloud.pojo.vo.CourseCardVo;
|
||||||
import com.bicloud.pojo.vo.CourseCatVo;
|
import com.bicloud.pojo.vo.CourseCatVo;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -8,4 +11,7 @@ public interface CourseService {
|
|||||||
|
|
||||||
// 获取课程分类列表
|
// 获取课程分类列表
|
||||||
List<CourseCatVo> listCourseCats();
|
List<CourseCatVo> listCourseCats();
|
||||||
|
|
||||||
|
// 课程中心分页查询
|
||||||
|
PageResult<CourseCardVo> pageQuery(CoursePageQueryDto queryDTO);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
package com.bicloud.service.impl;
|
package com.bicloud.service.impl;
|
||||||
|
|
||||||
|
import com.bicloud.common.result.PageResult;
|
||||||
import com.bicloud.mapper.CourseMapper;
|
import com.bicloud.mapper.CourseMapper;
|
||||||
|
import com.bicloud.pojo.dto.CoursePageQueryDto;
|
||||||
|
import com.bicloud.pojo.vo.CourseCardVo;
|
||||||
import com.bicloud.pojo.vo.CourseCatVo;
|
import com.bicloud.pojo.vo.CourseCatVo;
|
||||||
import com.bicloud.service.CourseService;
|
import com.bicloud.service.CourseService;
|
||||||
|
import com.github.pagehelper.Page;
|
||||||
|
import com.github.pagehelper.PageHelper;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -21,4 +26,20 @@ public class CourseServiceImpl implements CourseService {
|
|||||||
public List<CourseCatVo> listCourseCats() {
|
public List<CourseCatVo> listCourseCats() {
|
||||||
return courseMapper.listCourseCats();
|
return courseMapper.listCourseCats();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 课程中心分页查询
|
||||||
|
@Override
|
||||||
|
public PageResult<CourseCardVo> pageQuery(CoursePageQueryDto queryDTO) {
|
||||||
|
|
||||||
|
// 1) PageHelper 开始分页(兜底默认值,避免空指针)
|
||||||
|
int page = (queryDTO.getPage() == null || queryDTO.getPage() < 1) ? 1 : queryDTO.getPage();
|
||||||
|
int pageSize = (queryDTO.getPageSize() == null || queryDTO.getPageSize() < 1) ? 12 : queryDTO.getPageSize();
|
||||||
|
PageHelper.startPage(page, pageSize);
|
||||||
|
|
||||||
|
// 2) 执行 mapper 查询(PageHelper 自动加 limit)
|
||||||
|
Page<CourseCardVo> p = courseMapper.pageQuery(queryDTO);
|
||||||
|
|
||||||
|
// 3) 取 total + records
|
||||||
|
return new PageResult<>(p.getTotal(), p.getResult());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,4 +14,37 @@
|
|||||||
ORDER BY position ASC, id ASC
|
ORDER BY position ASC, id ASC
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<!-- 课程中心分页列表:只返回卡片展示字段(不返回播放地址) -->
|
||||||
|
<select id="pageQuery" resultType="com.bicloud.pojo.vo.CourseCardVo">
|
||||||
|
SELECT
|
||||||
|
c.id,
|
||||||
|
c.course_name AS courseName,
|
||||||
|
c.course_icon AS courseIcon,
|
||||||
|
c.addtime AS addtime,
|
||||||
|
c.course_collect AS hot,
|
||||||
|
c.course_cat AS catId,
|
||||||
|
cat.cat_name AS catName
|
||||||
|
FROM biology_courses c
|
||||||
|
LEFT JOIN biology_course_cats cat
|
||||||
|
ON cat.id = c.course_cat
|
||||||
|
WHERE c.removed = 0
|
||||||
|
<if test="catId != null">
|
||||||
|
AND c.course_cat = #{catId}
|
||||||
|
</if>
|
||||||
|
<if test="keyword != null and keyword != ''">
|
||||||
|
AND c.course_name LIKE CONCAT('%', #{keyword}, '%')
|
||||||
|
</if>
|
||||||
|
|
||||||
|
<!-- 排序白名单:latest / hot(默认 latest) -->
|
||||||
|
<choose>
|
||||||
|
<when test="sort != null and sort == 'hot'">
|
||||||
|
ORDER BY c.course_collect DESC, c.addtime DESC
|
||||||
|
</when>
|
||||||
|
<otherwise>
|
||||||
|
ORDER BY c.addtime DESC
|
||||||
|
</otherwise>
|
||||||
|
</choose>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
Reference in New Issue
Block a user