商品和详情对象关联查询:多对一
通过n(多)的一方找到1的一方
- 在n的一方(goodsDetail)的实体类中用“1”的一方(Goods) 创建属性
public class GoodsDetail {
private int gdId;
private int goodsId;
private String gdPicUrl;
private String gdOrder;
private Goods goods;
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public int getGdId() {
return gdId;
}
public void setGdId(int gdId) {
this.gdId = gdId;
}
public int getGoodsId() {
return goodsId;
}
public void setGoodsId(int goodsId) {
this.goodsId = goodsId;
}
public String getGdPicUrl() {
return gdPicUrl;
}
public void setGdPicUrl(String gdPicUrl) {
this.gdPicUrl = gdPicUrl;
}
public String getGdOrder() {
return gdOrder;
}
public void setGdOrder(String gdOrder) {
this.gdOrder = gdOrder;
}
}
// 创建实体类
public class Goods {
private int goodsId;
private String title;
private String subTitle;
private float originalCost;
private float currentPrice;
private float discount;
private int isFreeDelivery;
private int categoryId;
private List<GoodsDetail> goodsDetails;
public List<GoodsDetail> getGoodsDetails() {
return goodsDetails;
}
public void setGoodsDetails(List<GoodsDetail> goodsDetails) {
this.goodsDetails = goodsDetails;
}
public int getGoodsId() {
return goodsId;
}
public void setGoodsId(int goodsId) {
this.goodsId = goodsId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSubTitle() {
return subTitle;
}
public void setSubTitle(String subTitle) {
this.subTitle = subTitle;
}
public float getOriginalCost() {
return originalCost;
}
public void setOriginalCost(float originalCost) {
this.originalCost = originalCost;
}
public float getCurrentPrice() {
return currentPrice;
}
public void setCurrentPrice(float currentPrice) {
this.currentPrice = currentPrice;
}
public float getDiscount() {
return discount;
}
public void setDiscount(float discount) {
this.discount = discount;
}
public int getIsFreeDelivery() {
return isFreeDelivery;
}
public void setIsFreeDelivery(int isFreeDelivery) {
this.isFreeDelivery = isFreeDelivery;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
}
- 进行对象关联的描述
<?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">
<resultMap id="rmGoodsDetail" type="com.imooc.mybatis.entity.GoodsDetail">
<id column="gd_id" property="gdId"></id>
<!--因为原有的goodsId会作为参数传给联动的SQL,所以这里需要手动为GoodsDetail 的goodsId属性赋值。-->
<result column="goods_id" property="goodsId"/>
<association property="goods" select="goods.selectById" column="goods_id"/>
</resultMap>
<select id="selectManyToOne" resultMap="rmGoodsDetail">
select * from t_goods_detail limit 0,15;
</select>
</mapper>
<?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="int" resultType="com.imooc.mybatis.entity.Goods">
select * from t_goods where goods_id=#{value}
</select>
- 测试
@Test
public void testManyToOne(){
SqlSession sqlSession = null;
try {
sqlSession = MyBatisUtils.openSession();
List<GoodsDetail> goodsDetails = sqlSession.selectList("goodsDetail.selectManyToOne");
for (GoodsDetail goodsDetail:goodsDetails){
System.out.println(goodsDetail.getGdId()+goodsDetail.getGoods().getTitle());
}
} catch (Exception e) {
throw e;
} finally {
MyBatisUtils.closeSqlSession(sqlSession);
}
}
网友评论