一、在实体类上添加对应的属性,并添加set、get方法
// 多一对关联,在多的一方只有一的实体 private Goods goods;
二、xml文件中编写sql语句
<resultMap id="goodsDetailMap" type="com.itlaoqi.mybatis.entity.GoodsDetail"> <id property="gdId" column="gd_id"/> <!--在查询时每得到一个goods_detail表记录,便将goods_id字段值带入到 goods.findById SQL的 select * from t_goods where goods_id = goods_id字段值,获取Goods对象 并将其赋值给GoodsDetail对象的goods属性--> <association property="goods" select="goods.findById" column="goods_id"/> </resultMap> <select id="selectManyToOne" resultMap="goodsDetailMap"> select * from t_goods_detail limit 0,10 </select>
对应的单一查询语句为:
<select id="findById" parameterType="Integer" resultType="com.itlaoqi.mybatis.entity.Goods" useCache="false"> select * from t_goods where goods_id = #{value} </select>