一对多,多对一查询
<!--一对多操作--> <!--resultMap一对多的关系或多对一的关联映射--> <resultMap id="rmGoods" type="com.imooc.mybatis.entity.GoodsEntity"> <!--主键--> <id property="goodsId" column="goods_id"/> <!--collection 一对多的多数据集合select选择多的对象--> <!--collection的含义是在sql查询等到结果后代入到goodsDetail名称空间selectGoodsById的SQL中, 并将得到的结果集合赋值到Goods对象goodDetialList 属性中--> <collection property="goodDetialList" select="goodsDetail.selectGoodsById" column="goods_id"/> </resultMap> <select id="selectOneToMany" resultMap="rmGoods"> select * from t_goods limit 0,1 </select>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="goodsDetail"> <select id="selectGoodsById" parameterType="integer" resultType="com.imooc.mybatis.entity.GoodsDetailEntity"> select * from t_goods_detail where goods_id=#{goodsId} </select> <!--多对一--> <resultMap id="rmGoodsDetail" type="com.imooc.mybatis.entity.GoodsDetailEntity"> <id property="gdId" column="gd_id"/> <result property="goodsId" column="goods_id"/> <association property="goodsEntity" select="goods.findOneById" column="goods_id"/> </resultMap> <select id="selectManyToOne" resultMap="rmGoodsDetail"> select * from t_goods_detail limit 0,1 </select> </mapper>