Mybatis批量插入注解形式(Mysql和Oracle)

数据库:PostgreSQL,MYSQL

@Mapper
public interface CheckQualificationInfoMapper {
    // 方法一
    @Insert({""})
    void batchSaveInfo(@Param("poList") List poList);

    //方法二:
    @InsertProvider(type = BatchSaveCheckDTO.class,method = "insertAll" )
    public void insertAll(@Param("poList") List poList);

    public  class BatchSaveCheckDTO {
        public String insertAll(@Param("poList") List poList) {          
            StringBuilder sb = new StringBuilder();
            sb.append("INSERT INTO check_qualification_info ");
            sb.append("( name, id_no, mobile,apply_no ,cust_no ,batch_id, reason) ");
            sb.append("VALUES ");
            MessageFormat mf = new MessageFormat("(#'{'poList[{0}].name},#'{'poList[{0}].idNo},#'{'poList[{0}].phone},#'{'poList[{0}].applyNo}" +
                    ",#'{'poList[{0}].custNo},#'{'poList[{0}].batchId},#'{'poList[{0}].reason})");
            for (int i = 0; i < poList.size(); i++) {
                sb.append(mf.format(new Object[]{i}));
                if (i < poList.size() - 1) {
                    sb.append(",");
                }
            }
            return sb.toString();
        }
    }

    //方法三:
@Insert("")
boolean bathInsertStatitisticsInfo(@Param("infoList")  List infoList);

}

public class CheckQualificationInfoPO {
    private String id;
    
    private String name;

    private String phone;

    private String idNo;

    private String applyNo;

    private String reason;

    private String custNo;
    
    private String batchId;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

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

    public String getIdNo() {
        return idNo;
    }

    public void setIdNo(String idNo) {
        this.idNo = idNo;
    }

    public String getApplyNo() {
        return applyNo;
    }

    public void setApplyNo(String applyNo) {
        this.applyNo = applyNo;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getCustNo() {
        return custNo;
    }

    public void setCustNo(String custNo) {
        this.custNo = custNo;
    }

    public String getBatchId() {
        return batchId;
    }

    public void setBatchId(String batchId) {
        this.batchId = batchId;
    }

    @Override
    public String toString() {
        return "CheckQualificationInfoPO{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                ", idNo='" + idNo + '\'' +
                ", applyNo='" + applyNo + '\'' +
                ", reason='" + reason + '\'' +
                ", custNo='" + custNo + '\'' +
                ", batchId='" + batchId + '\'' +
                '}';
    }
}

 

数据库:Oracle

import org.apache.ibatis.annotations.*;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;


@Mapper
public interface ContactsMapper {

    /***
    * @Description: 方法一;loanType和 applyNo为每条数据的公共参数
    * @Date: 2019-10-16
    */
    @Insert("")
    public void insertTelList(@Param("map") Map map);


    /**
    * @Description:  方法二;loanType和 applyNo为每条数据的公共参数;设置到每个Map里面(比较死板,不如方法三)
    * @Date: 2019-10-16
    */
    @InsertProvider(type = InsertContactsAll.class , method = "insertContactsAll")
    int inserAll(@Param("contactList") List> list );

    public  class  InsertContactsAll{
        public String insertContactsAll( @Param("contactList") List> list ){
            StringBuilder sb = new StringBuilder();
            sb.append(" INSERT ALL ");
            String inVal=" INTO csp_appl_tel_list ( LOAN_TYPE , APPLY_NO,NAME, TEL_NO,  RELATION, DATE_CREATED ) VALUES ";
            sb.append(inVal);
            MessageFormat mf = new MessageFormat("("
                    + " #'{'contactList[{0}].loanType,jdbcType=VARCHAR},  #'{'contactList[{0}].applyNo,jdbcType=VARCHAR}, #'{'contactList[{0}].name,jdbcType=VARCHAR}, #'{'contactList[{0}].telNo,jdbcType=VARCHAR}"
                    + ", #'{'contactList[{0}].relation,jdbcType=VARCHAR}, #'{'contactList[{0}].dateCreated,jdbcType=DATE} )" );
            for (int i = 0; i < list.size(); i++) {
                sb.append(mf.format(new Object[] { i+"" }));
                if (i < list.size() - 1) {
                    sb.append(inVal);
                }
            }
            sb.append(" select 1 from dual");
            return sb.toString();
        }
    }

    /**
    * @Description: 方法三;这里考虑的每条数据具有公共参数,公共参数无需封装在每一条数据里面
    * @Param: [map]  注意!!!此处无需再加注解@Param,其一会导致内部类方法的入参出现相同的两份,其二会导致MessageFormat里面参数的取值层级发生变化
    * @Date: 2019-10-16
    */
    @InsertProvider(type = InsertContactsAllData.class , method = "insertContactsAllData")
    int inserAllData( Map map );

    public  class  InsertContactsAllData{
        public String insertContactsAllData(  Map map  ){
            StringBuilder sb = new StringBuilder();
            List> list = (List>) map.get("contactList");
            sb.append(" INSERT ALL ");
            String inVal=" INTO csp_appl_tel_list ( LOAN_TYPE ,APPLY_NO, NAME , TEL_NO , RELATION , DATE_CREATED) VALUES ";
            sb.append(inVal);
            MessageFormat mf = new MessageFormat("("
                    + " #'{'loanType,jdbcType=VARCHAR},  #'{'applyNo,jdbcType=VARCHAR}, #'{'contactList[{0}].name,jdbcType=VARCHAR}, #'{'contactList[{0}].telNo,jdbcType=VARCHAR},"
                    + "#'{'contactList[{0}].relation,jdbcType=VARCHAR},#'{'contactList[{0}].dateCreated,jdbcType=DATE} )" );
            for (int i = 0; i < list.size(); i++) {
                sb.append(mf.format(new Object[] { i+"" }));
                if (i < list.size() - 1) {
                    sb.append(inVal);
                }
            }
            sb.append(" select 1 from dual");
            return sb.toString();
        }
    }
}


如下粗略的测试:
@Component
@RefreshScope
public class MyTestJob {
    private static Logger logger = LoggerFactory.getLogger(MyTestJob.class);

    @Autowired
    ContactsMapper contactsMapper;

    @Scheduled(cron = "0 */1 * * * ?")
    public void push() {
        try {
            // 方法一
            Map telListMap = new HashMap();
            List> contactList = new ArrayList>();
            int i = 1000;
            for (int n =0; n> contactList2 = new ArrayList>();
            for (int n =0; n> contactList3 = new ArrayList>();
            for (int n =0; n

 

你可能感兴趣的:(Mybatis)