sql使用总结

单个增删改


mapper文件

    public void addGood(Good good);
    public void updateGood(Good good);
    public void deleteGood(@Param("id") int id);

xml文件

    <insert id="addGood" parameterType="com.lequshe.goods.model.Good">
        insert into goods
        (<include refid="good_column_list"/>)
        values
        (
        #{id}, #{title},#{price},#{stock},#{saleNum},#{createTime},#{categoryName},#{brandName},#{spec}
        )
    </insert>

    <update id="updateGood" parameterType="com.lequshe.goods.model.Good">
        update goods
        <set>
            <if test="title!=null and title!=''">title=#{title},</if>
            <if test="price!=null">price=#{price},</if>
            <if test="stock!=null">stock=#{stock},</if>
            <if test="saleNum!=null">saleNum=#{saleNum},</if>
            <if test="createTime!=null">createTime=#{createTime},</if>
            <if test="categoryName!=null and categoryName!=''">categoryName=#{categoryName},</if>
            <if test="brandName!=null and brandName!=''">brandName=#{brandName}</if>
        </set>
        where id=#{id}
    </update>

    <delete id="deleteGood">
        delete from goods
        where id=#{id}
    </delete>

批量增删改


mapper文件

    public void batchAddGood(@Param("goods") List<Good> goods);
    public void batchUpdateGood(@Param("goods") List<Good> goods);
    public void batchDeleteGood(@Param("ids") List<Integer> ids);

xml文件

    <insert id="batchAddGood" parameterType="java.util.List">
        insert into goods (<include refid="good_column_list" />)
        VALUES
        <foreach collection="goods" index="index" separator="," item="item">
            (
            #{item.id}, #{item.title},#{item.price},#{item.stock},#{item.saleNum},#{item.createTime},
            #{item.categoryName},#{item.brandName},#{item.spec}
            )
        </foreach>
    </insert>

    <update id="batchUpdateGood" parameterType="java.util.List">
        update goods
        <set>
            <foreach collection="goods" open="title=case" close="end," item="item">
                <if test="item.title!=null and item.title!=''">
                when id=#{item.id} then #{item.title}
                </if>
            </foreach>
            <foreach collection="goods" item="item" open="price=case" close="end,">
                <if test="item.price!=null">
                when id=#{item.id} then #{item.price}
                </if>
            </foreach>
        </set>
            where id in (
            <foreach collection="goods" item="item" separator=",">
                #{item.id}
            </foreach>
        )
    </update>

    <delete id="batchDeleteGood" parameterType="java.util.List">
        delete from  goods
        where id in (
        <foreach collection="ids" item="item" separator=",">
            #{item}
        </foreach>
        )
    </delete>

单表查询


mapper文件

List<Good> queryGoodByCondition(@Param("id") int id, @Param("title") String title);

xml文件

    <select id="queryGoodByCondition" resultMap="GoodInfo">
        select <include refid="good_column_list"/>
        from goods
        <where>
            <if test="id!=null">id=#{id}</if>
            <if test="title!=null and title!=''">
                and title like "%"#{title}"%"
            </if>
        </where>
    </select>

多表查询


mapper文件

Good queryGoodAndLocal(@Param("id") int id);

xml文件

    <resultMap id="GoodLocal" type="com.lequshe.goods.model.Good" extends="GoodInfo">
        <collection property="local" javaType="java.util.List" ofType="com.lequshe.goods.model.GoodLang">
            <result column="lang" property="lang"/>
            <result column="local_name" property="localName"/>
        </collection>
    </resultMap>

    <select id="queryGoodAndLocal" resultMap="GoodLocal">
      select g1.id,g1.title,g1. price,g1.stock,g1.saleNum ,g1.createTime,g1.categoryName,g1.brandName,g1.spec,
      g2.lang,g2.local_name
      from goods g1
      left join goods_lang g2 on g1.id=g2.good_id
      where g1.id=#{id}
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值