Mybatis一对多或多对多只能查出一条数据解决策略

原文:https://blog.csdn.net/ren814/article/details/81742242

<resultMap id="menuModelMap" type="com.yyzq.springboot.model.MenuModel" >
  <id column="id" property="id" jdbcType="BIGINT" />
  <result column="menuname" property="menuname" jdbcType="VARCHAR" />
  <result column="sort" property="sort" jdbcType="TINYINT" />
  <result column="inputtime" property="inputtime" jdbcType="TIMESTAMP" />
 
  <collection property="models" ofType="com.yyzq.springboot.model.Model">
    <id column="model_id" property="id" jdbcType="BIGINT" />
    <result column="modelname" property="modelname" jdbcType="VARCHAR" />
    <result column="model_sort" property="sort" jdbcType="TINYINT" />
    <result column="model_inputtime" property="inputtime" jdbcType="TIMESTAMP" />
 
    <result column="url" property="url" jdbcType="VARCHAR" />
  </collection>
</resultMap>
<select id="selectMenuModel" parameterType="Integer" resultMap="menuModelMap">
  SELECT m.id , m.menuname, m.sort, m.inputtime, d.id AS model_id, d.modelname , d.sort AS model_sort, d.url,d.inputtime AS model_inputtime
  FROM  `es_menu_model`  mm  LEFT JOIN `es_menu`  m ON m.id = mm.es_menu_id  LEFT JOIN `es_model`  d ON d.id = mm.es_model_id WHERE mm.`es_menu_id` =  #{id}
</select>

以上为正确。

错误区:在结果集中,将俩个column中的ID混淆,我这边是区分的id和model_id,如果俩个都为同样的字段,系统在数据筛选的时候会出现无法判断,所以只能读取第一条数据作为最后结果。切记,结果集有集合的,在mapper文件写column的时候不设为一样的,在具体的sql中修改列名字
————————————————
版权声明:本文为CSDN博主「北九道」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ren814/article/details/81742242

原文地址:https://www.cnblogs.com/lvchengda/p/12599815.html