温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在MyBatis中实现一个表关联查询功能

发布时间:2021-01-18 14:34:53 来源:亿速云 阅读:126 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关怎么在MyBatis中实现一个表关联查询功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

Mybatis中的一对多对象关联查询查询

模拟情景,商品与商品详情:一件商品可以对应多个商品详情信息,即从商品➡商品详情方向看,属于一对多。 在一对多关系中,需要在属于一的一方的实体类中添加多的一方的集合,一般为List<>类型

 //(省去了get和set的方法)
 public class Goods {
  private Integer goodsId ;
  private String title ;
  private String subTitle ;
  private Float originalCost ;
  private Float currentPrice ;
  private Float discount ;
  private Integer isFreeDelivery ;
  private Integer categoryId ;
  //在一对多关系中,在一方添加多的一方的集合
  private List<GoodsDetail> goodsDetailLists ; 
}

在"一方"实体类对应的xml 文件中,添加配置信息

<!--  OneToMany对象关联查询
    resultMap可用于说明一对多或者多对一的映射逻辑
    id 是resultMap属性引用的标志
    type 指向One的实体(Goods)
-->
  <resultMap id="rmGoods1" type="com.imooc.mybatis.entity.Goods">
<!--    映射goods对象的主键到goods_id字段-->
    <id column="goods_id" property="goodsId"></id>
<!--
    collection的含义是,在
    sql语句得到结果后,对所有Goods对象遍历得到goods_id字段值,
    并代入到goodsDetail命名空间的selectByGoodsId的sql中执行查询
    将得到的“商品详情”集合赋值给goodsDetailsList对象
-->
    <collection property="goodsDetailLists" select="goodsDetail.selectByGoodsId"
          column="goods_id"></collection>
  </resultMap>
  <select id="selectOneToMany" resultMap="rmGoods1">
    select * from t_goods limit 0 , 1
  </select>

在“多方”对应的xml文件中添加

<mapper namespace="goodsDetail">
  <select id="selectByGoodsId" parameterType="Integer"
      resultType="com.imooc.mybatis.entity.GoodsDetail">
    select * from t_goods_detail where goods_id = #{value}
  </select>
</mapper>

至此,关于商品到商品详情的一对多查询配置就完成了。

测试

 //OneToMany
  @Test
  public void selectOneToMany(){
    SqlSession sqlSession = null ;
    try{
      sqlSession = MybatisUtils.openSession() ;
      List<Goods> list = sqlSession.selectList("goods.selectOneToMany");
      for (Goods g : list){
      //输出商品和该商品的详情信息数量
        System.out.println(g.getTitle() + ":" + g.getGoodsDetailLists().size());
      }
    }catch (Exception e){
      e.printStackTrace();
    }finally {
      MybatisUtils.closeSession(sqlSession);
    }
  }

Mybatis多对一对象关联查询

在上诉情景中,商品详情➡商品即为多对一的关系
在多对一关系中,需要在多的一方的实体类中添加一的一方的实体对象

public class GoodsDetail {
  private Integer gdId ;
  private Integer goodsId ;
  private String gdPicUrl ;
  private Integer gdOrder ;
  //多对一:在多的一方添加一的一方的实体
  private Goods goods ;
}

在多的一方xml文件中添加

<!--  多对一关系-->
  <resultMap id="rmGoodsDetail" type="com.imooc.mybatis.entity.GoodsDetail">
    <id column="gd_id" property="gdId"></id>
    <result column="goods_id" property="goodsId"></result>
    <!-- goods.selectById 为goods.xml根据主键id查找goods信息。-->
    <association property="goods" select="goods.selectById" column="goods_id"></association>
  </resultMap>
  <select id="selectManyToOne" resultMap="rmGoodsDetail">
    select * from t_goods_detail limit 0 , 1
  </select>

测试

/**
   * 多对一对象关联映射
   * */
  @Test
  public void selectManyToOne(){
    SqlSession sqlSession = null ;
    try{
      sqlSession = MybatisUtils.openSession() ;
      List<GoodsDetail> list = sqlSession.selectList("goodsDetail.selectManyToOne");
      for (GoodsDetail gd : list){
        System.out.println(gd.getGdPicUrl() + ":" + gd.getGoods().getTitle());
      }
    }catch (Exception e){
      e.printStackTrace();
    }finally {
      MybatisUtils.closeSession(sqlSession);
    }
  }

上述就是小编为大家分享的怎么在MyBatis中实现一个表关联查询功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI