在Mybatis中使用注解@多个参数查询

时间:2021-12-21 05:09:27
    @Select("SELECT * FROM wc_homework WHERE organization_id=#{classId} ORDER BY createtime DESC LIMIT #{start},#{count}")
    @Results({
            @Result(column = "course_id", property = "course_id"),
            @Result(column = "homework_id", property = "homework_id"),
            @Result(column = "organization_id", property = "organization_id"),
            @Result(property = "course", column = "course_id", one = @One(select = "selectCourse")),
            @Result(property = "viewTotalNum", column = "organization_id", one = @One(select = "SelectStudentCount")),
            @Result(property = "attachments", javaType = List.class, column = "homework_id", many = @Many(select = "selectAttachments")),
            @Result(property = "viewNum",column = "{homework_id = homework_id,organization_id = organization_id}",one = @One(select = "selectViewCounts")),
            @Result(property = "feedbackNum",column = "{homework_id = homework_id,organization_id = organization_id}",one = @One(select = "selectFeedbackCounts"))
    })

    List<HomeworkExt> selectByClassId(@Param("classId") Long classId, @Param("start") int start, @Param("count") int count);

上面的 @Result(property = "feedbackNum",column = "{homework_id = homework_id,organization_id = organization_id}",one = @One(select = "selectFeedbackCounts"))

最后一行,通过传入两个参数查询:对应下面的Map键值对。

 @Select("SELECT COUNT(DISTINCT f.user_id)  FROM wc_homework_log_see ws INNER JOIN \n" +
            "(SELECT wu.user_id, wf.`family_user_id` FROM wc_organization_user wu INNER JOIN wc_family wf ON wu.`user_id` = wf.`student_id` \n" +
            "WHERE wu.`organization_id` = #{organization_id} AND wu.`role_id` = 4) f ON f.family_user_id = ws.`user_id` \n" +
            "WHERE ws.`homework_id` = #{homework_id}")
    int selectViewCounts(Map<String,Object> map);