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>

相关推荐

  1. Mysql mybatis 语法示例

    2023-12-12 07:30:03       68 阅读
  2. FPGA ——Verilog语法示例

    2023-12-12 07:30:03       30 阅读
  3. Markdown 常用语法示例

    2023-12-12 07:30:03       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-12 07:30:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-12 07:30:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-12 07:30:03       82 阅读
  4. Python语言-面向对象

    2023-12-12 07:30:03       91 阅读

热门阅读

  1. Docker与K8s的区别

    2023-12-12 07:30:03       69 阅读
  2. x的平方根算法(leetcode第69题)

    2023-12-12 07:30:03       63 阅读
  3. Flask存储在内存中的密钥被读取

    2023-12-12 07:30:03       57 阅读
  4. MATLAB 2021b 安装教程

    2023-12-12 07:30:03       78 阅读
  5. 微服务学习二

    2023-12-12 07:30:03       72 阅读
  6. 洛谷 P8628 [蓝桥杯 2015 国 AC] 穿越雷区

    2023-12-12 07:30:03       70 阅读
  7. 【Fiddler】IDEA配置Fiddler

    2023-12-12 07:30:03       52 阅读
  8. 面试经典150题(14)

    2023-12-12 07:30:03       62 阅读