先来看看购物车的构建表的语句:
CREATE TABLE IF NOT EXISTS `cart` ( `cart_id ` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `user_id` BIGINT UNSIGNED NOT NULL , `product_id` BIGINT UNSIGNED NOT NULL , `selected_item_count` int(30) COMMENT '选中的商品总数', `total_price` int(30) COMMENT '购物车选中的商品实际总价格', `real_toal_price` int(30) COMMENT '购物车选中的商品优惠后的价格', `order_count` int(30) COMMENT '购物车中订单数量', `select_order_count` int(30) COMMENT '购物车中勾选的订单数量', `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`cart_id`), foreign key(`user_id `) references `user`(`user_id`), foreign key(`product_id `) references `product`(`product_id`) ) ENGINE = InnoDB;
购物车分别有用户的id和商品的id,设置外键user_id,product_id,对应了用户表和商品表的主键
cart的自动创建用户Bean要加上product_id,user_id,
大致结构与已创建的两个表差不多,先看下目录结构:
代码我都打到下方:可以根据类名来判断:
package com.wayknew.demo.cart.request; public class CartRequestDTO { private Long productId; private Long userId; private Integer selectedItemCount; private Integer totalPrice; private Integer realToalPrice; private Integer orderCount; private Integer selectOrderCount; public Long getProductId() { return productId; } public void setProductId(Long productId) { this.productId = productId; } public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } public Integer getSelectedItemCount() { return selectedItemCount; } public void setSelectedItemCount(Integer selectedItemCount) { this.selectedItemCount = selectedItemCount; } public Integer getTotalPrice() { return totalPrice; } public void setTotalPrice(Integer totalPrice) { this.totalPrice = totalPrice; } public Integer getRealToalPrice() { return realToalPrice; } public void setRealToalPrice(Integer realToalPrice) { this.realToalPrice = realToalPrice; } public Integer getOrderCount() { return orderCount; } public void setOrderCount(Integer orderCount) { this.orderCount = orderCount; } public Integer getSelectOrderCount() { return selectOrderCount; } public void setSelectOrderCount(Integer selectOrderCount) { this.selectOrderCount = selectOrderCount; } }
package com.wayknew.demo.cart.response; import com.wayknew.demo.cart.CartDTO; import java.util.List; public class CartListResonseDTO { private Integer code; private String message; private List<CartDTO> carts; public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public List<CartDTO> getCarts() { return carts; } public void setCarts(List<CartDTO> carts) { this.carts = carts; } }
package com.wayknew.demo.cart.response; import com.wayknew.demo.cart.CartDTO; import java.util.List; public class CartResponseDTO { private Integer code; private String message; private CartDTO cart; private List<CartDTO> carts; public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public CartDTO getCart() { return cart; } public void setCart(CartDTO cart) { this.cart = cart; } public List<CartDTO> getCarts() { return carts; } public void setCarts(List<CartDTO> carts) { this.carts = carts; } }
package com.wayknew.demo.cart; import javax.persistence.*; import java.sql.Timestamp; import java.util.Objects; @Entity public class Cart { private long cartId; private long userId; private long productId; private Integer selectedItemCount; private Integer totalPrice; private Integer realToalPrice; private Integer orderCount; private Integer selectOrderCount; private Timestamp createdAt; private Timestamp updatedAt; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "cart_id") public long getCartId() { return cartId; } public void setCartId(long cartId) { this.cartId = cartId; } @Basic @Column(name = "user_id") public long getUserId() { return userId; } public void setUserId(long userId) { this.userId = userId; } @Basic @Column(name = "product_id") public long getProductId() { return productId; } public void setProductId(long productId) { this.productId = productId; } @Basic @Column(name = "selected_item_count") public Integer getSelectedItemCount() { return selectedItemCount; } public void setSelectedItemCount(Integer selectedItemCount) { this.selectedItemCount = selectedItemCount; } @Basic @Column(name = "total_price") public Integer getTotalPrice() { return totalPrice; } public void setTotalPrice(Integer totalPrice) { this.totalPrice = totalPrice; } @Basic @Column(name = "real_toal_price") public Integer getRealToalPrice() { return realToalPrice; } public void setRealToalPrice(Integer realToalPrice) { this.realToalPrice = realToalPrice; } @Basic @Column(name = "order_count") public Integer getOrderCount() { return orderCount; } public void setOrderCount(Integer orderCount) { this.orderCount = orderCount; } @Basic @Column(name = "select_order_count") public Integer getSelectOrderCount() { return selectOrderCount; } public void setSelectOrderCount(Integer selectOrderCount) { this.selectOrderCount = selectOrderCount; } @Basic @Column(name = "created_at") public Timestamp getCreatedAt() { return createdAt; } public void setCreatedAt(Timestamp createdAt) { this.createdAt = createdAt; } @Basic @Column(name = "updated_at") public Timestamp getUpdatedAt() { return updatedAt; } public void setUpdatedAt(Timestamp updatedAt) { this.updatedAt = updatedAt; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Cart cart = (Cart) o; return cartId == cart.cartId && userId == cart.userId && productId == cart.productId && Objects.equals(selectedItemCount, cart.selectedItemCount) && Objects.equals(totalPrice, cart.totalPrice) && Objects.equals(realToalPrice, cart.realToalPrice) && Objects.equals(orderCount, cart.orderCount) && Objects.equals(selectOrderCount, cart.selectOrderCount) && Objects.equals(createdAt, cart.createdAt) && Objects.equals(updatedAt, cart.updatedAt); } @Override public int hashCode() { return Objects.hash(cartId, userId, productId, selectedItemCount, totalPrice, realToalPrice, orderCount, selectOrderCount, createdAt, updatedAt); } }
package com.wayknew.demo.cart; import com.wayknew.demo.cart.request.CartRequestDTO; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class CartBO { @Resource private CartDAO cartDAO; Cart create(Long productId,Long userId,Integer selectedItemCount,Integer totalPrice,Integer realToalPrice,Integer orderCount,Integer selectOrderCount){ Cart cart = new Cart(); cart.setProductId(productId); cart.setUserId(userId); cart.setOrderCount(orderCount); cart.setRealToalPrice(realToalPrice); cart.setSelectedItemCount(selectedItemCount); cart.setSelectOrderCount(selectOrderCount); cart.setTotalPrice(totalPrice); return cartDAO.save(cart); } Cart getByCartId(Long cartId) { return cartDAO.findCartByCartId(cartId); } List<Cart> getCartList() { return cartDAO.findAll(); } void deleteCart(Long cartId) { Cart cart = cartDAO.findCartByCartId(cartId); if (cart != null) { cartDAO.delete(cart); } } Cart updateCart(Long cartId, CartRequestDTO body) { Cart cart = cartDAO.findCartByCartId(cartId); if (body.getOrderCount() != null) { cart.setOrderCount(body.getOrderCount()); } if (body.getRealToalPrice() != null) { cart.setRealToalPrice(body.getRealToalPrice()); } if (body.getSelectedItemCount() != null) { cart.setSelectedItemCount(body.getSelectedItemCount()); } if (body.getSelectOrderCount() != null) { cart.setSelectOrderCount(body.getSelectOrderCount()); } if (body.getTotalPrice() != null) { cart.setTotalPrice(body.getTotalPrice()); } cart = cartDAO.save(cart); return cart; } }
package com.wayknew.demo.cart; import com.wayknew.demo.cart.request.CartRequestDTO; import com.wayknew.demo.cart.response.CartResponseDTO; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.List; @RestController @RequestMapping(value = "/api/v1") public class CartController { @Resource private CartBO cartBO; @RequestMapping(value = "/cart", method = RequestMethod.POST) public CartResponseDTO createCart(@RequestBody CartRequestDTO body) { try { Cart cart = cartBO.create(body.getProductId(),body.getUserId(),body.getTotalPrice(),body.getSelectOrderCount(),body.getSelectedItemCount(),body.getOrderCount(),body.getRealToalPrice()); return CartFactory.successResponse(cart); } catch (Exception e) { return CartFactory.errorResponse(1, e.toString()); } } @RequestMapping(value = "/cart", method = RequestMethod.GET) public CartResponseDTO getCartList() { try { List<Cart> carts = cartBO.getCartList(); return CartFactory.successResponse(carts); } catch (Exception e) { return CartFactory.errorResponse(2, e.toString()); } } @RequestMapping(value = "/cart/{cartId}", method = RequestMethod.GET) public CartResponseDTO getCartById(@PathVariable("cartId") Long cartId) { try { Cart cart = cartBO.getByCartId(cartId); return CartFactory.successResponse(cart); } catch (Exception e) { return CartFactory.errorResponse(3, e.toString()); } } @RequestMapping(value = "/cart/{cartId}", method = RequestMethod.PATCH) public CartResponseDTO updateCart(@PathVariable("cartId") Long cartId, @RequestBody CartRequestDTO body) { try { Cart cart = cartBO.updateCart(cartId, body); return CartFactory.successResponse(cart); } catch (Exception e) { return CartFactory.errorResponse(3, e.toString()); } } @RequestMapping(value = "/cart/{cartId}", method = RequestMethod.DELETE) public CartResponseDTO deleteCart(@PathVariable("cartId") Long cartId) { try { cartBO.deleteCart(cartId); return CartFactory.successResponse(); } catch (Exception e) { return CartFactory.errorResponse(3, e.toString()); } } }
package com.wayknew.demo.cart; import org.springframework.data.jpa.repository.JpaRepository; public interface CartDAO extends JpaRepository<Cart, Long> { Cart findCartByCartId(Long cardId); }
package com.wayknew.demo.cart; import com.alibaba.fastjson.annotation.JSONField; import java.sql.Timestamp; public class CartDTO { private long cartId; private Integer selectedItemCount; private Integer totalPrice; private Integer realToalPrice; private Integer orderCount; private Integer selectOrderCount; @JSONField(format = "yyyy-MM-dd HH:mm") private Timestamp createdAt; @JSONField(format = "yyyy-MM-dd HH:mm") private Timestamp updatedAt; public long getCartId() { return cartId; } public void setCartId(long cartId) { this.cartId = cartId; } public Integer getSelectedItemCount() { return selectedItemCount; } public void setSelectedItemCount(Integer selectedItemCount) { this.selectedItemCount = selectedItemCount; } public Integer getTotalPrice() { return totalPrice; } public void setTotalPrice(Integer totalPrice) { this.totalPrice = totalPrice; } public Integer getRealToalPrice() { return realToalPrice; } public void setRealToalPrice(Integer realToalPrice) { this.realToalPrice = realToalPrice; } public Integer getOrderCount() { return orderCount; } public void setOrderCount(Integer orderCount) { this.orderCount = orderCount; } public Integer getSelectOrderCount() { return selectOrderCount; } public void setSelectOrderCount(Integer selectOrderCount) { this.selectOrderCount = selectOrderCount; } public Timestamp getCreatedAt() { return createdAt; } public void setCreatedAt(Timestamp createdAt) { this.createdAt = createdAt; } public Timestamp getUpdatedAt() { return updatedAt; } public void setUpdatedAt(Timestamp updatedAt) { this.updatedAt = updatedAt; } }
package com.wayknew.demo.cart; import com.wayknew.demo.cart.response.CartResponseDTO; import java.util.ArrayList; import java.util.List; public class CartFactory { static CartDTO toCartDTO(Cart cart) { CartDTO cartDTO = new CartDTO(); cartDTO.setCartId(cart.getCartId()); cartDTO.setOrderCount(cart.getOrderCount()); cartDTO.setRealToalPrice(cart.getRealToalPrice()); cartDTO.setSelectedItemCount(cart.getSelectedItemCount()); cartDTO.setSelectOrderCount(cart.getSelectOrderCount()); cartDTO.setTotalPrice(cart.getTotalPrice()); cartDTO.setCreatedAt(cart.getCreatedAt()); cartDTO.setUpdatedAt(cart.getUpdatedAt()); return cartDTO; } static CartResponseDTO successResponse() { CartResponseDTO res = new CartResponseDTO(); res.setCode(0); res.setMessage("success"); return res; } static CartResponseDTO successResponse(Cart cart) { CartDTO cartDTO = toCartDTO(cart); CartResponseDTO res = new CartResponseDTO(); res.setCode(0); res.setMessage("success"); res.setCart(cartDTO); return res; } static CartResponseDTO successResponse(List<Cart> carts) { List<CartDTO> cartDTOList = new ArrayList<>(); carts.forEach(user -> { cartDTOList.add(toCartDTO(user)); }); CartResponseDTO res = new CartResponseDTO(); res.setCode(0); res.setMessage("success"); res.setCarts(cartDTOList); return res; } static CartResponseDTO errorResponse(Integer code, String message) { CartResponseDTO res = new CartResponseDTO(); res.setCode(code); res.setMessage(message); return res; } }