表关系: 问题表1n 问题选项表, 需求: 查询问题时候,联查出来问题选项 //问题 实体类 public class Question{private Stringid;//ID private String content;//问题 private Stringtype;//问题类型1:单选,2:多选,3:问答 private Integersort;//排序 private ListQuestionOptionoptions;//问题选项 *** 问题表里不需要有这个属性对应的字段 //...}//问题选项 实体类 public class QuestionOption{private Stringid;//ID private String qid;//问题ID *** 问题选项表里需要有这个属性对应的字段 private String content;//选项 private Integersort;//排序 //...}方式一: 代码复用性高, 主表分页查询正确1QuestionMapper.xmlmappernamespacecom.xxx.modules.xxx.mapper.QuestionMapperresultMapidBaseResultMaptypecom.xxx.modules.xxx.entity.QuestionidcolumnidpropertyidjdbcTypeVARCHAR/resultcolumncontentpropertycontentjdbcTypeVARCHAR/resultcolumntypepropertytypejdbcTypeVARCHAR/resultcolumnsortpropertysortjdbcTypeINTEGER/collectionpropertyoptionsjavaTypejava.util.ArrayListofTypecom.xxx.modules.xxx.entity.QuestionOptionselectcom.xxx.modules.xxx.mapper.QuestionOptionMapper.selectListcolumn{qidid,sortsort}/!-- qid/sort是定义的变量名, id/sort是主表的字段id/sort, 先查出主表的结果, 然后主表记录数是几 就执行几次 collection 的select, javaType和ofType 写不写都行, select的值: 对应xml的namespace 对应xml中的代码片段的id, column作为select语句的参数传入,如果只传一个参数id可以简写:columnid--/resultMap!-- 查询列表 --selectidselectListresultMapBaseResultMapSELECT pq.id, pq.content, pq.type, pq.sort FROM question AS pqwhere/where/selectQuestionOptionMapper.xmlmappernamespacecom.xxx.modules.xxx.mapper.QuestionOptionMapper!-- 查询列表 --selectidselectListresultTypeQuestionOptionSELECT pqo.id, pqo.content, pqo.sort FROM question_option AS pqowherepqo.qid#{qid} !-- 变量名 qid 对应上文的 qid --!-- 如果上文中 collection只传一个参数columnid,只要类型匹配,在这里随便写个变量名就可以取到值#{xyz} --/where/select方式二: 只需要执行一次sql查询, 主表分页查询不正确1QuestionMapper.xmlmappernamespacecom.xxx.modules.xxx.mapper.QuestionMapperresultMapidBaseResultMaptypecom.xxx.modules.xxx.entity.QuestionidcolumnidpropertyidjdbcTypeVARCHAR/resultcolumncontentpropertycontentjdbcTypeVARCHAR/resultcolumntypepropertytypejdbcTypeVARCHAR/resultcolumnsortpropertysortjdbcTypeINTEGER/collectionpropertyoptionsjavaTypejava.util.ArrayListofTypecom.xxx.modules.data.entity.QuestionOptionidcolumno_idpropertyidjdbcTypeVARCHAR/resultcolumno_contentpropertycontentjdbcTypeVARCHAR/resultcolumno_sortpropertysortjdbcTypeINTEGER//collection!-- 列的别名 o_id,o_content,o_sort , 起别名是因为主子表都有这几个字段 这里要写 ofType, javaType还是可以不写 --/resultMap!-- 查询列表 --selectidselectListresultMapBaseResultMapSELECT pq.id, pq.content, pq.type, pq.sort ,pqo.id AS oid ,pqo.content AS ocontent ,pqo.sort AS osort!-- 联查子表字段,起别名 --FROM question AS pq LEFT JOIN question_option pqo ON pq.idpqo.qid!-- 联查子表 --where/where/select