diff --git a/pom.xml b/pom.xml index adbb0e8..c470267 100644 --- a/pom.xml +++ b/pom.xml @@ -56,6 +56,19 @@ 5.0.7 + + com.github.pagehelper + pagehelper + 5.3.2 + + + + + com.github.xiaoymin + knife4j-spring-boot-starter + 3.0.3 + + diff --git a/src/main/java/com/zhiyun/zhiyun03/Zhiyun03Application.java b/src/main/java/com/zhiyun/zhiyun03/Zhiyun03Application.java index 8f91bf9..aa812b3 100644 --- a/src/main/java/com/zhiyun/zhiyun03/Zhiyun03Application.java +++ b/src/main/java/com/zhiyun/zhiyun03/Zhiyun03Application.java @@ -1,9 +1,12 @@ package com.zhiyun.zhiyun03; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; @SpringBootApplication + public class Zhiyun03Application { public static void main(String[] args) { diff --git a/src/main/java/com/zhiyun/zhiyun03/application/mapper/ApplicationMapper.java b/src/main/java/com/zhiyun/zhiyun03/application/mapper/ApplicationMapper.java index 3408c5c..5c5aab4 100644 --- a/src/main/java/com/zhiyun/zhiyun03/application/mapper/ApplicationMapper.java +++ b/src/main/java/com/zhiyun/zhiyun03/application/mapper/ApplicationMapper.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.zhiyun.zhiyun03.application.entity.Application; import org.apache.ibatis.annotations.Mapper; + @Mapper public interface ApplicationMapper extends BaseMapper { } diff --git a/src/main/java/com/zhiyun/zhiyun03/course/config/MyWebMvcConfig.java b/src/main/java/com/zhiyun/zhiyun03/course/config/MyWebMvcConfig.java new file mode 100644 index 0000000..ebf4cd7 --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/course/config/MyWebMvcConfig.java @@ -0,0 +1,25 @@ +package com.zhiyun.zhiyun03.course.config; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; + +@Configuration +public class MyWebMvcConfig extends WebMvcConfigurationSupport { + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/**").addResourceLocations( + "classpath:/static/"); + + // swagger静态资源相关 + registry.addResourceHandler("swagger-ui.html", "doc.html").addResourceLocations( + "classpath:/META-INF/resources/"); + registry.addResourceHandler("/webjars/**").addResourceLocations( + "classpath:/META-INF/resources/webjars/"); + // 针对swagger自带的静态资源 + registry.addResourceHandler("/swagger-ui/**").addResourceLocations( + "classpath:/META-INF/resources/webjars/springfox-swagger-ui/"); + super.addResourceHandlers(registry); + } + +} diff --git a/src/main/java/com/zhiyun/zhiyun03/course/config/MybatisPlusConfig.java b/src/main/java/com/zhiyun/zhiyun03/course/config/MybatisPlusConfig.java new file mode 100644 index 0000000..e5e1263 --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/course/config/MybatisPlusConfig.java @@ -0,0 +1,19 @@ +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/config/SwaggerConfig.java b/src/main/java/com/zhiyun/zhiyun03/course/config/SwaggerConfig.java new file mode 100644 index 0000000..b13017a --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/course/config/SwaggerConfig.java @@ -0,0 +1,112 @@ +package com.zhiyun.zhiyun03.course.config; + +import io.swagger.annotations.ApiOperation; +import io.swagger.models.auth.In; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.oas.annotations.EnableOpenApi; +import springfox.documentation.service.*; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spi.service.contexts.SecurityContext; +import springfox.documentation.spring.web.plugins.Docket; + +import java.util.ArrayList; +import java.util.List; + +/** + * Swagger3的接口配置 + *

+ * http://localhost:8080/swagger-ui/index.html#/ + */ +@Configuration +@EnableOpenApi // swagger3使用的注解 +public class SwaggerConfig { + + /** + * 是否开启swagger + */ + // 注入常量值 + @Value("${swagger.enabled}") + private boolean enabled; + + /** + * 创建API + */ + @Bean + public Docket createRestApi() { + return new Docket(DocumentationType.OAS_30) + // 是否启用Swagger + .enable(enabled) + // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息) + .apiInfo(apiInfo()) + // 设置哪些接口暴露给Swagger展示 + .select() + // 扫描所有有注解的api,用这种方式更灵活 + .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) + // 扫描指定包中的swagger注解 + //.apis(RequestHandlerSelectors.basePackage("com.carpjump")) + // 扫描所有 .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build() + /* 设置安全模式,swagger可以设置访问token */ + .securitySchemes(securitySchemes()) + .securityContexts(securityContexts()) + .pathMapping("/"); + } + + /** + * 安全模式,这里指定token通过请求头的token头传递 + */ + private List securitySchemes() { + List apiKeyList = new ArrayList(); + apiKeyList.add(new ApiKey("token", "token", In.HEADER.toValue())); + return apiKeyList; + } + + /** + * 安全上下文 + */ + private List securityContexts() { + List securityContexts = new ArrayList<>(); + securityContexts.add( + SecurityContext.builder() + .securityReferences(defaultAuth()) + .operationSelector(o -> o.requestMappingPattern().matches("/.*")) + .build()); + return securityContexts; + } + + /** + * 默认的安全上引用 + */ + private List defaultAuth() { + AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); + AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; + authorizationScopes[0] = authorizationScope; + List securityReferences = new ArrayList<>(); + securityReferences.add(new SecurityReference("Authorization", authorizationScopes)); + return securityReferences; + } + + /** + * 添加摘要信息 + */ + private ApiInfo apiInfo() { + // 用ApiInfoBuilder进行定制 + return new ApiInfoBuilder() + // 设置标题 + .title("app接口文档") + // 描述 + .description("具体包括XXX,XXX模块...") + // 作者信息 + .contact(new Contact("qfedu", null, null)) + // 版本 + .version("1.0.0") + .build(); + } +} + diff --git a/src/main/java/com/zhiyun/zhiyun03/course/controller/CourseController.java b/src/main/java/com/zhiyun/zhiyun03/course/controller/CourseController.java new file mode 100644 index 0000000..e52020f --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/course/controller/CourseController.java @@ -0,0 +1,70 @@ +package com.zhiyun.zhiyun03.course.controller; + + +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.vo.PageVO; + +import com.zhiyun.zhiyun03.utils.common.JsonResult; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +@Api("课程") +@RestController +@RequestMapping("/api/course") +public class CourseController { + + @Resource + private CourseService courseService; + + + @ApiOperation("查询课程") + @GetMapping("/selectAll") + public JsonResult queryCourse(){ + + List lists = courseService.queryCourse(); + + return JsonResult.success(lists); + } + + + @ApiOperation("课程添加") + @PostMapping("/add") + public JsonResult addCourse(Course course){ + courseService.addCourse(course); + return JsonResult.success(); + } +// +//// @ApiOperation("查询目录") +//// @PostMapping("/select") +//// public JsonResult selectDir(Directory directory){ +//// courseService.se(course); +//// return JsonResult.ok(); +//// } +// + @ApiOperation("删除课程") + @DeleteMapping("delById") + public JsonResult delCourseById(Integer id){ + courseService.delCourseById(id); + return JsonResult.success(); + } + + @ApiOperation("更新课程") + @PostMapping("update") + public JsonResult updateById(Course course){ + courseService.update(course); + 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 new file mode 100644 index 0000000..2548fa2 --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/course/mapper/CourseMapper.java @@ -0,0 +1,14 @@ +package com.zhiyun.zhiyun03.course.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.zhiyun.zhiyun03.course.entity.Course; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +@Mapper +public interface CourseMapper extends BaseMapper { + + + +} diff --git a/src/main/java/com/zhiyun/zhiyun03/course/param/PageParam.java b/src/main/java/com/zhiyun/zhiyun03/course/param/PageParam.java new file mode 100644 index 0000000..38c843a --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/course/param/PageParam.java @@ -0,0 +1,9 @@ +package com.zhiyun.zhiyun03.course.param; + +import lombok.Data; + +@Data +public class PageParam { + private Integer currentPage; + private Integer pageSize; +} diff --git a/src/main/java/com/zhiyun/zhiyun03/course/service/CourseService.java b/src/main/java/com/zhiyun/zhiyun03/course/service/CourseService.java new file mode 100644 index 0000000..ef858c1 --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/course/service/CourseService.java @@ -0,0 +1,27 @@ +package com.zhiyun.zhiyun03.course.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.zhiyun.zhiyun03.course.entity.Course; +import com.zhiyun.zhiyun03.course.vo.CourseVo; +import com.zhiyun.zhiyun03.course.vo.PageVO; +import io.swagger.annotations.ApiParam; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.List; + +public interface CourseService extends IService { +// public void addCourse(Course course); +// +// void delCourseById(Integer id); +// +// void updateById(Course course); + + + List queryCourse(); + + void addCourse(Course course); + + void delCourseById(Integer id); + + void update(Course course); +} 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 new file mode 100644 index 0000000..13e573b --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/course/service/impl/CourseServiceImpl.java @@ -0,0 +1,101 @@ +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.fasterxml.jackson.databind.util.BeanUtil; +import com.github.pagehelper.Page; +import com.zhiyun.zhiyun03.application.entity.Directory; +import com.zhiyun.zhiyun03.application.mapper.DirectoryMapper; +import com.zhiyun.zhiyun03.application.util.ConvertUtil; +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.course.vo.PageVO; +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +@Service +public class CourseServiceImpl extends ServiceImpl implements CourseService { + + @Resource + private CourseMapper courseMapper; + + @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 updateWrapper = new UpdateWrapper<>(); +// updateWrapper.set("id",course.getDirId()); +// directoryMapper.update(null,updateWrapper); + + + } + + /** + * 删除课程 + */ + @Override + public void delCourseById(Integer id) { + courseMapper.deleteById(id); + } + + /** + * 课程更新 + */ + @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); + } + + +} diff --git a/src/main/java/com/zhiyun/zhiyun03/course/vo/CourseVo.java b/src/main/java/com/zhiyun/zhiyun03/course/vo/CourseVo.java new file mode 100644 index 0000000..804ee88 --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/course/vo/CourseVo.java @@ -0,0 +1,21 @@ +package com.zhiyun.zhiyun03.course.vo; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.zhiyun.zhiyun03.course.param.PageParam; +import lombok.Data; + +@Data +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 new file mode 100644 index 0000000..f37cbee --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/course/vo/PageVO.java @@ -0,0 +1,13 @@ +package com.zhiyun.zhiyun03.course.vo; + +import lombok.Data; + +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/common/JsonResult.java b/src/main/java/com/zhiyun/zhiyun03/utils/common/JsonResult.java new file mode 100644 index 0000000..5c17099 --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/utils/common/JsonResult.java @@ -0,0 +1,48 @@ +package com.zhiyun.zhiyun03.utils.common; + +import lombok.Data; + +@Data +public class JsonResult { + + private String code; + private String msg; + private T data; + + + public static JsonResult success() { + + JsonResult jsonResult = new JsonResult(); + jsonResult.setCode("200");//code值是自行定义 + jsonResult.setMsg("success"); + + return jsonResult; + } + + public static JsonResult success(T data) { + + JsonResult jsonResult = new JsonResult(); + jsonResult.setCode("200");//code值是自行定义 + jsonResult.setMsg("success"); + jsonResult.setData(data); + return jsonResult; + } + + public static JsonResult error() { + + JsonResult jsonResult = new JsonResult(); + jsonResult.setCode("500"); + jsonResult.setMsg("fail"); + + return jsonResult; + } + + public static JsonResult error(String code, String msg) { + + JsonResult jsonResult = new JsonResult(); + jsonResult.setCode(code); + jsonResult.setMsg(msg); + + return jsonResult; + } +} diff --git a/src/main/java/com/zhiyun/zhiyun03/utils/common/ResultCode.java b/src/main/java/com/zhiyun/zhiyun03/utils/common/ResultCode.java new file mode 100644 index 0000000..bc08107 --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/utils/common/ResultCode.java @@ -0,0 +1,22 @@ +package com.zhiyun.zhiyun03.utils.common; + +public enum ResultCode { + SUCCESS("0", "成功"), + ERROR("-1", "系统异常"), + PARAM_ERROR("1001", "参数异常"), + USER_EXIST_ERROR("2001", "用户已存在"), + USER_ACCOUNT_ERROR("2002", "账号或密码错误"), + USER_NOT_EXIST_ERROR("2003", "未找到用户"), + ORDER_PAY_ERROR("3001", "库存不足,下单失败"), + PARAM_LOST_ERROR("2004", "参数缺失"), + PARAM_PASSWORD_ERROR("2005", "原密码输入错误"), + ; + + public String code; + public String msg; + + ResultCode(String code, String msg) { + this.code = code; + this.msg = msg; + } +} diff --git a/src/main/java/com/zhiyun/zhiyun03/utils/exception/GlobalExceptionHandler.java b/src/main/java/com/zhiyun/zhiyun03/utils/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..5166940 --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/utils/exception/GlobalExceptionHandler.java @@ -0,0 +1,27 @@ +package com.zhiyun.zhiyun03.utils.exception; + +import com.zhiyun.zhiyun03.utils.common.JsonResult; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +// @RestController + @ResponseBody +@RestControllerAdvice +@Slf4j +public class GlobalExceptionHandler { + + @ExceptionHandler(ServiceException.class) + public JsonResult serviceExceptionHandler(ServiceException e) { + log.error("异常信息", e); + + return JsonResult.error(e.getCode(), e.getMsg()); + } + + @ExceptionHandler(Exception.class) + public JsonResult exceptionHandler(Exception e) { + log.error("未知异常", e); + + return JsonResult.error("500", "未知异常,请联系管理员"); + } + +} diff --git a/src/main/java/com/zhiyun/zhiyun03/utils/exception/ServiceException.java b/src/main/java/com/zhiyun/zhiyun03/utils/exception/ServiceException.java new file mode 100644 index 0000000..315528b --- /dev/null +++ b/src/main/java/com/zhiyun/zhiyun03/utils/exception/ServiceException.java @@ -0,0 +1,33 @@ +package com.zhiyun.zhiyun03.utils.exception; + +public class ServiceException extends RuntimeException { + + private String code; + private String msg; + + public ServiceException() { + } + + public ServiceException(String code, String msg) { + super(msg); + this.code = code; + this.msg = msg; + + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 0653c9e..bd3bb65 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,14 +1,14 @@ server: port: 8080 - -mybatis: - mapper-locations: classpath*:mapper/**/*.xml - configuration: - map-underscore-to-camel-case: true - # ??????? - log-impl: org.apache.ibatis.logging.stdout.StdOutImpl - +# +#mybatis: +# mapper-locations: classpath*:mapper/**/*.xml +# configuration: +# map-underscore-to-camel-case: true +# # ??????? +# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl +# spring: datasource: type: com.alibaba.druid.pool.DruidDataSource @@ -16,5 +16,14 @@ spring: druid: url: jdbc:mysql://localhost:3306/zhiyun?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai username: root - password: 123456 + password: root + + +# mybatis plus的配置和mybatis类似,之前在mybatis的配置,使用mybtis-plus替换 +mybatis-plus: + configuration: + # 控制台打印日志 + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl +swagger: + enabled: true diff --git a/src/main/resources/mapper/DirectoryMapper.xml b/src/main/resources/mapper/DirectoryMapper.xml new file mode 100644 index 0000000..8055e7d --- /dev/null +++ b/src/main/resources/mapper/DirectoryMapper.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file