這期內容當中小編將會給大家帶來有關怎么在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); } }
在上訴情景中,商品詳情?商品即為多對一的關系
在多對一關系中,需要在多的一方的實體類中添加一的一方的實體對象
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中實現一個表關聯查詢功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。