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);
}
}