千峰商城-springboot项目搭建-73-购物车列表数据库实现


一、流程分析     

二、数据库实现

SQL:

#根据用户id查询当前用户的购物车信息
SELECT c.*,p.product_name,i.url
FROM shopping_cart c 
INNER JOIN product p
INNER JOIN product_img i
ON c.product_id=p.product_id AND i.item_id=p.product_id
WHERE user_id=14 AND i.is_main=1;

实体类:

ShoppingCartVO 
//新增productName,productImg属性
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ShoppingCartVO {

    private Integer cartId;
    private String productId;
    private String skuId;
    private String userId;
    private String cartNum;
    private String cartTime;
    private BigDecimal productPrice;
    private String skuProps;
    
    private String productName;
    private String productImg;

    
}

三、接口实现

 1.接口定义查询方法:

ShoppingCartMapper :
@Repository
public interface ShoppingCartMapper extends GeneralDAO {
    
    public List selectShopcartByUserId(int userId);
}

2.映射配置:

<?xml version="1.0" encoding="UTF-8"?>


  
    
    
    
    
    
    
    
    
    
  

  
    
    
    
    
    
    
    
    
    
    
    
    
  
  
  

 3.测试:

   @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Test
    public void testShoppingCart(){
        List shoppingCartVOS = shoppingCartMapper.selectShopcartByUserId(1);
        System.out.println(shoppingCartVOS);
    }
 

 

   

相关