mybatis sql 删除多行_mybatis 根据id批量删除的实现操作

第一种,直接传递给mapper.xml 集合/数组形式

delete from user where 1>2

or id in

#{item}

1.如果传入的是单参数且参数类型是一个list的时候,collection属性值为list

int deletebylogic(list list);

2.如果传入的是单参数且参数类型是一个array数组的时候, 参数类型为parametertype="int" 集合 collection的属性值为array

int deletebylogic(int[] array);

#{item}

第二种,直接在service中将数据给分装传递到mapper中

前端封装为以,为分隔符的id字符串。调用下方工具类。生成数据类型为(‘12',‘34'....)形式

/**

* stringutil.getsqlinstrbystrarray()

*

author : wyp

*

date : 2016年6月15日下午6:14:05

*

desc : 数组字符串转换为sql in 字符串拼接

* @param strarray 数组字符串

* @return sql in 字符串

*/

public static string getsqlinstrbystrarray(string str) {

stringbuffer temp = new stringbuffer();

if(stringutils.isempty(str)){

return "('')";

}

temp.append("(");

if(stringutils.isnotempty(str)){

string[] strarray=str.split(",");

if (strarray != null && strarray.length > 0 ) {

for (int i = 0; i < strarray.length; i++) {

temp.append("'");

temp.append(strarray[i]);

temp.append("'");

if (i != (strarray.length-1) ) {

temp.append(",");

}

}

}

}

temp.append(")");

return temp.tostring();

}

在mapper中直接使用 $ 符号接收即可

int deletebylogic(string ids);

delete from user where 1>2

or id in ${ids}

还有第三种。不过比较浪费资源

直接在service中循环调用mapper中的delete方法。.....

补充知识:mybatis中一次执行多条sql语句,例如一次性删除多条数据

1.首先在数据库连接url上加上allowmultiqueries=true,默认mysql是不支持一次执行多条sql语句的。

jdbc:mysql://127.0.0.1:3306/test?useunicode=true&characterencoding=utf-8&allowmultiqueries=true

2.在delete节点中添加多条语句:

delete from music_favorite where id = #{id,jdbctype=integer};

delete from music_favorite_song where f_id = #{id,jdbctype=integer};

这可以用在mybatis的级联关系删除上,删除主表记录前,先删除关联表的记录,两条一起执行。

以上这篇mybatis 根据id批量删除的实现操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持萬仟网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值