午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

MyBatis中的表關聯查詢實現示例

瀏覽:108日期:2023-10-20 12:12:30
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 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 resultMap='rmGoods1'> select * from t_goods limit 0 , 1 </select>

在“多方”對應的xml文件中添加

<mapper namespace='goodsDetail'> <select 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 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 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中的表關聯查詢實現示例的文章就介紹到這了,更多相關MyBatis 表關聯查詢內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 神马午夜视频 | 久久久久久国产精品三级玉女聊斋 | 成人在线视频观看 | 亚洲第一视频网站 | 伊人久久婷婷 | 久草一区二区 | 日本三级韩国三级美三级91 | 国产精品免费一区 | 国产亚洲精品精品精品 | 一级黄色片在线 | 国产一二在线 | 亚洲成人激情小说 | 国产中文在线视频 | 中文av免费观看 | 另类图片av | 国产伦理久久精品久久久久 | 婷婷av在线| 国产精品每日更新 | 91久久国产精品 | 永久免费观看片现看 | 黄网页在线观看 | 涩涩网站免费 | 99久久久久 | 欧美成人免费一级人片100 | 中文字字幕在线中文 | 国产绿帽 | 黄色网zhan| 国内成人在线 | 狼人色综合 | wwwav视频| 奇米第四色7777| 中文字幕永久在线视频 | 精品国产一区二 | 潘金莲一级淫片aaaaaa播放1 | 亚洲一二三在线观看 | 在线观看中文字幕码 | 香蕉视频在线观看网站 | 黄色一级视频播放 | 日韩在线视频播放 | 国产三区视频 | 视屏一区|