0
点赞
收藏
分享

微信扫一扫

【教学典型案例】06.没有复用思想


目录

  • ​​一:背景介绍​​
  • ​​反例1​​
  • ​​反例2​​
  • ​​二:思路&方案​​
  • ​​反例一​​
  • ​​优化代码​​
  • ​​测试结果​​
  • ​​反例二​​
  • ​​优化代码​​
  • ​​优化前​​
  • ​​优化后​​
  • ​​测试结果​​
  • ​​三:总结​​

一:背景介绍

反例1

【教学典型案例】06.没有复用思想_反例

反例2

【教学典型案例】06.没有复用思想_mybatis_02

二:思路&方案

反例一

两个查询在线人员的接口我们可以使用一个接口进行实现,两个接口的区别主要是在于入参不一致,我们可以通过使用mybatis的动态SQL进行实现。

优化代码

Controller层

@PostMapping("/queryCourseContent")
public List<CourseContentEntity> queryCourseContent(@RequestBody CourseContentEntity courseContent){
return iCourseContentService.queryCourseContent(courseContent);
}

IService层

List<CourseContentEntity> queryCourseContent(CourseContentEntity courseContent);

ServiceImpl层

@Resource
CourseContentMapper courseContentMapper;

@Override
public List<CourseContentEntity> queryCourseContent(CourseContentEntity courseContent) {
return courseContentMapper.queryCourseContentRecord(courseContent);
}

Mapper层

//通用查询语句
List<CourseContentEntity> queryCourseContentRecord(CourseContentEntity courseContentEntity);

Mapper.xml

<select id="queryCourseContentRecord" resultMap="courseContentMap">
SELECT id,course_assembly_id,assembly_content,create_time,created_id,created_by,update_time,updated_id,updated_by
FROM tar_course_content_info
WhERE
is_delete=0
<if test="id != null"> and id = #{id} </if>
<if test="courseAssemblyId != null">and course_assembly_id = #{courseAssemblyId}</if>
<if test="assemblyContent != null">and assembly_content = #{assemblyContent}</if>
<if test="createdBy != null">and created_by = #{createdBy}</if>
<if test="updatedBy != null">and updated_by = #{updatedBy}</if>
<if test="remark != null">and remark = #{remark}</if>
</select>

测试结果

Body中什么都不传,查出来的所有课程

【教学典型案例】06.没有复用思想_mybatis_03


Body中传入参数,查出来的是某个人创建的课程

【教学典型案例】06.没有复用思想_mysql_04

反例二

将course_id和class_id抽出来作为公共数据使用

优化代码

优化前

select id,
user_id,
user_name,
questionnaire_id,
activity_name,
course_id,
class_id,
user_answer,
start_time,
update_time,
remark,
is_delete
from
`arpro_user_answer`
<where>
<choose>
<when test="id !='' and id != null">
and id=#{id}
</when>
<when test="user_answer !='' and user_answer != null">
user_answer=#{user_answer}
and course_id = #{course_id}
and class_id = #{class_id}
</when>
<when test="questionnaire_id !='' and questionnaire_id != null">
and questionnaire_id=#{questionnaire_id}
and course_id = #{course_id}
and class_id = #{class_id}
</when>
<otherwise>
and course_id = #{course_id}
and class_id = #{class_id}
and is_delete = 0
</otherwise>
</choose>
</where>
</select>

优化后

select id,
user_id,
user_name,
questionnaire_id,
activity_name,
course_id,
class_id,
user_answer,
start_time,
update_time,
remark,
is_delete
from
`arpro_user_answer`
<where>
course_id = #{course_id}
and class_id = #{class_id}
and is_delete = 0
<choose>
<when test="id !='' and id != null">
and id=#{id}
</when>
<when test="user_answer !='' and user_answer != null">
user_answer=#{user_answer}
</when>
<when test="questionnaire_id !='' and questionnaire_id != null">
and questionnaire_id=#{questionnaire_id}
</when>
</choose>
</where>
</select>

测试结果

【教学典型案例】06.没有复用思想_mysql_05

三:总结

在软件设计的时候要从全局观出发,要考虑到前端使用哪些组件,后端使用哪些接口,以及后端需要的接口是有可以复用的。在进行软件设计时,我们也要想是面向对象的设计还是面向过程的设计。如果是面向对象的设计,那么就需要考虑可复用、可扩展、可维护。


举报

相关推荐

0 条评论