Mybatis因为会与数据库交互,所以经常会有返回map集合的场景,那一般mybatis返回map集合的格式是什么呢?下面小编就用一些实例与你分享分享吧。
例1:返回key不定:返回key为学员id,value集合为学员信息
javaDao层:
/** * 获取学员分类 以id为key, StudentType对象为value * * @return */ @Override public Map < Long, StudentType > getStudentTypeMap() { return this.sqlSession.selectMap("StudentTypeMapper.getStudentTypeMap", "id"); }
Mapper配置文件:
<!--获取学员分类 以id为key, StudentType对象为value--> <select id="getStudentTypeMap" resultType="StudentType"> select <include refid="edu_student_type_columns"/> from edu_student_type </select>
例2:返回指定map中的具体key
javaDao层:指定key为userPaper
public Map < String, PaperRecordDto > getExamPaperRecordMaxScoreByUserIds(String userIds) { return sqlSession.selectMap("PaperRecordMapper.getExamPaperRecordMaxScoreByUserIds", userIds, "userPaper"); }
Mapper配置文件:
<!--获取用户每张试卷的最高成绩的考试信息 以 userId_parentId 作为key, PaperRecord对象作为value 此处内层查询 在mysql5.7后 order by 字段后必须加 limit 否则外层group by 的结果仍未排序--> <select id="getExamPaperRecordMaxScoreByUserIds" parameterType="String" resultType="PaperRecordDto"> SELECT * FROM ( SELECT CONCAT_WS('_', cus_id, parent_id) AS userPaper, exam_exampaper_record.id, exam_exampaper_record.user_score AS userScore, exam_exampaper_record.cus_id AS cusId, exam_exampaper_record.parent_id AS parentId, exam_exampaper_record.add_time AS addTime FROM exam_exampaper_record WHERE exam_exampaper_record.status = 0 AND !ISNULL(parent_id) AND cus_id IN (${value}) ORDER BY user_score DESC LIMIT 100000000 )AS a GROUP BY a.cusId, a.parentId </select>
以上就是今天的所有内容了,更多相关mybatis常见问题请持续关注本网站了解详情吧。
推荐阅读: