项目实战6—没有复用思想的反例

问题背景:
反例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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值