Mysql mybatis 语法示例

2023-12-15 14:41:46

?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>

文章来源:https://blog.csdn.net/weixin_67573348/article/details/134920914
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。