1. 程式人生 > >在Mybatis中使用註解@多個引數查詢

在Mybatis中使用註解@多個引數查詢

    @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);