问题背景:
反例1.接口没有复用思想的反例子
两个接口看起来很相似,第一个有入参而第二个没有,那么可以合并为一个实现吗?减少了代码
反例2.SQL语句没有复用思想的反例
三个红框内都有一样的参数,#{course_id}和#{class_id},在“相同”的情况下,是不是可以考虑抽象出来呢?避免传三次参数的麻烦。
优化方案
反例1优化:
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>
反例2优化:
<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"> and user_answer=#{user_answer} </when> <when test="questionnaire_id !='' and questionnaire_id != null"> and questionnaire_id=#{questionnaire_id} </when> </choose> </where>