【1】购物车实体类,注意改表的private CommodityInfo commodityInfo; 用于和商品实体类关联。并添加该字段的get和set方法
public class ShoppingCarInfo implements Serializable {
private Integer id;
private Integer personid;
private String carDate;
private Integer commodityid;
private Integer commoditynum;
private Date carTime;
private Integer isActive;
private CommodityInfo commodityInfo; //用于多表查询
private static final long serialVersionUID = 1L;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPersonid() {
return personid;
}
public void setPersonid(Integer personid) {
this.personid = personid;
}
public String getCarDate() {
return carDate;
}
public void setCarDate(String carDate) {
this.carDate = carDate == null ? null : carDate.trim();
}
public Integer getCommodityid() {
return commodityid;
}
public void setCommodityid(Integer commodityid) {
this.commodityid = commodityid;
}
public Integer getCommoditynum() {
return commoditynum;
}
public void setCommoditynum(Integer commoditynum) {
this.commoditynum = commoditynum;
}
public Date getCarTime() {
return carTime;
}
public void setCarTime(Date carTime) {
this.carTime = carTime;
}
public Integer getIsActive() {
return isActive;
}
public void setIsActive(Integer isActive) {
this.isActive = isActive;
}
public CommodityInfo getCommodityInfo() {
return commodityInfo;
}
public void setCommodityInfo(CommodityInfo commodityInfo) {
this.commodityInfo = commodityInfo;
}
}
【2】商品实体类
public class CommodityInfo implements Serializable {
private Integer id;
private String commname;
private Integer commtype;
private Integer stock;
private String picurl;
private Double price;
private Double discount;
private Double disprice;
private Integer creater;
private Date creatdate;
private Date creattime;
private String remark;
private Integer modifyer;
private Integer inUse;
private static final long serialVersionUID = 1L;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCommname() {
return commname;
}
public void setCommname(String commname) {
this.commname = commname == null ? null : commname.trim();
}
public Integer getCommtype() {
return commtype;
}
public void setCommtype(Integer commtype) {
this.commtype = commtype;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
public String getPicurl() {
return picurl;
}
public void setPicurl(String picurl) {
this.picurl = picurl == null ? null : picurl.trim();
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Double getDiscount() {
return discount;
}
public void setDiscount(Double discount) {
this.discount = discount;
}
public Double getDisprice() {
return disprice;
}
public void setDisprice(Double disprice) {
this.disprice = disprice;
}
public Integer getCreater() {
return creater;
}
public void setCreater(Integer creater) {
this.creater = creater;
}
public Date getCreatdate() {
return creatdate;
}
public void setCreatdate(Date creatdate) {
this.creatdate = creatdate;
}
public Date getCreattime() {
return creattime;
}
public void setCreattime(Date creattime) {
this.creattime = creattime;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark == null ? null : remark.trim();
}
public Integer getModifyer() {
return modifyer;
}
public void setModifyer(Integer modifyer) {
this.modifyer = modifyer;
}
public Integer getInUse() {
return inUse;
}
public void setInUse(Integer inUse) {
this.inUse = inUse;
}
@Override
public String toString() {
return "CommodityInfo [id=" + id + ", commname=" + commname + ", commtype=" + commtype + ", stock=" + stock + ", picurl=" + picurl + ", price=" + price + ", discount=" + discount + ", disprice=" + disprice + ", creater=" + creater + ", creatdate=" + creatdate + ", creattime=" + creattime + ", remark=" + remark + ", modifyer=" + modifyer + ", inUse=" + inUse + "]";
}
}
【3】ShoppingCarInfoMapper.xml写法
<!-- 购物车商品信息 -->
<select id="selectShoppingCarCommodityInfo" resultMap="shoppingCarCommodityMap">
SELECT A.ID,A.COMMODITYID,A.COMMODITYNUM,B.COMMNAME,B.PICURL FROM WX_SHOPPINGCAR A, WX_COMMODITY B WHERE A.PERSONID = 1 AND A.COMMODITYID = B.ID;
</select>
<resultMap type="com.zlw.model.ShoppingCarInfo" id="shoppingCarCommodityMap">
<id column="id" property="id" />
<result column="commodityid" property="commodityid"/>
<result column="commoditynum" property="commoditynum"/>
<association property="commodityInfo" javaType="com.zlw.model.CommodityInfo">
<result column="commname" property="commname"/>
<result column="picurl" property="picurl"/>
</association>
</resultMap>
注意:
(1)select里面的 resultMap的值 和resultMap里面的id值是对应的。
(2)association里面的property的值是ShoppingCarInfo 实体类里面定义的关联关系。
【4】测试
List<ShoppingCarInfo> list = iShopService.selectShoppingCarCommodityInfo();
for (ShoppingCarInfo shoppingCarInfo : list) {
log.info("id:" + shoppingCarInfo.getId());
log.info("name:" + shoppingCarInfo.getCommodityInfo().getCommname());
}
Assert.assertEquals(6, list.size());
注意获取结果集数据的方法。
测试结果:
10-3021:30:06[com.zlw.test.Test-114][main][2984] - id:1
10-3021:30:06[com.zlw.test.Test-115][main][2985] - name:2017夏天新款女士短袖t恤青少年韩版修身印花圆领半袖男装衣服潮
10-3021:30:06[com.zlw.test.Test-114][main][2985] - id:2
10-3021:30:06[com.zlw.test.Test-115][main][2985] - name:骆驼男装2016夏装男士短袖T恤 圆领衣服 印花男装体恤 半袖打底衫
10-3021:30:06[com.zlw.test.Test-114][main][2985] - id:3
10-3021:30:06[com.zlw.test.Test-115][main][2985] - name:夏季青少年衣服男生潮牌t恤 男士 夏秋学生 日系棉短袖半袖男小衫
10-3021:30:06[com.zlw.test.Test-114][main][2985] - id:4
10-3021:30:06[com.zlw.test.Test-115][main][2986] - name:条纹短袖T恤男士韩版衣服大码潮流男装夏季圆领体恤2016新款半袖
10-3021:30:06[com.zlw.test.Test-114][main][2986] - id:5
10-3021:30:06[com.zlw.test.Test-115][main][2986] - name:男士长袖t恤男款体恤男装衣服男春装薄款圆领纯棉T恤男打底衫白色
10-3021:30:06[com.zlw.test.Test-114][main][2986] - id:6
10-3021:30:06[com.zlw.test.Test-115][main][2986] - name:小米音响