wanghb 2 years ago
parent e15847538c
commit 2d5ca1f2a8

@ -56,6 +56,19 @@
<version>5.0.7</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
<build>

@ -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
@MapperScan("com.zhiyun.**.dao")
public class Zhiyun03Application {
public static void main(String[] args) {

@ -0,0 +1,48 @@
package com.zhiyun.zhiyun03.course.common;
import lombok.Data;
@Data
public class JsonResult<T> {
private Integer code;
private String msg;
private T data;
private Long count;
public static JsonResult ok() {
JsonResult jsonResult = new JsonResult();
jsonResult.setCode(200);//code值是自行定义
jsonResult.setMsg("success");
return jsonResult;
}
public static <T> JsonResult ok(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(int code, String msg) {
JsonResult jsonResult = new JsonResult();
jsonResult.setCode(code);
jsonResult.setMsg(msg);
return jsonResult;
}
}

@ -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);
}
}

@ -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
* <p>
* 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("/");
}
/**
* tokentoken
*/
private List<SecurityScheme> securitySchemes() {
List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>();
apiKeyList.add(new ApiKey("token", "token", In.HEADER.toValue()));
return apiKeyList;
}
/**
*
*/
private List<SecurityContext> securityContexts() {
List<SecurityContext> securityContexts = new ArrayList<>();
securityContexts.add(
SecurityContext.builder()
.securityReferences(defaultAuth())
.operationSelector(o -> o.requestMappingPattern().matches("/.*"))
.build());
return securityContexts;
}
/**
*
*/
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
List<SecurityReference> 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();
}
}

@ -0,0 +1,73 @@
package com.zhiyun.zhiyun03.course.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyun.zhiyun03.course.common.JsonResult;
import com.zhiyun.zhiyun03.course.entity.Course;
import com.zhiyun.zhiyun03.course.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api("课程")
@RestController
@RequestMapping("/api/course")
public class CourseController {
@Autowired
private CourseService courseService;
@ApiOperation("查询课程")
@GetMapping("/selectAll")
public JsonResult selectAll(@RequestParam(value = "page", required = false,defaultValue = "1") Integer pageNum,
@RequestParam(value = "limit", required = false,defaultValue = "1") Integer pageSize){
PageHelper.startPage(pageNum,pageSize);
List<Course> courseList = courseService.selectAll();
PageInfo<Course> coursePageInfo = new PageInfo<>();
JsonResult jsonResult = JsonResult.ok();
jsonResult.setData(coursePageInfo.getList());
jsonResult.setCount(coursePageInfo.getTotal());
return jsonResult;
}
@ApiOperation("课程添加")
@PostMapping("/add")
public JsonResult addCourse(Course course){
courseService.addCourse(course);
return JsonResult.ok();
}
// @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.ok();
}
@ApiOperation("更新课程")
@DeleteMapping("updateById")
public JsonResult updateById(Course course){
courseService.updateById(course);
return JsonResult.ok();
}
}

@ -0,0 +1,15 @@
package com.zhiyun.zhiyun03.course.service;
import com.zhiyun.zhiyun03.course.entity.Course;
import java.util.List;
public interface CourseService {
public void addCourse(Course course);
void delCourseById(Integer id);
void updateById(Course course);
List<Course> selectAll();
}

@ -0,0 +1,36 @@
package com.zhiyun.zhiyun03.course.service.impl;
import com.zhiyun.zhiyun03.course.dao.CourseDAO;
import com.zhiyun.zhiyun03.course.entity.Course;
import com.zhiyun.zhiyun03.course.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CourseServiceImpl implements CourseService {
@Autowired
private CourseDAO courseDAO;
@Override
public void addCourse(Course course) {
courseDAO.insert(course);
}
@Override
public void delCourseById(Integer id) {
courseDAO.deleteByPrimaryKey(id);
}
@Override
public void updateById(Course course) {
courseDAO.updateByPrimaryKey(course);
}
@Override
public List<Course> selectAll() {
return courseDAO.selectAll();
}
}

@ -0,0 +1,33 @@
package com.zhiyun.zhiyun03.directory.controller;
import com.zhiyun.zhiyun03.course.common.JsonResult;
import com.zhiyun.zhiyun03.directory.entity.Directory;
import com.zhiyun.zhiyun03.directory.servcie.DirectoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api("目录")
@RestController
@RequestMapping("/api/directory")
public class DirectoryController {
@Autowired
private DirectoryService directoryService;
@ApiOperation("添加目录")
@PostMapping("/add")
public JsonResult addDirectory(Directory directory){
directoryService.addDirectory(directory);
return JsonResult.ok();
}
}

@ -0,0 +1,7 @@
package com.zhiyun.zhiyun03.directory.servcie;
import com.zhiyun.zhiyun03.directory.entity.Directory;
public interface DirectoryService {
void addDirectory(Directory directory);
}

@ -0,0 +1,19 @@
package com.zhiyun.zhiyun03.directory.servcie.impl;
import com.zhiyun.zhiyun03.course.dao.DirectoryDAO;
import com.zhiyun.zhiyun03.directory.entity.Directory;
import com.zhiyun.zhiyun03.directory.servcie.DirectoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DirectoryServiceImpl implements DirectoryService {
@Autowired
private DirectoryDAO directoryDAO;
@Override
public void addDirectory(Directory directory) {
directoryDAO.insert(directory);
}
}

@ -12,9 +12,12 @@ mybatis:
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
driverClassName: com.mysql.cj.jdbc.Driver
druid:
url: jdbc:mysql://localhost:3306/zhiyun?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
password: root
swagger:
enabled: true

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhiyun.zhiyun03.course.dao.DirectoryDAO">
<resultMap id="BaseResultMap" type="com.zhiyun.zhiyun03.directory.entity.Directory">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="dir_name" jdbcType="VARCHAR" property="dirName" />
<result column="dir_img" jdbcType="VARCHAR" property="dirImg" />
</resultMap>
<sql id="Base_Column_List">
id, dir_name, dir_img
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from directory
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from directory
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.zhiyun.zhiyun03.directory.entity.Directory" useGeneratedKeys="true">
insert into directory (id,dir_name, dir_img)
values (#{id},#{dirName,jdbcType=VARCHAR}, #{dirImg,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.zhiyun.zhiyun03.directory.entity.Directory" useGeneratedKeys="true">
insert into directory
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="dirName != null">
dir_name,
</if>
<if test="dirImg != null">
dir_img,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="dirName != null">
#{dirName,jdbcType=VARCHAR},
</if>
<if test="dirImg != null">
#{dirImg,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.zhiyun.zhiyun03.directory.entity.Directory">
update directory
<set>
<if test="dirName != null">
dir_name = #{dirName,jdbcType=VARCHAR},
</if>
<if test="dirImg != null">
dir_img = #{dirImg,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.zhiyun.zhiyun03.directory.entity.Directory">
update directory
set dir_name = #{dirName,jdbcType=VARCHAR},
dir_img = #{dirImg,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
Loading…
Cancel
Save