欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis映射文件头以及批量删除以及优化

程序员文章站 2022-03-13 20:23:43
mybatis映射文件头链接https://mybatis.org/mybatis-3/zh/getting-started.html...

mybatis映射文件头

链接

https://mybatis.org/mybatis-3/zh/getting-started.html

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

批量删除时

int deleteObjects(@Param(“ids”) Integer…ids);
namespace=“com.test.dao.GoodsDao” namespace后面是类的全限定类名

如果在映射文件中collection使用ids 需要在dao层的操作语句中加@param注解open 属性是开始,close是结束,separator是中间间隔符号 item是中间的 每一个元素,#{id}占位符获取每个id值
如果不使用可以用array
映射文件代码

<mapper namespace="com.test.dao.GoodsDao">

    <delete id="deleteObjects">

        delete from tb_goods

        where id in <!-- (1,2,3,4,5) -->

        <foreach collection="ids"

                 open="("

                 close=")"

                 separator=","

                 item="id">

            #{id}

        </foreach>

    </delete>

</mapper>

数据操作语句优化

第一种

   <delete id="deleteObjects">

        delete from tb_goods
<choose>
    <when test="ids!=null and ids.length>0">
        where id in <!-- (1,2,3,4,5) -->

        <foreach collection="ids"

                 open="("

                 close=")"

                 separator=","

                 item="id">

            #{id}

        </foreach>
     </when>
    <otherwise>
        where 1=2;
    </otherwise>
</choose>
    </delete>

第二种

 <delete id="deleteObjects">

        delete from tb_goods
        <where>
<choose>
    <when test="ids!=null and ids.length>0">
         id in <!-- (1,2,3,4,5) -->

        <foreach collection="ids"

                 open="("

                 close=")"

                 separator=","

                 item="id">

            #{id}

        </foreach>
     </when>
    <otherwise>
         1=2;
    </otherwise>
</choose>
</where>
    </delete>

第三种

<delete id="deleteObjects">

        delete from tb_goods
        <where>

    <if test="ids!=null and ids.length>0">
         id in <!-- (1,2,3,4,5) -->

        <foreach collection="ids"

                 open="("

                 close=")"

                 separator=","

                 item="id">

            #{id}

        </foreach>
     </if>
   or 1=2
</where>
    </delete>

误删binlog恢复

本文地址:https://blog.csdn.net/lpw12121/article/details/107375692

相关标签: http