当前位置: 移动技术网 > IT编程>开发语言>Java > mybatis映射文件头以及批量删除以及优化

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

2020年07月17日  | 移动技术网IT编程  | 我要评论

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网