newbee商城购物车模块mapper.xml

news2025/4/16 14:42:45

1.浏览代码

1)表 自定义

DROP TABLE IF EXISTS `tb_newbee_mall_shopping_cart_item`;
CREATE TABLE `tb_newbee_mall_shopping_cart_item`  (
  `cart_item_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '购物项主键id',
  `user_id` bigint(20) NOT NULL COMMENT '用户主键id',
  `goods_id` bigint(20) NOT NULL DEFAULT 0 COMMENT '关联商品id',
  `goods_count` int(11) NOT NULL DEFAULT 1 COMMENT '数量(最大为5)',
  `is_deleted` tinyint(4) NOT NULL DEFAULT 0 COMMENT '删除标识字段(0-未删除 1-已删除)',
  `create_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最新修改时间',
  PRIMARY KEY (`cart_item_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 69 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

2)实体类和Mapper接口 可由mybatis代码生成器生成,再修改

@Data
public class NewBeeMallShoppingCartItem {
    private Long cartItemId;

    private Long userId;

    private Long goodsId;

    private Integer goodsCount;

    private Byte isDeleted;

    private Date createTime;

    private Date updateTime;
}
public interface NewBeeMallShoppingCartItemMapper {
    int deleteByPrimaryKey(Long cartItemId);

    int insert(NewBeeMallShoppingCartItem record);

    int insertSelective(NewBeeMallShoppingCartItem record);

    NewBeeMallShoppingCartItem selectByPrimaryKey(Long cartItemId);

    NewBeeMallShoppingCartItem selectByUserIdAndGoodsId(@Param("newBeeMallUserId") Long newBeeMallUserId, @Param("goodsId") Long goodsId);

    List<NewBeeMallShoppingCartItem> selectByUserId(@Param("newBeeMallUserId") Long newBeeMallUserId, @Param("number") int number);

    int selectCountByUserId(Long newBeeMallUserId);

    int updateByPrimaryKeySelective(NewBeeMallShoppingCartItem record);

    List<NewBeeMallShoppingCartItem> findMyNewBeeMallCartItems (PageQueryUtil pageutil);

    int getTotalMyNewBeeMallCartItems(PageQueryUtil pageutil);
}

3)Mapper.xml 文件 可由mybatis代码生成器生成,再修改

<?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="ltd.newbee.mall.dao.NewBeeMallShoppingCartItemMapper">
    <resultMap id="BaseResultMap" type="ltd.newbee.mall.entity.NewBeeMallShoppingCartItem">
        <id column="cart_item_id" jdbcType="BIGINT" property="cartItemId"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="goods_id" jdbcType="BIGINT" property="goodsId"/>
        <result column="goods_count" jdbcType="INTEGER" property="goodsCount"/>
        <result column="is_deleted" jdbcType="TINYINT" property="isDeleted"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <sql id="Base_Column_List">
    cart_item_id, user_id, goods_id, goods_count, is_deleted, create_time, update_time
  </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tb_newbee_mall_shopping_cart_item
        where cart_item_id = #{cartItemId,jdbcType=BIGINT} and is_deleted = 0
    </select>
    <select id="selectByUserIdAndGoodsId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tb_newbee_mall_shopping_cart_item
        where user_id = #{newBeeMallUserId,jdbcType=BIGINT} and goods_id=#{goodsId,jdbcType=BIGINT} and is_deleted = 0
        limit 1
    </select>
    <select id="selectByUserId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tb_newbee_mall_shopping_cart_item
        where user_id = #{newBeeMallUserId,jdbcType=BIGINT} and is_deleted = 0
        limit #{number}
    </select>
    <select id="findMyNewBeeMallCartItms" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tb_newbee_mall_shopping_cart_item
        where user_id = #{userId,jdbcType=BIGINT} and is_deleted = 0
        <if test="start!=null and limit!=null">
            limit #{start},#{limit}
        </if>
    </select>
    <select id="getTotalMyNewBeeMallCartItems" resultType="int">
        select
        count(*)
        from tb_newbee_mall_shopping_cart_item
        where user_id = #{userId,jdbcType=BIGINT} and is_deleted = 0
    </select>
    <select id="selectByUserIdAndCartItemIds" resultType="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tb_newbee_mall_shopping_cart_item
        where 
        cart_item_id in
        <foreach item="id" collection="cartItemIds" open="(" separator="," close=")">
            #{id}
        </foreach>
        and user_id = #{newBeeMallUserId,jdbcType=BIGINT} and is_deleted = 0
    </select>
    <select id="selectCountByUserId" resultType="int">
        select
        count(*)
        from tb_newbee_mall_shopping_cart_item
        where user_id = #{newBeeMallUserId,jdbcType=BIGINT} and is_deleted = 0
    </select>
    <update id="deleteByPrimaryKey" parameterType="java.lang.Long">
    update tb_newbee_mall_shopping_cart_item set is_deleted = 1
    where cart_item_id = #{cartItemId,jdbcType=BIGINT} and is_deleted = 0
  </update>
    <insert id="insertSelective" parameterType="ltd.newbee.mall.entity.NewBeeMallShoppingCartItem">
        insert into tb_newbee_mall_shopping_cart_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="cartItemId != null">
                cart_item_id,
            </if>
            <if test="userId != null">
                user_id,
            </if>
            <if test="goodsId != null">
                goods_id,
            </if>
            <if test="goodsCount != null">
                goods_count,
            </if>
            <if test="isDeleted != null">
                is_deleted,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="cartItemId != null">
                #{cartItemId,jdbcType=BIGINT},
            </if>
            <if test="userId != null">
                #{userId,jdbcType=BIGINT},
            </if>
            <if test="goodsId != null">
                #{goodsId,jdbcType=BIGINT},
            </if>
            <if test="goodsCount != null">
                #{goodsCount,jdbcType=INTEGER},
            </if>
            <if test="isDeleted != null">
                #{isDeleted,jdbcType=TINYINT},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="ltd.newbee.mall.entity.NewBeeMallShoppingCartItem">
        update tb_newbee_mall_shopping_cart_item
        <set>
            <if test="userId != null">
                user_id = #{userId,jdbcType=BIGINT},
            </if>
            <if test="goodsId != null">
                goods_id = #{goodsId,jdbcType=BIGINT},
            </if>
            <if test="goodsCount != null">
                goods_count = #{goodsCount,jdbcType=INTEGER},
            </if>
            <if test="isDeleted != null">
                is_deleted = #{isDeleted,jdbcType=TINYINT},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        where cart_item_id = #{cartItemId,jdbcType=BIGINT}
    </update>
</mapper>

2.mapper.xml文件分段解释

1)字段

<?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="ltd.newbee.mall.dao.NewBeeMallShoppingCartItemMapper">
    <resultMap id="BaseResultMap" type="ltd.newbee.mall.entity.NewBeeMallShoppingCartItem">
        <id column="cart_item_id" jdbcType="BIGINT" property="cartItemId"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="goods_id" jdbcType="BIGINT" property="goodsId"/>
        <result column="goods_count" jdbcType="INTEGER" property="goodsCount"/>
        <result column="is_deleted" jdbcType="TINYINT" property="isDeleted"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>

2)sql查询语句

Base_Column_List 为公共字段,包含所有字段,通过  <include refid="Base_Column_List"/> 复用,避免重复书写字段。

<sql id="Base_Column_List">
    cart_item_id, user_id, goods_id, goods_count, is_deleted, create_time, update_time
  </sql>

select 明确查询范围 from 指出查询的表 where 过滤条件,只查询某字段,某状态内容 

limit 自定义要求 1表示只返回一条数据,#{number} 只返回数字(注:用#,不用$)

 
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tb_newbee_mall_shopping_cart_item
        where cart_item_id = #{cartItemId,jdbcType=BIGINT} and is_deleted = 0
    </select>
    <select id="selectByUserIdAndGoodsId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tb_newbee_mall_shopping_cart_item
        where user_id = #{newBeeMallUserId,jdbcType=BIGINT} and goods_id=#{goodsId,jdbcType=BIGINT} and is_deleted = 0
        limit 1
    </select>
    <select id="selectByUserId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tb_newbee_mall_shopping_cart_item
        where user_id = #{newBeeMallUserId,jdbcType=BIGINT} and is_deleted = 0
        limit #{number}
    </select>
    <select id="findMyNewBeeMallCartItms" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tb_newbee_mall_shopping_cart_item
        where user_id = #{userId,jdbcType=BIGINT} and is_deleted = 0
        <if test="start!=null and limit!=null">
            limit #{start},#{limit}
        </if>
    </select>
    <select id="getTotalMyNewBeeMallCartItems" resultType="int">
        select
        count(*)
        from tb_newbee_mall_shopping_cart_item
        where user_id = #{userId,jdbcType=BIGINT} and is_deleted = 0
    </select>
    <select id="selectByUserIdAndCartItemIds" resultType="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tb_newbee_mall_shopping_cart_item
        where
        cart_item_id in
        <foreach item="id" collection="cartItemIds" open="(" separator="," close=")">
            #{id}
        </foreach>
        and user_id = #{newBeeMallUserId,jdbcType=BIGINT} and is_deleted = 0
    </select>
    <select id="selectCountByUserId" resultType="int">
        select
        count(*)
        from tb_newbee_mall_shopping_cart_item
        where user_id = #{newBeeMallUserId,jdbcType=BIGINT} and is_deleted = 0
    </select>

3)sql 插入和更新语句

指定对应实体类:ltd.newbee.mall.entity.NewBeeMallShoppingCartItem

指定插入的表:tb_newbee_mall_shopping_cart_item

<trim prefix="(" suffix=")" suffixOverrides=","> 格式设置,自动包裹括号,去掉最后的,

<if test="cartItemId != null"> 非空校验,字段值非空才会包含这列

#{cartItemId,jdbcType=BIGINT} 匹配后,把下划线转换为驼峰式,  cart_item_id -> cartItemId

等效于 INSERT INTO tb_newbee_mall_shopping_cart_item ( cart_item_id,...)VALUES(...);

更新和插入类似,增加了状态的判断条件,符合状态才更新。

<insert id="insertSelective" parameterType="ltd.newbee.mall.entity.NewBeeMallShoppingCartItem">
        insert into tb_newbee_mall_shopping_cart_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="cartItemId != null">
                cart_item_id,
            </if>
            <if test="userId != null">
                user_id,
            </if>
            <if test="goodsId != null">
                goods_id,
            </if>
            <if test="goodsCount != null">
                goods_count,
            </if>
            <if test="isDeleted != null">
                is_deleted,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="cartItemId != null">
                #{cartItemId,jdbcType=BIGINT},
            </if>
            <if test="userId != null">
                #{userId,jdbcType=BIGINT},
            </if>
            <if test="goodsId != null">
                #{goodsId,jdbcType=BIGINT},
            </if>
            <if test="goodsCount != null">
                #{goodsCount,jdbcType=INTEGER},
            </if>
            <if test="isDeleted != null">
                #{isDeleted,jdbcType=TINYINT},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>

    <update id="deleteByPrimaryKey" parameterType="java.lang.Long">
    update tb_newbee_mall_shopping_cart_item set is_deleted = 1
    where cart_item_id = #{cartItemId,jdbcType=BIGINT} and is_deleted = 0
   </update>
    <update id="updateByPrimaryKeySelective" parameterType="ltd.newbee.mall.entity.NewBeeMallShoppingCartItem">
        update tb_newbee_mall_shopping_cart_item
        <set>
            <if test="userId != null">
                user_id = #{userId,jdbcType=BIGINT},
            </if>
            <if test="goodsId != null">
                goods_id = #{goodsId,jdbcType=BIGINT},
            </if>
            <if test="goodsCount != null">
                goods_count = #{goodsCount,jdbcType=INTEGER},
            </if>
            <if test="isDeleted != null">
                is_deleted = #{isDeleted,jdbcType=TINYINT},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        where cart_item_id = #{cartItemId,jdbcType=BIGINT}
    </update> 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2335115.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SQL学习笔记-聚合查询

非聚合查询和聚合查询的概念及差别 1. 非聚合查询 非聚合查询&#xff08;Non-Aggregate Query&#xff09;是指不使用聚合函数的查询。这类查询通常用于从表中检索具体的行和列数据&#xff0c;返回的结果是表中的原始数据。 示例 假设有一个名为 employees 的表&#xff…

【Vue 3 + Element Plus 实现产品标签的动态添加、删除与回显】

&#x1f680;Vue 3 Element Plus 实现产品标签的动态添加、删除与回显 在后台管理系统中&#xff0c;我们经常需要对表单数据进行动态处理&#xff0c;尤其是类似“产品标签”这样的字段&#xff0c;它需要用户能够灵活添加、删除&#xff0c;并在编辑时自动回显。今天我们就…

IntelliJ 配置(二)配置相关类库(2)LineMarkerProvider

一、介绍 LineMarkerProvider 是 IntelliJ 平台插件开发中的一个接口&#xff0c;它的作用是在编辑器左侧的“行标记区域”&#xff08;就是代码行号左边那一栏&#xff09;添加各种图标、标记或导航链接。比如Java 类中看到的&#xff1a; 小绿色三角形&#xff08;可以点击运…

从零开始学java--线性表

数据结构基础 目录 数据结构基础 线性表 顺序表 链表 顺序表和链表的区别&#xff1a; 栈 队列 线性表 线性表&#xff08;linear list&#xff09;是n个具有相同特性的数据元素的有限序列。 线性表中的元素个数就是线性表的长度&#xff0c;表的起始位置称为表头&am…

AD917X系列JESD204B MODE7使用

MODE7特殊在F8&#xff0c;M4使用2个复数通道 CH0_NCO10MHz CH1_NCO30MHZ DP_NCO50MHz DDS1偏移20MHz DDS2偏移40MHz

Spring Cloud之远程调用OpenFeign最佳实践

目录 OpenFeign最佳实践 问题引入 Feign 继承方式 创建Module 引入依赖 编写接口 打Jar包 服务提供方 服务消费方 启动服务并访问 Feign 抽取方式 创建Module 引入依赖 编写接口 打Jar包 服务消费方 启动服务并访问 服务部署 修改pom.xml文件 观察Nacos控制…

【Python爬虫】详细入门指南

目录 一、简单介绍 二、详细工作流程以及组成部分 三、 简单案例实现 一、简单介绍 在当今数字化信息飞速发展的时代&#xff0c;数据的获取与分析变得愈发重要&#xff0c;而网络爬虫技术作为一种能够从互联网海量信息中自动抓取所需数据的有效手段&#xff0c;正逐渐走入…

Win11系统 VMware虚拟机 安装教程

Win11系统 VMware虚拟机 安装教程 一、介绍 Windows 11是由微软公司&#xff08;Microsoft&#xff09;开发的操作系统&#xff0c;应用于计算机和平板电脑等设备 。于2021年6月24日发布 &#xff0c;2021年10月5日发行 。 Windows 11提供了许多创新功能&#xff0c;增加了新…

打造AI应用基础设施:Milvus向量数据库部署与运维

目录 打造AI应用基础设施&#xff1a;Milvus向量数据库部署与运维1. Milvus介绍1.1 什么是向量数据库&#xff1f;1.2 Milvus主要特点 2. Milvus部署方案对比2.1 Milvus Lite2.2 Milvus Standalone2.3 Milvus Distributed2.4 部署方案对比表 3. Milvus部署操作命令实战3.1 Milv…

【深度学习与大模型基础】第11章-Bernoulli分布,Multinoulli分布

一、Bernoulli分布 1. 基本概念 想象你抛一枚硬币&#xff1a; 正面朝上&#xff08;记为 1&#xff09;概率是 p&#xff08;比如 0.6&#xff09;。 反面朝上&#xff08;记为 0&#xff09;概率是 1-p&#xff08;比如 0.4&#xff09;。 这就是一个Bernoulli分布&…

基于Windows通过nginx代理访问Oracle数据库

基于Windows通过nginx代理访问Oracle数据库 环境说明&#xff1a; 生产环境是一套一主一备的ADG架构服务器&#xff0c;用户需要访问生产数据&#xff0c;基于安全考虑&#xff0c;生产IP地址不能直接对外服务&#xff0c;所以需要在DMZ部署一个前置机&#xff0c;并在该前置机…

北斗和GPS信号频率重叠-兼容与互操作

越来越多的同学们发现北斗三代信号的B1C&#xff0c;B2a信号居然和美国GPS L1,L5处在同样频率上&#xff1f; 为什么美国会允许这样的事情发生&#xff1f;同频率难道不干扰彼此的信号吗&#xff1f; 思博伦卫星导航技术支持文章TED 这事得从2006年联合国成立全球卫星导航系统…

python爬虫:喜马拉雅案例(破解sign值)

声明&#xff1a; 本文章中所有内容仅供学习交流使用&#xff0c;不用于其他任何目的&#xff0c;严禁用于商业用途和非法用途&#xff0c;否则由此产生的一切后果均与作者无关&#xff01; 根据上一篇文章&#xff0c;我们破解了本网站的&#xff0c;手机号和密码验证&#x…

51单片机波特率与溢出率的关系

1. 波特率与溢出率的基本关系 波特率(Baud Rate)表示串口通信中每秒传输的位数(bps),而溢出率是定时器每秒溢出的次数。在51单片机中,波特率通常通过定时器的溢出率来生成。 公式关系: 波特率=溢出率/​分频系数 其中,分频系数与定时器的工作模…

摄影测量——单像空间后方交会

空间后方交会的求解是一个非线性问题&#xff0c;通常采用最小二乘法进行迭代解算。下面我将详细介绍具体的求解步骤&#xff1a; 1. 基本公式&#xff08;共线条件方程&#xff09; 共线条件方程是后方交会的基础&#xff1a; 复制 x - x₀ -f * [m₁₁(X-Xₛ) m₁₂(Y-…

基于RV1126开发板的人脸姿态估计算法开发

1. 人脸姿态估计简介 人脸姿态估计是通过对一张人脸图像进行分析&#xff0c;获得脸部朝向的角度信息。姿态估计是多姿态问题中较为关键的步骤。一般可以用旋转矩阵、旋转向量、四元数或欧拉角表示。人脸的姿态变化通常包括上下俯仰(pitch)、左右旋转(yaw)以及平面内角度旋转(r…

鲲鹏+昇腾部署集群管理软件GPUStack,两台服务器搭建双节点集群【实战详细踩坑篇】

前期说明 配置&#xff1a;2台鲲鹏32C2 2Atlas300I duo&#xff0c;之前看网上文档&#xff0c;目前GPUstack只支持910B芯片&#xff0c;想尝试一下能不能310P也部署试试&#xff0c;毕竟华为的集群软件要收费。 系统&#xff1a;openEuler22.03-LTS 驱动&#xff1a;24.1.rc…

机器学习中 提到的张量是什么?

在机器学习中, 张量(Tensor) 是一个核心数学概念,用于表示和操作多维数据。以下是关于张量的详细解析: 一、数学定义与本质 张量在数学和物理学中的定义具有多重视角: 多维数组视角 传统数学和物理学中,张量被定义为多维数组,其分量在坐标变换时遵循协变或逆变规则。例…

edge 更新到135后,Clash 打开后,正常网页也会自动跳转

发现了一个有意思的问题&#xff1a;edge 更新135后&#xff0c;以前正常使用的clash出现了打开deepseek也会自动跳转&#xff1a; Search Resultshttps://zurefy.com/zu1.php#gsc.tab0&gsc.qdeepseek &#xff0c;也就是不需要梯子的网站打不开了&#xff0c;需要的一直正…

prime 1 靶场笔记(渗透测试)

环境说明&#xff1a; 靶机prime1和kali都使用的是NAT模式&#xff0c;网段在192.168.144.0/24。 Download (Mirror): https://download.vulnhub.com/prime/Prime_Series_Level-1.rar 一.信息收集 1.主机探测&#xff1a; 使用nmap进行全面扫描扫描&#xff0c;找到目标地址及…