多表關聯查詢是兩個表通過主外鍵在一條SQL中完成所有數據的提取,多表級聯查詢是指通過一個對象來獲取與他關聯的另外一個對象,執行的SQL語句分為多條。
首先確定實體關系是一對多或是多對一
OneToMany對象關聯查詢
1.在Many的Mapper XML文件添加SQL語句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="goodsDetail">
<select id="selectByGoodsId" parameterType="Integer" resultType="com.MyBatis.entity.GoodsDetail"> select * from t_goods_detail where goods_id = #{value} </select>
</mapper>
2.用對象的方式表達一對多關系,在Goods(One)實體類中增加GoodsDetail(Many)的List集合
private List<GoodsDetail> goodsDetails; public List<GoodsDetail> getGoodsDetails() { return goodsDetails; } public void setGoodsDetails(List<GoodsDetail> goodsDetails) { this.goodsDetails = goodsDetails; }
3.在One的Mapper XML文件中描述對象的關聯
<!-- resultMap可用於說明一對多或多對一的映射邏輯 id 是resultMap屬性引用的標志 type 指向One的實體(Goods) -->
<resultMap id="rmGoods1" type="com.MyBatis.entity.Goods">
<!-- 映射goods對象的主鍵到goods_id字段-->
<id column="goods_id" property="goodsId"></id>
<!-- collection的含義是,在 select * from t_goods limit 0,1 得到結果后,對所有Goods對象遍歷得到Goods_id字段, 並代入到goodsDetail命名空間的findByGoodsId的SQL中執行查詢, 將得到的"商品詳情"集合賦值給goodsDetails List對象 -->
<collection property="goodsDetails" select="goodsDetail.selectByGoodsId" column="goods_id"></collection>
</resultMap>
<select id="selectOneToMany" resultMap="rmGoods1"> select * from t_goods limit 0,10 </select>
4.在mybatis-config.xml中增加mapper聲明
<!--增加mapper聲明-->
<mappers>
<mapper resource="mappers/goods.xml"></mapper>
<mapper resource="mappers/goods_detail.xml"></mapper>
</mappers>
5.測試
@Test public void testOneToMany(){ SqlSession sqlSession=null; try{ sqlSession=MyBatisUtils.openSession(); List<Goods> list = sqlSession.selectList("goods.selectOneToMany"); for(Goods g:list){ System.out.println(g.getTitle()+":"+g.getGoodsDetails().size()); } }catch (Exception e){ if(sqlSession!=null){ sqlSession.rollback(); } throw e; }finally { MyBatisUtils.closeSession(sqlSession); } }
ManyToOne對象關聯查詢
1.在One的Mapper XML文件添加SQL語句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="goods">
<select id="selectById" parameterType="Integer" resultType="com.MyBatis.entity.Goods"> select * from t_goods where goods_id = #{value} </select>
</mapper>
2.用對象的方式表達多對一關系,在GoodsDetail(Many)實體類中增加Goods(One)對象
private Goods goods; public Goods getGoods() { return goods; } public void setGoods(Goods goods) { this.goods = goods; }
3.在Many的Mapper XML中描述對象的關聯
<resultMap id="rmGoodsDetail" type="com.MyBatis.entity.GoodsDetail">
<id column="gd_id" property="gdId"></id>
<result column="goods_id" property="goodsId"></result>
<!--其他字段只要符合駝峰命名就不需要result標簽-->
<association property="goods" select="goods.selectById" column="goods_id"></association>
</resultMap>
<select id="selectManyToOne" resultMap="rmGoodsDetail"> select * from t_goods_detail limit 0,20 </select>
4.在mybatis-config.xml中增加mapper聲明
5.測試
@Test public void testManyToOne(){ 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){ if(sqlSession!=null){ sqlSession.rollback(); } throw e; }finally { MyBatisUtils.closeSession(sqlSession); } }