学生类Student里含有属性clazz(是Clazz的对象), 而Clazz类里又有学生列表StudentList
这种情况用Mybatis怎么查询
关键字 resultMap collection 剩下的自己查
额,我推荐呢就不要用这种一对多多对多关系映射来进行表查询,这样只会增加代码的复杂度,完全可以在查询语句中使用 resultType="java.util.Map"来接收查询结果,然后接口类用Map 来传递值就可以了。
比如我的一段查询
<select id="selectById" resultType="java.util.Map" parameterType="java.lang.Integer">
select tp.tp_id as tp_id ,tp.tp_name as tp_name,tp.tp_audit as tp_audit,tp.tp_publish as tp_publish,
tp.tp_enable_user as tp_enable_user, tp.tp_enable_business as tp_enable_business,
tp.tp_sale_link as tp_sale_link,tp.tp_summary as tp_summary,tp.tp_sd_id as tp_sd_id ,
tp.tp_details as tp_details,tp.tp_image as tp_image,
tt.tt_name as tt_name ,tpt.tpt_name as tpt_name ,b.b_name as b_name
from ticket_product tp
left join ticket_type tt on tt.tt_id = tp.tp_ticket_type
left join ticket_product_type tpt on tpt.tpt_id = tp.tp_product_type
left join business b on b.b_id = tp.tp_b_id
where tp.tp_id = #{tp_id,jdbcType=INTEGER}
</select>
和接口
Map<String,Object> selectById(@Param("tp_id") Integer tp_id);
当然你如果想照关系映射方式来写的话,无法就是在mapper.xml配置文件里面使用collection或者association映射
这种情况需要配置mapper.xml的映射关系,如果是一对一,需要使用关键字association,如果是一对多需要使用关键字collection,然后通过关联查询,映射到resultMap,像你这种情况,就属于,一对一的关系,一个学生刚好对应一个班级,所以需要StudentMapper.xml中的resultMap中使用association将班级的属性映射进来,只有这样,使用关联查询的时候,才能找到其对应的映射关系。