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<User> students = (List<User>) 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<User> students = (List<User>) 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<User> students);

    /*
    批量删除学生
     */
    @DeleteProvider(type = BatchStudent.class, method = "batStuDel")
    Integer batStuDel(List<User> 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);
}

测试一下

/*
批量增加学生测试
 */
@Test
public void batStuAdd() {
    List<User> students = new ArrayList<>();
    String phone, name, password, uuid;
    for (int i = 0; i < 10; i++) {
        uuid = UUID.randomUUID().toString().substring(0, 5);
        students.add(new User(i + "q", i + "w", i + "e"));
    }
    userMapper.batStuAdd(students);
    // return null;
}
 

中间多出来的这一列 是我自增的id

参考文章:

SpringBoot入门-Mybatis通过注解批量插入/删除

MyBatis注解应用之动态SQL语句

猜你喜欢

转载自blog.csdn.net/qq_37638061/article/details/83304477