mybatis如何实现多条件的in批量删除。

这个是但条件查询。
图片说明
但是我想做到的是这种的sql语句。应该怎样传递参数,如何写。
图片说明

图片说明

这个sql写上去不一样,给你上图

使用注解传多个参数,或者使用Map,实体之类,以下是使用注解方式,比较好理解
对应java代码:

 public int deleteAllByPrimaryKey(@Param("sidList")List<Integer> sidList,@Param("versionList")List<Integer> versionList);

xml代码:

 <delete id="deleteAllByPrimaryKey">
    delete from MD_RES_CAPACITIES where
    SID in
    <foreach item="sidsitem" collection="sidList" open="(" separator="," close=")">
        ${sidsitem}
    </foreach>
    and version in
    <foreach item="versionsitem" collection="versionList" open="(" separator="," close=")">
        ${versionsitem}
    </foreach>
</delete>

delete from table where id in (select id from tab where name in (#{name}) and .....)

select * from MD_RES_CAPACITIES WHERE sid in #{sidsItem} and version in #{versionItem}

select * from MD_RES_CAPACITIES WHERE sid in #{sidsItem}and version in #{versionItem}

例子:
public Capacities selectMdResCapatities(@Param("sids")ArrayList sids, @Param("versions")ArrayList versions);

xml配置这样写:


SELECT * FROM MD_RES_CAPACITIES a
    where
a.sid in
separator="," close=")">
#{item}

and a.version in
separator="," close=")">
#{item}

试一下吧 或许可以

mybatis实现多条件参数,你可以Java代码中传一个数组或者list或者map,这些都可以的,只是配置不一样,你可以参考http://blog.csdn.net/aiyawalie/article/details/52954138

看了你想要最终实现的sql样子,不建议用mybatis的循环,建议你在外部把in部分的参数直接根据你的需求拼接好传递进去。

话不多说先实体类
1、实体类

public class User implements Serializable {
    private Integer id; //id
    private String userCode; //用户编码
    private String userName; //用户名称
    private String userPassword; //用户密码
    private Integer gender;  //性别
    private Date birthday;  //出生日期
    private String phone;   //电话
    private String address; //地址
    private Integer userRole;    //用户角色
    private Integer createdBy;   //创建者
    private Date creationDate; //创建时间
    private Integer modifyBy;     //更新者
    private Date modifyDate;   //更新时间

    public User() {
    }

    public User(Integer id, String userCode, String userName, String userPassword, Integer gender, Date birthday, String phone, String address, Integer userRole, Integer createdBy, Date creationDate, Integer modifyBy, Date modifyDate) {
        this.id = id;
        this.userCode = userCode;
        this.userName = userName;
        this.userPassword = userPassword;
        this.gender = gender;
        this.birthday = birthday;
        this.phone = phone;
        this.address = address;
        this.userRole = userRole;
        this.createdBy = createdBy;
        this.creationDate = creationDate;
        this.modifyBy = modifyBy;
        this.modifyDate = modifyDate;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getUserRole() {
        return userRole;
    }

    public void setUserRole(Integer userRole) {
        this.userRole = userRole;
    }

    public Integer getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Integer createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Integer getModifyBy() {
        return modifyBy;
    }

    public void setModifyBy(Integer modifyBy) {
        this.modifyBy = modifyBy;
    }

    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userCode='" + userCode + '\'' +
                ", userName='" + userName + '\'' +
                ", userPassword='" + userPassword + '\'' +
                ", gender=" + gender +
                ", birthday=" + birthday +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", userRole=" + userRole +
                ", createdBy=" + createdBy +
                ", creationDate=" + creationDate +
                ", modifyBy=" + modifyBy +
                ", modifyDate=" + modifyDate +
                '}';
    }

2、Dao层delete方法

 /**
     * 通过id批量删除用户
     * @param list
     * @return
     * @throws RuntimeException
     */
    int deleteUser(List<Integer> list) throws RuntimeException;

3、userMapper.xml类;id一定要和Dao层的方法名一致。

<!-- 6、通过ids批量删除用户 -->
    <delete id="deleteUser" parameterType="java.util.List">
        DELETE FROM smbms_user
        WHERE id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


4、测试一把

 @Test
    public void deleteUser(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);//映射文件的namespace
        //添加多个id
        List<Integer> list = new ArrayList<Integer>();
        list.add(15);
        list.add(17);
        for (Integer li:list
             ) {
            System.out.println(li);
        }
        //调用批量删除方法
        if(userDao.deleteUser(list) == 0){
            System.out.println(userDao.deleteUser(list));
            sqlSession.rollback();//删除失败,回滚
            System.out.println("删除失败!");
        }else{
            sqlSession.commit();//删除成功,提交
            System.out.println("删除成功!");
        }
        sqlSession.close();//关流
    }