1. 程式人生 > >springboot 註解版 mybatis 批量增加與刪除 動態增加與修改

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語句