各位用户为了找寻关于解决myBatis中删除条件的拼接问题的资料费劲了很多周折。这里教程网为您整理了关于解决myBatis中删除条件的拼接问题的相关资料,仅供查阅,以下为您介绍关于解决myBatis中删除条件的拼接问题的详细内容
今天刚刚学习了mybatis,做了简单的对数据库的增删改查。在进行删除操作时,单条删除时很简单,但是批量删除的时候拼接删除条件却有些麻烦,现记录一下做法。
Sql语句中,当删除条件并不唯一的时候,我们有两种删除的sql语句,一种使用or拼接where中的条件,例如delete from 表名where 条件1 or 条件2,另一种是使用in 例如delete from 表名where 元素in( )
利用第一种删除语句在mybatis中的mapping.xml中进行拼接:
利用第二种删除语句在mybatis中的mapping.xml中进行拼接:
以上删除操作基本完成,但是,这样有一个缺陷,当查询条件idList中没有元素的时候,会删除整个表ac01中的记录。所以,我们需要对上述方法进行改进。
第二种删除语句进行改进后:
第一种删除语句进行改进后:
至此,删除操作完美完成。
补充知识:myBatis 查询时注意and拼接条件
<!-- 加个条件 -->
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29<sql id=
"queryjiachaAllPageId"
>
<
where
>
<if test=
"danweimc !=null and danweimc !=''"
>
and
jcb.danweimc
like
'%${danweimc}%'
</if>
<if test=
"ge_jianchaks !=null and ge_jianchaks !=''"
>
and
DATE_FORMAT(jcb.jianchaks,
'%Y-%m-%d'
) >= #{ge_jianchaks}
</if>
<if test=
"le_jianchaks != null and le_jianchaks != ''"
>
and
DATE_FORMAT(jcb.jianchaks,
'%Y-%m-%d'
) <= #{le_jianchaks}
</if>
<if test=
"jiancharxm !=null and jiancharxm !=''"
>
and
jcb.jiancharxm
like
'%${jiancharxm}%'
</if>
<if test=
"zhuangtai !=null and zhuangtai !=''"
>
and
yhb.zhuangtai = ${zhuangtai}
</if>
<if test=
"danweilb !=null and danweilb !=''"
>
and
yhb.yinhuanmc
like
'${danweilb}%'
</if>
<if test=
"jiedao != null and jiedao !='' and jiedao !='110105000000'"
>
and
jcb.jiedao=#{jiedao}
</if>
<if test=
"danweimcs != null and danweimcs !=''"
>
and
jcb.danweimc = #{danweimcs}
</if>
</
where
>
</sql>
例如上诉问题 要么都加and ,要么都不加 ,一定要注意,如果有加and 和没有加and的 查询会报错
以上这篇解决myBatis中删除条件的拼接问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
原文链接:https://blog.csdn.net/sinat_36059653/article/details/73822116