Mysql mybatis 语法示例

 service

package com.ruoyi.goods.service;

import java.util.List;
import com.ruoyi.goods.domain.GoodsProducts;

/**
 * 商品Service接口
 * 
 * @author ruoyi
 * @date 2023-08-27
 */
public interface IGoodsProductsService 
{
    /**
     * 查询商品
     * 
     * @param ProductID 商品主键
     * @return 商品
     */
    public GoodsProducts selectGoodsProductsByProductID(Long ProductID);

    /**
     * 查询商品列表
     * 
     * @param goodsProducts 商品
     * @return 商品集合
     */
    public List<GoodsProducts> selectGoodsProductsList(GoodsProducts goodsProducts);

    /**
     * 新增商品
     * 
     * @param goodsProducts 商品
     * @return 结果
     */
    public int insertGoodsProducts(GoodsProducts goodsProducts);

    /**
     * 修改商品
     * 
     * @param goodsProducts 商品
     * @return 结果
     */
    public int updateGoodsProducts(GoodsProducts goodsProducts);

    /**
     * 批量删除商品
     * 
     * @param ProductIDs 需要删除的商品主键集合
     * @return 结果
     */
    public int deleteGoodsProductsByProductIDs(Long[] ProductIDs);

    /**
     * 删除商品信息
     * 
     * @param ProductID 商品主键
     * @return 结果
     */
    public int deleteGoodsProductsByProductID(Long ProductID);
}

 xml

<?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.ruoyi.goods.mapper.GoodsProductsMapper">
    
    <resultMap type="GoodsProducts" id="GoodsProductsResult">
        <result property="ProductID"    column="ProductID"    />
        <result property="Name"    column="Name"    />
        <result property="Price"    column="Price"    />
        <result property="Description"    column="Description"    />
        <result property="Category"    column="Category"    />
        <result property="StockQuantity"    column="StockQuantity"    />
    </resultMap>

    <sql id="selectGoodsProductsVo">
        select ProductID, Name, Price, Description, Category, StockQuantity from goods_products
    </sql>

    <select id="selectGoodsProductsList" parameterType="GoodsProducts" resultMap="GoodsProductsResult">
        <include refid="selectGoodsProductsVo"/>
        <where>
            <if test="Name != null  and Name != ''"> and Name like concat('%', #{Name}, '%')</if>
            <if test="Price != null "> and Price = #{Price}</if>
            <if test="Description != null  and Description != ''"> and Description = #{Description}</if>
            <if test="Category != null  and Category != ''"> and Category = #{Category}</if>
            <if test="StockQuantity != null "> and StockQuantity = #{StockQuantity}</if>
        </where>
    </select>
    
    <select id="selectGoodsProductsByProductID" parameterType="Long" resultMap="GoodsProductsResult">
        <include refid="selectGoodsProductsVo"/>
        where ProductID = #{ProductID}
    </select>
        
    <insert id="insertGoodsProducts" parameterType="GoodsProducts" useGeneratedKeys="true" keyProperty="ProductID">
        insert into goods_products
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="Name != null and Name != ''">Name,</if>
            <if test="Price != null">Price,</if>
            <if test="Description != null">Description,</if>
            <if test="Category != null">Category,</if>
            <if test="StockQuantity != null">StockQuantity,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="Name != null and Name != ''">#{Name},</if>
            <if test="Price != null">#{Price},</if>
            <if test="Description != null">#{Description},</if>
            <if test="Category != null">#{Category},</if>
            <if test="StockQuantity != null">#{StockQuantity},</if>
         </trim>
    </insert>

    <update id="updateGoodsProducts" parameterType="GoodsProducts">
        update goods_products
        <trim prefix="SET" suffixOverrides=",">
            <if test="Name != null and Name != ''">Name = #{Name},</if>
            <if test="Price != null">Price = #{Price},</if>
            <if test="Description != null">Description = #{Description},</if>
            <if test="Category != null">Category = #{Category},</if>
            <if test="StockQuantity != null">StockQuantity = #{StockQuantity},</if>
        </trim>
        where ProductID = #{ProductID}
    </update>

    <delete id="deleteGoodsProductsByProductID" parameterType="Long">
        delete from goods_products where ProductID = #{ProductID}
    </delete>

    <delete id="deleteGoodsProductsByProductIDs" parameterType="String">
        delete from goods_products where ProductID in 
        <foreach item="ProductID" collection="array" open="(" separator="," close=")">
            #{ProductID}
        </foreach>
    </delete>
</mapper>

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值