mybatis foreach方法遍历对象

    xiaoxiao2021-03-25  112

    <delete id="deleteAppUserByIds"> delete from app_userinfo where <foreach item="item" index="key" collection="ids" open="" separator="," close=""> userid=#{item.userid} and appid=#{item.appid} </foreach> </delete>

    测试代码

    // ids = "12_34,45_67,78_90"; public int removeAppUseryIds(String ids) throws Exception { try { String[] keysArr = ids.split(","); List<AppUserInfo> targets = new ArrayList<AppUserInfo>(); String appid = ""; String userid = ""; boolean removed = false; int flag = 0; if (keysArr.length > 0) { for (int i = 0; i < keysArr.length; i++) { appid = keysArr[i].split("_")[0]; userid = keysArr[i].split("_")[1]; AppUserInfo target = new AppUserInfo(userid, appid); // 生成对象 targets.add(target); removed = appUserInfoDao.deleteAppUserByIds(targets); // 调用mybatis函数 if (removed) { flag++; } } } if(flag == keysArr.length) { return 1; } return 0; } catch (Exception e) { throw new Exception(); } }
    转载请注明原文地址: https://ju.6miu.com/read-12812.html

    最新回复(0)