SpringBoot购物车功能开发步骤与范例

本文详细介绍了电商网站购物车功能的各个子功能实现,包括添加购物车、展示购物车商品列表、数量增减、勾选数据、收货地址选择及订单生成等。每个子功能涉及到的数据库操作、接口设计、异常处理及前端交互均有详尽说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、需求分析

(1)添加购物车(子功能)

在进行加入购物车功能时,先到数据库查询是否有同一商品存在,如有,则需要更改数量即可,如无,则需要添加到购物车当中。

(2)展示购物车的商品列表(子功能)

用户在登录的情况下,可以看到购物车的商品列表。

(3)购物车页面商品数量的增加和减少(子功能)

在查询到用户的购物车商品后,能通过控件来实现商品的增加和减少。

如:

(4)显示勾选的购物车数据(子功能)

用户在购物车页面可以进行商品的勾选,点击“结算”按钮后,跳转到结算页面,并显示所勾选的购物车数据。

(5)收货地址下拉列表(子功能)

在用户登录的情况下,选择购物车后跳转到结算页面,可以根据收货地址下拉列表选择相应的收货地址。

(6)生成订单(子功能)

用户可以在购物车页面进行商品的结算,并生成订单。

二、开发步骤与范例

持久层

1.添加购物车(子功能)

(1)sql语句

<?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="com.example.store.mapper.CartMapper">
    <resultMap id="Cart" type="com.example.store.domain.Cart">
        <result property="createdUser" column="created_user"></result>
        <result property="createdTime" column="created_time"></result>
        <result property="modifiedUser" column="modified_user"></result>
        <result property="modifiedTime" column="modified_time"></result>
    </resultMap>

    <insert id="insertCart" useGeneratedKeys="true" keyProperty="cid">
        insert into t_cart (uid,pid,price ,num,created_user,created_time,modified_user,modified_time)
        value (#{uid},#{pid},#{price},#{num},#{createdUser},#{createdTime},#{modifiedUser},#{modifiedTime})
    </insert>

    <update id="updateCart">
        update t_cart set num=#{num} ,modified_user=#{modifiedUser},modified_time=#{modifiedTime} where cid=#{cid}
    </update>

    <select id="findByPidAndUid" resultMap="Cart">
        select * from t_cart where pid=#{pid} and uid=#{uid}
    </select>
</mapper>

(2)设计相应的接口和抽象方法

@Mapper
public interface CartMapper {
    //添加购物车
    Integer insertCart(Cart cart);

    //更新购买的数量
    Integer updateCart(Integer num , Integer cid , String modifiedUser , Date modifiedTime);

    //查询购物车是否存在商品信息
    Cart findByPidAndUid(Integer uid ,Integer pid );
}

2.展示用户的购物车商品列表(子功能)

(1)sql语句

<select id="findByuid" resultMap="Cart">
        select cid,price,num,pid from t_cart where uid=#{uid}
 </select>

(2)设计抽象方法

//查询用户购物车的所有商品信息
    List<Cart> findByuid(Integer uid);

3.购物车页面商品数量的增加和减少(子功能)

(1)sql

<select id="findByCid" resultMap="Cart">
        select * from t_cart where cid=#{cid}
</select>

<update id="updateCart">
        update t_cart set num=#{num} ,modified_user=#{modifiedUser},modified_time=#{modifiedTime} where cid=#{cid}
    </update>

(2)设计抽象方法

//通过购物车数据id查询数据
    Cart findByCid(Integer cid);


//更新购买的数量
    Integer updateCart(Integer num , Integer cid , String modifiedUser , Date modifiedTime);

4.显示勾选的购物车数据(子功能)

(1)sql

 <select id="findByListCid" resultMap="Cart">
        select cid,price,num,pid from t_cart
        where cid in (
        <foreach collection="array" separator="," item="cids">
            #{cids}
        </foreach>
            )
        order by created_time desc
    </select>

(2)设计抽象方法

//根据购物车id查询数据
    List<Cart> findByListCid(Integer [] cids);

5.收货地址下拉列表(子功能)

(1)sql

<!-- 通过用户id查询收货地址-->
    <select id="selectAddress" resultMap="Address">
        select aid,tag ,name ,phone ,address ,province_name ,city_name,area_name ,is_default from t_address where uid=#{uid} order by is_default desc ,created_time DESC
    </select>

(2)设计抽象方法

/**
     * 根据用户的id查询用户的收货地址信息
     * @param uid 用户的id
     * @return 返回一个地址实体类对象
     */
    List<Address> selectAddress(Integer uid);

6.生成订单(子功能):根据表格数据分析,怎么获取到这些数据。(购物车、地址、订单结合)

(1)sql语句(涉及两个表)

 <insert id="insertOrder" parameterType="com.example.store.domain.Order">
        <!--通过mybatis框架提供selecKey标签获得自增长产生的ID-->
        <selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="oid">
            select LAST_INSERT_ID()
        </selectKey>
        insert into t_order (uid,recv_name,recv_phone,fanlyAddress,total_price,status,order_time,
                             pay_time,created_user,created_time,modified_user,modified_time)
        values(#{uid},#{recvName},#{recvPhone},#{fanlyAddress},#{totalPrice},#{s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值