springboot 注解版 mybatis 批量增加与删除 动态增加与修改

mybatis 不止提供了 @Insert @Delete @Update @Select 这些基本注解

还提供了@InsertProvider,@UpdateProvider,@DeleteProvider和@SelectProvider等基于动态语言的注解

首先需要新建一个普通类来编写批量处理语句
package cn.junhui.springboot.dao;

import cn.junhui.springboot.bean.User;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

public class BatchStudent {
/*
批量增加
*/
public String batStuAdd(Map map) {
List students = (List) map.get(“list”);
StringBuilder sb = new StringBuilder();
sb.append(“insert into tb_user(phone,name,password) values”);
MessageFormat mf = new MessageFormat(
“(#’{‘list[{0}].phone},#’{‘list[{0}].name},#’{'list[{0}].password})”
);
for (int i = 0; i < students.size(); i++) {
sb.append(mf.format(new Object[]{i}));
if (i < students.size() - 1) {
sb.append(",");
}
}
return sb.toString();
}

/*
批量删除
 */
public String batStuDel(Map map) {
    List students = (List) map.get("list");
    StringBuffer sb = new StringBuffer();
    sb.append("delete from tb_user where registerId in (");
    for (int i = 0; i < students.size(); i++) {
        sb.append("'").append(students.get(i).getRegisterid()).append("'");
        if (i < students.size() - 1) {
            sb.append(",");
        }
    }
    sb.append(")");
    return sb.toString();
}

/*
动态修改
*/
public String dynamicStuUpd(User user) {
return new SQL() {{
UPDATE(“tb_user”);
if (user.getName() != null) {
SET(“name = #{name}”);
}
if (user.getPassword() != null) {
SET(“password = #{password}”);
}
if (user.getSex() != null) {
SET(“sex=#{sex}”);
}
if (user.getBirth() != null) {
SET(“birth=#{birth}”);
}
if (user.getPhone() != null) {
SET(“phone=#{phone}”);
}
if (user.getIdcard() != null) {
SET(“idCard = #{idCard}”);
}
if (user.getAddress() != null) {
SET(“address = #{address}”);
}
if (user.getPhoto() != null) {
SET(“photo = #{photo}”);
}
if (user.getNote() != null) {
SET(“note = #{note}”);
}
WHERE(“registerId =#{registerId}”);
}}.toString();
}

/*
动态增加
 */
public String dynamicStuAdd(User user) {
    return new SQL() {{
        INSERT_INTO("tb_user");
        if (user.getName() != null) {
            VALUES("name", "#{name}");
        }
        if (user.getPassword() != null) {
            VALUES("password", "#{password}");
        }
        if (user.getSex() != null) {
           VALUES("sex", "#{sex}");
        }
        if (user.getBirth() != null) {
            VALUES("birth", "#{birth}");
        }
        if (user.getPhone() != null) {
            VALUES("phone", "#{phone}");
        }
        if (user.getIdcard() != null) {
            VALUES("idCard", "#{idCard}");
        }
        if (user.getAddress() != null) {
            VALUES("address", "#{address}");
        }
        if (user.getPhoto() != null) {
            VALUES("photo", "#{photo}");
        }
        if (user.getNote() != null) {
            VALUES("note", "#{note}");
        }
    }}.toString();
}

/*
动态查询
/
public String dynamicStuSel(User user) {
return new SQL() {{
SELECT("
");
FROM(“tb_user”);
if (user.getRegisterId() != null) {
WHERE(“registerId = #{registerId}”);
}
if (user.getAdmissionId() != null) {
WHERE(“admissionId = #{admissionId}”);
}
if (user.getPhone() != null) {
WHERE(“phone = #{phone}”);
}
if (user.getIdCard() != null) {
WHERE(“idCard=#{idCard}”);
}

    }}.toString();
}

}

接下来 在mapper里导入方法
package cn.junhui.springboot.dao;

import cn.junhui.springboot.bean.User;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface UserMapper {
/*
批量增加学生
*/
@InsertProvider(type = BatchStudent.class, method = “batStuAdd”)
Integer batStuAdd(List students);

/*
批量删除学生
 */
@DeleteProvider(type = BatchStudent.class, method = "batStuDel")
Integer batStuDel(List students);

/*
动态修改
 */
@UpdateProvider(type = BatchStudent.class, method = "dynamicStuUpd")
Integer dynamicStuUpd(User user);

/*
动态增加
 */
@InsertProvider(type = BatchStudent.class, method = "dynamicStuAdd")
Integer dynamicStuAdd(User user);

/*
动态查询
 */
@SelectProvider(type = BatchStudent.class, method = "dynamicStuSel")
Integer dynamicStuSel(User user);

}

————————————————
版权声明:本文为CSDN博主「落叶无痕00」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_37638061/article/details/83304477

你可能感兴趣的:(springboot 注解版 mybatis 批量增加与删除 动态增加与修改)