springBoot项目-添加购物车
springBoot项目-添加购物车
一、持久层
1、创建mapper接口及抽象方法
CartMapper接口:
package com.cy.store.mapper; import com.cy.store.entity.Cart; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.Date; /** *购物车mapper接口层及抽象方法 */ @Repository public interface CartMapper { /** * 说明:商品加入购物车时: * 购物车中没有此商品,执行新增 * 购物车已经有此商品的信息,只需要修改数量即可 */ /** * 插入购物车数据 * @param cart 插入的购物车数据 * @return 受影响的行数 */ Integer insert(Cart cart); /** * 修改购物车数据中商品的数量 * @param cid 购物车数据cid * @param num 加入的商品的数量 * @param modifiedUser 修改者 * @param modifiedTime 修改时间 * @return 受影响的行数 */ Integer updateNumByCid( @Param("cid") Integer cid, @Param("num") Integer num, @Param("modifiedUser") String modifiedUser, @Param("modifiedTime") Date modifiedTime ); /** * 根据用户uid和商品pid查询购物车数据 / 判断购物车中是否存在用户已经添加的商品信息 * @param uid 用户uid * @param pid 商品pid * @return 查询到的购物车中的数据 */ Cart findByUidAndPid(@Param("uid") Integer uid,@Param("pid") Integer pid); }
2、创建mapper映射文件,定义sql语句
CartMapper映射文件:
<?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.cy.store.mapper.CartMapper"> <!-- Integer insert(Cart cart);--> <insert id="insert"> insert into t_cart (uid,pid,price,num,created_user,created_time,modified_user,modified_time) values (#{uid},#{pid},#{price},#{num},#{createdUser},#{createdTime},#{modifiedUser},#{modifiedTime}) </insert> <!-- Integer updateNumByCid( @Param("cid") Integer cid, @Param("num") Integer num, @Param("modifiedUser") String modifiedUser, @Param("modifiedTime") Date modifiedTime ); --> <update id="updateNumByCid"> update t_cart set num=#{num}, modified_user=#{modifiedUser}, modified_time=#{modifiedTime} where cid=#{cid} </update> <!--Cart findByUidAndPid(@Param("uid") Integer uid,@Param("pid") Integer pid); --> <select id="findByUidAndPid" resultType="Cart"> select * from t_cart where uid=#{uid} and pid=#{pid} </select> </mapper>
3、自行创建单元测试方法进行测试
自行创建CartMapperTests单元测试类进行测试
二、业务层
1、创建业务层接口,定义抽象方法
ICartService接口:
package com.cy.store.service; /** * 购物车业务层接口及抽象方法 */ public interface ICartService { /** * 将商品加入到购物车中 * @param uid 用户uid * @param pid 商品pid * @param amount 添加的商品的数量 * @param username 修改者 */ void addToCart(Integer uid, Integer pid, Integer amount, String username); }
2、创建业务层接口实现类,实现抽象方法
CartServiceImpl实现类:
package com.cy.store.service.impl; import com.cy.store.entity.Cart; import com.cy.store.entity.Product; import com.cy.store.mapper.CartMapper; import com.cy.store.mapper.ProductMapper; import com.cy.store.service.ICartService; import com.cy.store.service.ex.InsertException; import com.cy.store.service.ex.UpdateServiceException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; @Service public class CartServiceImpl implements ICartService { @Autowired CartMapper cartMapper; @Autowired ProductMapper productMapper; @Override public void addToCart(Integer uid, Integer pid, Integer amount, String username) { //1、根据uid和pid查询购物车是否存在用户要添加的商品信息 Cart result = cartMapper.findByUidAndPid(uid, pid); if(result==null) { //2、查询到的结果为null,说明用户没有再购物车中添加过此商品信息===》进行新增购物车数据 //3、创建新增的cart对象 Cart cart=new Cart(); //封装数据:uid pid price amount cart.setUid(uid); cart.setPid(pid); Product product = productMapper.findById(pid);//productMapper.findById(pid)查询商品数据,得到商品价格 cart.setPrice(product.getPrice()); cart.setNum(amount);//购物车中商品中的数量即为添加的数量 /新增 //封装数据:4个日志 Date date=new Date(); cart.setCreatedUser(username); cart.setCreatedTime(date); cart.setModifiedUser(username); cart.setModifiedTime(date); // 4、调用insert(cart)执行将数据插入到数据表中 Integer insert = cartMapper.insert(cart); if(insert!=1){ throw new InsertException("插入商品数据时出现未知错误,请及时联系系统管理员"); } } //5、查询到的结果不为null,说明用户之前在购物车中添加过此商品信息,现在只需要修改购物车中此商品的数量即可 else { //6、可以从findByUidAndPid中得到购物车中原有的数据result,抽取其中的信息作为后续修改时需要信息 Integer cid=result.getCid(); Integer num=result.getNum()+amount;//从查询结果中取出原数量,与参数amount相加,得到新的数量 // 7、执行更新数量 Integer update = cartMapper.updateNumByCid(cid, num, username, new Date()); if(update!=1) { throw new UpdateServiceException("修改商品数量时出现未知错误,请及时联系系统管理员"); } } } }
3、自行创建单元测试方法进行测试
自行创建CartServiceTests单元测试类进行测试
三、控制层
CartController控制类
package com.cy.store.controller; import com.cy.store.service.ICartService; import com.cy.store.util.JsonResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpSession; @RestController @RequestMapping("/carts") public class CartController extends BaseController{ @Autowired ICartService service; @RequestMapping("/add_to_cart") public JsonResult<Void> addToCart(@RequestParam("pid") Integer pid, @RequestParam("amount")Integer amount, HttpSession session) { // 调用业务方法执行--》用户添加商品到购物车 // 从Session中获取uid和username service.addToCart(getUidFromSession(session), pid, amount, getUsernameFromSession(session)); // 返回成功 return new JsonResult<>(OK); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!