一、根据id查询课程发布信息

方式一:业务层组装多个表多次的查询结果
方式二:数据访问层进行关联查询
我们使用第二种方式实现

1、定义vo

  1. package com.guli.edu.vo;
  2. @ApiModel(value = "课程发布信息")
  3. @Data
  4. public class CoursePublishVo implements Serializable {
  5. private static final long serialVersionUID = 1L;
  6. private String title;
  7. private String cover;
  8. private Integer lessonNum;
  9. private String subjectLevelOne;
  10. private String subjectLevelTwo;
  11. private String teacherName;
  12. private String price;//只用于显示
  13. }

2、数据访问层

接口:CourseMapper.java

  1. package com.guli.edu.mapper;
  2. public interface CourseMapper extends BaseMapper<Course> {
  3. CoursePublishVo selectCoursePublishVoById(String id);
  4. }

实现:CourseMapper.xml

  1. <select id="getCoursePublishVoById" resultType="com.guli.edu.vo.CoursePublishVo">
  2. SELECT
  3. c.title,
  4. c.cover,
  5. c.lesson_num AS lessonNum,
  6. CONVERT(c.price, DECIMAL(8,2)) AS price,
  7. s1.title AS subjectLevelOne,
  8. s2.title AS subjectLevelTwo,
  9. t.name AS teacherName
  10. FROM
  11. edu_course c
  12. LEFT JOIN edu_teacher t ON c.teacher_id = t.id
  13. LEFT JOIN edu_subject s1 ON c.subject_parent_id = s1.id
  14. LEFT JOIN edu_subject s2 ON c.subject_id = s2.id
  15. WHERE
  16. c.id = #{id}
  17. </select>

3、业务层

接口:CourseService.java

CoursePublishVo getCoursePublishVoById(String id);

实现:CourseServiceImpl.java

@Override
public CoursePublishVo getCoursePublishVoById(String id) {
    return baseMapper.getCoursePublishVoById(id);
}

4、web层

@ApiOperation(value = "根据ID获取课程发布信息")
@GetMapping("course-publish-info/{id}")
public R getCoursePublishVoById(
   @ApiParam(name = "id", value = "课程ID", required = true)
   @PathVariable String id){


   CoursePublishVo courseInfoForm = courseService.getCoursePublishVoById(id);
   return R.ok().data("item", courseInfoForm);

}

测试:报告异常
AbstractHandlerExceptionResolver.java:194 |org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver |Resolved exception caused by handler execution: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.guli.edu.mapper.CourseMapper.getCoursePublishVoById

问题分析:
dao层编译后只有class文件,没有mapper.xml,因为maven工程在默认情况下src/main/java目录下的所有资源文件是不发布到target目录下的,

image.png

解决方案:
1、在guli_edu的pom中配置如下节点

<!-- 项目打包时会将java目录中的*.xml文件也进行打包 -->
<build>
  <resources>
      <resource>
          <directory>src/main/java</directory>
          <includes>
              <include>**/*.xml</include>
          </includes>
          <filtering>false</filtering>
      </resource>
  </resources>

</build>

重新打包项目会发现target目录下出现了xml文件夹
image.png
2、在Spring Boot配置文件中添加配置

#配置mapper xml文件的路径
mybatis-plus.mapper-locations=classpath:com/guli/edu/mapper/xml/*.xml

二、根据id发布课程

1、web层

@ApiOperation(value = "根据id发布课程")
@PutMapping("publish-course/{id}")
public R publishCourseById(
   @ApiParam(name = "id", value = "课程ID", required = true)
   @PathVariable String id){


   courseService.publishCourseById(id);
   return R.ok();

}

2、service层

接口

void publishCourseById(String id);

实现

@Override
public boolean publishCourseById(String id) {
   Course course = new Course();
   course.setId(id);
   course.setStatus(Course.COURSE_NORMAL);
   Integer count = baseMapper.updateById(course);
   return null != count && count > 0;

}