成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術(shù)文章
文章詳情頁

MyBatis中的表關(guān)聯(lián)查詢實(shí)現(xiàn)示例

瀏覽:55日期:2023-10-20 12:12:30
Mybatis中的一對多對象關(guān)聯(lián)查詢查詢

模擬情景,商品與商品詳情:一件商品可以對應(yīng)多個(gè)商品詳情信息,即從商品➡商品詳情方向看,屬于一對多。 在一對多關(guān)系中,需要在屬于一的一方的實(shí)體類中添加多的一方的集合,一般為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 ; //在一對多關(guān)系中,在一方添加多的一方的集合 private List<GoodsDetail> goodsDetailLists ; }

在'一方'實(shí)體類對應(yīng)的xml 文件中,添加配置信息

<!-- OneToMany對象關(guān)聯(lián)查詢 resultMap可用于說明一對多或者多對一的映射邏輯 id 是resultMap屬性引用的標(biāo)志 type 指向One的實(shí)體(Goods)--> <resultMap type='com.imooc.mybatis.entity.Goods'><!-- 映射goods對象的主鍵到goods_id字段--> <id column='goods_id' property='goodsId'></id><!-- collection的含義是,在 sql語句得到結(jié)果后,對所有Goods對象遍歷得到goods_id字段值, 并代入到goodsDetail命名空間的selectByGoodsId的sql中執(zhí)行查詢 將得到的“商品詳情”集合賦值給goodsDetailsList對象--> <collection property='goodsDetailLists' select='goodsDetail.selectByGoodsId' column='goods_id'></collection> </resultMap> <select resultMap='rmGoods1'> select * from t_goods limit 0 , 1 </select>

在“多方”對應(yīng)的xml文件中添加

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

至此,關(guān)于商品到商品詳情的一對多查詢配置就完成了。

測試

//OneToMany @Test public void selectOneToMany(){ SqlSession sqlSession = null ; try{ sqlSession = MybatisUtils.openSession() ; List<Goods> list = sqlSession.selectList('goods.selectOneToMany'); for (Goods g : list){ //輸出商品和該商品的詳情信息數(shù)量System.out.println(g.getTitle() + ':' + g.getGoodsDetailLists().size()); } }catch (Exception e){ e.printStackTrace(); }finally { MybatisUtils.closeSession(sqlSession); } }Mybatis多對一對象關(guān)聯(lián)查詢

在上訴情景中,商品詳情➡商品即為多對一的關(guān)系在多對一關(guān)系中,需要在多的一方的實(shí)體類中添加一的一方的實(shí)體對象

public class GoodsDetail { private Integer gdId ; private Integer goodsId ; private String gdPicUrl ; private Integer gdOrder ; //多對一:在多的一方添加一的一方的實(shí)體 private Goods goods ;}

在多的一方xml文件中添加

<!-- 多對一關(guān)系--> <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根據(jù)主鍵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>測試

/** * 多對一對象關(guān)聯(lián)映射 * */ @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); } }

到此這篇關(guān)于MyBatis中的表關(guān)聯(lián)查詢實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)MyBatis 表關(guān)聯(lián)查詢內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Mybatis 數(shù)據(jù)庫
相關(guān)文章:
主站蜘蛛池模板: 在线a亚洲视频播放在线观看 | 国产成人综合久久精品亚洲 | 国产成人午夜性a一级毛片 国产成人午夜性视频影院 国产成人香蕉久久久久 | 色偷偷成人网免费视频男人的天堂 | 久久久成人影院 | 美国第一毛片 | 欧美69视频 | 欧美毛片日韩一级在线 | 欧美在线精品一区二区三区 | 99久久99久久精品免费看子 | 亚洲欧美日本在线 | 亚洲不卡影院 | 国产成人毛片亚洲精品不卡 | 久久99精品九九九久久婷婷 | 国产大片在线观看 | 国产欧美久久精品 | 久草成人在线视频 | 成年人视频在线免费播放 | 爱呦视频在线播放网址 | 国产精品免费精品自在线观看 | 亚洲成人网页 | 久久精品视频免费在线观看 | 成人黄色免费看 | 欧美成人毛片免费网站 | 国产成人99精品免费观看 | 久久久一级 | 久久久久久青草大香综合精品 | 国产亚洲人成网站在线观看 | 成人欧美在线 | 国产原创自拍 | 亚洲一区在线观看视频 | pgone太大了兽王免费视频 | 青青草国产免费国产是公开 | 亚洲成人一区 | 成人网18免费下 | 一级特黄特黄的大片免费 | 日韩欧美亚洲综合久久99e | 99国内精品| 美国黑人特大一级毛片 | 国内精品自产拍在线观看91 | 国产成人综合久久精品红 |