023.ManyToOne对象关联查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了023.ManyToOne对象关联查询相关的知识,希望对你有一定的参考价值。
1.在GoodsDetail.java中添加少的一方的实体
private Goods goods;
2.在goods_detail.xml中进行对象关联的描述
<resultMap id="rmGoodsDetail" type="com.imooc.mybatis.entity.GoodsDetail">
<id column="gd_id" property="gdId"/>
<result column="goods_id" property="goodsId"/>
<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>
3.测试用例
/**
* 测试多对一对象关联映射
*/
@Test
public void testManyToOne() throws Exception
SqlSession session = null;
try
session = MyBatisUtils.openSession();
List<GoodsDetail> list = session.selectList("goodsDetail.selectManyToOne");
for (GoodsDetail gd : list)
System.out.println(gd.getGdPicUrl() + ":" + gd.getGoods().getTitle());
catch (Exception e)
throw e;
finally
MyBatisUtils.closeSession(session);
以上是关于023.ManyToOne对象关联查询的主要内容,如果未能解决你的问题,请参考以下文章