From 795e3e5461e9a9dfa69b6e5c9c8e19e71d97dc04 Mon Sep 17 00:00:00 2001 From: whb <17803890193@163.com> Date: Thu, 10 Aug 2023 21:56:37 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E7=A8=8B=E4=BF=AE=E6=94=B9=E7=AC=AC?= =?UTF-8?q?=E4=BA=8C=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 13 +- .../application/mapper/DirectoryMapper.java | 1 + .../course/config/MybatisPlusConfig.java | 19 --- .../course/controller/CourseController.java | 45 ++++--- .../zhiyun03/course/mapper/CourseMapper.java | 6 + .../course/service/CourseService.java | 14 +- .../service/impl/CourseServiceImpl.java | 117 ++++++++++------ .../zhiyun/zhiyun03/course/vo/CourseVo.java | 9 +- .../com/zhiyun/zhiyun03/course/vo/PageVO.java | 3 + .../utils/config/GlobalCrossConfig.java | 33 +++++ src/main/resources/application.yml | 12 +- src/main/resources/mapper/CourseMapper.xml | 51 +++++++ src/main/resources/mapper/DirectoryMapper.xml | 3 + .../templates/layui-v2.6.8/courseList.html | 127 ++++++++++++++++-- .../templates/layui-v2.6.8/index.html | 2 +- 15 files changed, 352 insertions(+), 103 deletions(-) delete mode 100644 src/main/java/com/zhiyun/zhiyun03/course/config/MybatisPlusConfig.java create mode 100644 src/main/java/com/zhiyun/zhiyun03/utils/config/GlobalCrossConfig.java create mode 100644 src/main/resources/mapper/CourseMapper.xml diff --git a/pom.xml b/pom.xml index c470267..b2ced62 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,8 @@ test + + com.alibaba druid-spring-boot-starter @@ -56,13 +58,18 @@ 5.0.7 + + + + + + com.github.pagehelper - pagehelper - 5.3.2 + pagehelper-spring-boot-starter + 1.4.6 - com.github.xiaoymin knife4j-spring-boot-starter diff --git a/src/main/java/com/zhiyun/zhiyun03/application/mapper/DirectoryMapper.java b/src/main/java/com/zhiyun/zhiyun03/application/mapper/DirectoryMapper.java index 2de95b0..3dc326d 100644 --- a/src/main/java/com/zhiyun/zhiyun03/application/mapper/DirectoryMapper.java +++ b/src/main/java/com/zhiyun/zhiyun03/application/mapper/DirectoryMapper.java @@ -6,4 +6,5 @@ import org.apache.ibatis.annotations.Mapper; @Mapper public interface DirectoryMapper extends BaseMapper { + Integer selectByName(String dirName); } diff --git a/src/main/java/com/zhiyun/zhiyun03/course/config/MybatisPlusConfig.java b/src/main/java/com/zhiyun/zhiyun03/course/config/MybatisPlusConfig.java deleted file mode 100644 index e5e1263..0000000 --- a/src/main/java/com/zhiyun/zhiyun03/course/config/MybatisPlusConfig.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.zhiyun.zhiyun03.course.config; - -import com.baomidou.mybatisplus.annotation.DbType; -import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; -import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class MybatisPlusConfig { - // 从MyBatis-Plus 3.4.0开始,不再使用旧版本的PaginationInterceptor ,而是使用MybatisPlusInterceptor - @Bean - public MybatisPlusInterceptor mybatisPlusInterceptor() { - MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); - // 向MyBatis-Plus的过滤器链中添加分页拦截器,需要设置数据库类型(主要用于分页方言) - interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); - return interceptor; - } -} diff --git a/src/main/java/com/zhiyun/zhiyun03/course/controller/CourseController.java b/src/main/java/com/zhiyun/zhiyun03/course/controller/CourseController.java index e52020f..040e035 100644 --- a/src/main/java/com/zhiyun/zhiyun03/course/controller/CourseController.java +++ b/src/main/java/com/zhiyun/zhiyun03/course/controller/CourseController.java @@ -1,6 +1,7 @@ package com.zhiyun.zhiyun03.course.controller; +import com.zhiyun.zhiyun03.application.entity.Directory; import com.zhiyun.zhiyun03.course.entity.Course; import com.zhiyun.zhiyun03.course.service.CourseService; @@ -11,6 +12,7 @@ import com.zhiyun.zhiyun03.utils.common.JsonResult; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; +import io.swagger.models.auth.In; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -26,41 +28,48 @@ public class CourseController { private CourseService courseService; + @ApiOperation("查询课程") - @GetMapping("/selectAll") - public JsonResult queryCourse(){ + @GetMapping("/queryCourse") + public JsonResult> queryCourse(@ApiParam @RequestParam(value = "page",required = false,defaultValue = "1")Integer page, + @ApiParam @RequestParam(value = "limit",required = false,defaultValue = "1")Integer limit ){ - List lists = courseService.queryCourse(); + PageVO lists = courseService.queryCourse(page,limit); return JsonResult.success(lists); } + @ApiOperation("根据ID查询课程") + @GetMapping("/queryCourseById") + public JsonResult queryCourseById(Integer id){ + + CourseVo courseVo =courseService.queryCourseById(id); + + return JsonResult.success(courseVo); + } + + + @ApiOperation("课程添加") - @PostMapping("/add") - public JsonResult addCourse(Course course){ - courseService.addCourse(course); + @PostMapping("/addCourse") + public JsonResult addCourse(CourseVo courseVo){ + int count = courseService.addCourse(courseVo); + return JsonResult.success(); } -// -//// @ApiOperation("查询目录") -//// @PostMapping("/select") -//// public JsonResult selectDir(Directory directory){ -//// courseService.se(course); -//// return JsonResult.ok(); -//// } -// + @ApiOperation("删除课程") - @DeleteMapping("delById") + @DeleteMapping("delCourseById") public JsonResult delCourseById(Integer id){ courseService.delCourseById(id); return JsonResult.success(); } @ApiOperation("更新课程") - @PostMapping("update") - public JsonResult updateById(Course course){ - courseService.update(course); + @PostMapping("updateById") + public JsonResult updateById(CourseVo courseVo){ + int count = courseService.updateById(courseVo); return JsonResult.success(); } diff --git a/src/main/java/com/zhiyun/zhiyun03/course/mapper/CourseMapper.java b/src/main/java/com/zhiyun/zhiyun03/course/mapper/CourseMapper.java index 2548fa2..03358bc 100644 --- a/src/main/java/com/zhiyun/zhiyun03/course/mapper/CourseMapper.java +++ b/src/main/java/com/zhiyun/zhiyun03/course/mapper/CourseMapper.java @@ -2,6 +2,8 @@ package com.zhiyun.zhiyun03.course.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.zhiyun.zhiyun03.course.entity.Course; +import com.zhiyun.zhiyun03.course.vo.CourseVo; +import com.zhiyun.zhiyun03.course.vo.PageVO; import org.apache.ibatis.annotations.Mapper; import java.util.List; @@ -10,5 +12,9 @@ import java.util.List; public interface CourseMapper extends BaseMapper { + List queryCourse(); + CourseVo queryCourseById(Integer id); + + int updateCourseById(Course course); } diff --git a/src/main/java/com/zhiyun/zhiyun03/course/service/CourseService.java b/src/main/java/com/zhiyun/zhiyun03/course/service/CourseService.java index ef858c1..bf40208 100644 --- a/src/main/java/com/zhiyun/zhiyun03/course/service/CourseService.java +++ b/src/main/java/com/zhiyun/zhiyun03/course/service/CourseService.java @@ -16,12 +16,18 @@ public interface CourseService extends IService { // // void updateById(Course course); + /* + * 分页查询课程 + * */ + PageVO queryCourse(Integer page, Integer limit ); - List queryCourse(); - - void addCourse(Course course); + int addCourse(CourseVo courseVo); void delCourseById(Integer id); - void update(Course course); + int updateById(CourseVo courseVo); + /* + * 根据ID查询课程 + * */ + CourseVo queryCourseById(Integer id); } diff --git a/src/main/java/com/zhiyun/zhiyun03/course/service/impl/CourseServiceImpl.java b/src/main/java/com/zhiyun/zhiyun03/course/service/impl/CourseServiceImpl.java index 77a1885..d78e89f 100644 --- a/src/main/java/com/zhiyun/zhiyun03/course/service/impl/CourseServiceImpl.java +++ b/src/main/java/com/zhiyun/zhiyun03/course/service/impl/CourseServiceImpl.java @@ -1,15 +1,17 @@ package com.zhiyun.zhiyun03.course.service.impl; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.zhiyun.zhiyun03.application.entity.Directory; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; import com.zhiyun.zhiyun03.application.mapper.DirectoryMapper; -import com.zhiyun.zhiyun03.utils.convert.ConvertUtil; +import com.zhiyun.zhiyun03.course.vo.PageVO; import com.zhiyun.zhiyun03.course.entity.Course; import com.zhiyun.zhiyun03.course.service.CourseService; import com.zhiyun.zhiyun03.course.vo.CourseVo; import com.zhiyun.zhiyun03.course.mapper.CourseMapper; +import com.zhiyun.zhiyun03.utils.exception.ServiceException; +import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -24,33 +26,29 @@ public class CourseServiceImpl extends ServiceImpl implemen @Resource private DirectoryMapper directoryMapper; - - /** - * 应用中心查询 + * 课程中心查询 */ @Override - public List queryCourse() { - - QueryWrapper qwa=new QueryWrapper<>(); - List courses = courseMapper.selectList(qwa); - QueryWrapper qwd=new QueryWrapper<>(); - List directories = directoryMapper.selectList(qwd); - ConvertUtil convertUtil=new ConvertUtil(); - List applicationVos = convertUtil.entityToVoList(courses, CourseVo.class); - - for (int i = 0; i < courses.size(); i++) { - for (int j = 0; j queryCourse(Integer page, Integer limit) { + //开启分页 + PageHelper.startPage(page,limit); + //查询所有课程 + List courseVoList = courseMapper.queryCourse(); + if (courseVoList.isEmpty()) + { + throw new ServiceException("400","课程数据为空"); + } + PageInfo pageInfo = new PageInfo(courseVoList); + PageVO courseVoPageInfo = new PageVO<>(); + //当前页 + courseVoPageInfo.setCurrentPage(pageInfo.getPageNum()); + //总条数 + courseVoPageInfo.setTotal(pageInfo.getTotal()); + //分页数据 + courseVoPageInfo.setList(pageInfo.getList()); + + return courseVoPageInfo; } @@ -59,12 +57,29 @@ public class CourseServiceImpl extends ServiceImpl implemen * 添加课程 */ @Override - public void addCourse(Course course) { + public int addCourse(CourseVo courseVo) { + + if (courseVo == null) + { + throw new ServiceException("400","课程添加失败"); + } + + Course course = new Course(); + + BeanUtils.copyProperties(courseVo,course); + + String dirName = courseVo.getDirName(); - courseMapper.insert(course); -// UpdateWrapper updateWrapper = new UpdateWrapper<>(); -// updateWrapper.set("id",course.getDirId()); -// directoryMapper.update(null,updateWrapper); + Integer id = directoryMapper.selectByName(dirName); + course.setDirId(id); + + int count = courseMapper.insert(course); + System.out.println("count====="+count); + if (count<0) + { + throw new ServiceException("400","课程添加失败"); + } + return count; } @@ -81,16 +96,34 @@ public class CourseServiceImpl extends ServiceImpl implemen * 课程更新 */ @Override - public void update(Course course) { - //变更数据进行更新 - UpdateWrapper updateWrapper = new UpdateWrapper<>(); - updateWrapper.set("course_name",course.getCourseName()); - updateWrapper.set("course_brief",course.getCourseBrief()); - updateWrapper.set("course_url",course.getCourseUrl()); - updateWrapper.set("dir_id",course.getDirId()); - updateWrapper.set("course_icon",course.getCourseIcon()); - updateWrapper.eq("id",course.getId()); - courseMapper.update(null,updateWrapper); + public int updateById(CourseVo courseVo) { + Course course = new Course(); + BeanUtils.copyProperties(courseVo,course); + String dirName = courseVo.getDirName(); + Integer id = directoryMapper.selectByName(dirName); + course.setDirId(id); + + int count = courseMapper.updateCourseById(course); + if (count<0) + { + throw new ServiceException("400","课程更新失败"); + } + return count; + + } + + /** + * 根据ID查询课程 + */ + @Override + public CourseVo queryCourseById(Integer id) { + //根据id查找数据 + CourseVo courseVo = courseMapper.queryCourseById(id); + if (courseVo==null) + { + throw new ServiceException("401","课程不存在"); + } + return courseVo; } diff --git a/src/main/java/com/zhiyun/zhiyun03/course/vo/CourseVo.java b/src/main/java/com/zhiyun/zhiyun03/course/vo/CourseVo.java index 804ee88..6ab9a15 100644 --- a/src/main/java/com/zhiyun/zhiyun03/course/vo/CourseVo.java +++ b/src/main/java/com/zhiyun/zhiyun03/course/vo/CourseVo.java @@ -9,13 +9,14 @@ public class CourseVo { @TableId private int id; + //课程名 private String courseName; - + //课程简介 private String courseBrief; - + //课程链接 private String courseUrl; - + //归属目录 private String dirName; - + //课程图标 private String courseIcon; } diff --git a/src/main/java/com/zhiyun/zhiyun03/course/vo/PageVO.java b/src/main/java/com/zhiyun/zhiyun03/course/vo/PageVO.java index f37cbee..ae0ddb7 100644 --- a/src/main/java/com/zhiyun/zhiyun03/course/vo/PageVO.java +++ b/src/main/java/com/zhiyun/zhiyun03/course/vo/PageVO.java @@ -7,7 +7,10 @@ import java.util.List; @Data public class PageVO { + //当前页 private Integer currentPage; + //总条数 private Long total; + //数据 private List list; } diff --git a/src/main/java/com/zhiyun/zhiyun03/utils/config/GlobalCrossConfig.java b/src/main/java/com/zhiyun/zhiyun03/utils/config/GlobalCrossConfig.java new file mode 100644 index 0000000..f204a79 --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/utils/config/GlobalCrossConfig.java @@ -0,0 +1,33 @@ +package com.zhiyun.zhiyun03.utils.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; +import org.springframework.web.filter.CorsFilter; + +@Configuration +public class GlobalCrossConfig { + @Bean + public CorsFilter corsFilter() { + //1. 添加 CORS配置信息 + CorsConfiguration config = new CorsConfiguration(); + //放行哪些原始域,*表示所有 + // config.addAllowedOrigin("*"); + // 注意:springboot2.4以上使用该方式 + config.addAllowedOriginPattern("*"); + //是否发送 Cookie + config.setAllowCredentials(true); + //放行哪些请求方式 + config.addAllowedMethod("*"); + //放行哪些原始请求头部信息 + config.addAllowedHeader("*"); + //暴露哪些头部信息 + config.addExposedHeader("*"); + //2. 添加映射路径 + UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource(); + corsConfigurationSource.registerCorsConfiguration("/**", config); + //3. 返回新的CorsFilter + return new CorsFilter(corsConfigurationSource); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 5667f33..2a02a29 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -16,7 +16,7 @@ spring: druid: url: jdbc:mysql://localhost:3306/zhiyun?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai username: root - password: 123456 + password: root @@ -25,5 +25,15 @@ mybatis-plus: configuration: # 控制台打印日志 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl + + + +pagehelper: + helper-dialect: mysql + reasonable: true + support-methods-arguments: true + + + swagger: enabled: true diff --git a/src/main/resources/mapper/CourseMapper.xml b/src/main/resources/mapper/CourseMapper.xml new file mode 100644 index 0000000..f2909f8 --- /dev/null +++ b/src/main/resources/mapper/CourseMapper.xml @@ -0,0 +1,51 @@ + + + + + + + update course + + + course_name=#{courseName}, + + + course_url=#{courseUrl}, + + + course_brief=#{courseBrief}, + + + dir_id=#{dirId}, + + + course_icon=#{courseIcon} + + + + + id = #{id} + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/mapper/DirectoryMapper.xml b/src/main/resources/mapper/DirectoryMapper.xml index 8055e7d..d02639e 100644 --- a/src/main/resources/mapper/DirectoryMapper.xml +++ b/src/main/resources/mapper/DirectoryMapper.xml @@ -2,4 +2,7 @@ + \ No newline at end of file diff --git a/src/main/resources/templates/layui-v2.6.8/courseList.html b/src/main/resources/templates/layui-v2.6.8/courseList.html index da6688c..a3b4daf 100644 --- a/src/main/resources/templates/layui-v2.6.8/courseList.html +++ b/src/main/resources/templates/layui-v2.6.8/courseList.html @@ -25,38 +25,100 @@ 删除 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + diff --git a/src/main/resources/templates/layui-v2.6.8/index.html b/src/main/resources/templates/layui-v2.6.8/index.html index 5128b87..fc35088 100644 --- a/src/main/resources/templates/layui-v2.6.8/index.html +++ b/src/main/resources/templates/layui-v2.6.8/index.html @@ -54,7 +54,7 @@