@InsertProvider實現批量插入


方法上引用@InsertProvider注解 

@InsertProvider(type = UrlBlackDAOProvider.class, method = "insertAll")
    void batchSaveBlackList(@Param("list") List<UrlBlackInfo> blacklists);

 

寫一個名為UrlBlackDAOProvider類,並且類里寫一個名為insertAll的方法

 public String insertAll(Map map) {
        List<UrlBlackInfo> urlBlack = (List<UrlBlackInfo>) map.get("list");
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO tb_url_blacklist ");
        sb.append("(url, receive_num, url_type, create_time) ");
        sb.append("VALUES ");
        MessageFormat mf = new MessageFormat("(#'{'list[{0}].url},#'{'list[{0}].receiveNum},#'{'list[{0}].urlType},#'{'list[{0}].createTime})");
        for (int i = 0; i < urlBlack.size(); i++) {
            sb.append(mf.format(new Object[]{i}));
            if (i < urlBlack.size() - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

 

第二種寫法:

@Insert("<script> INSERT INTO tb_thirdstatistic_info "
+ "(serviceName,channel,thirdName,requestTimes,channelResponseCount,channelRequestTimes,htttpOKTimes,clientErrorTimes,serverErrorTimes,date) "
+ "VALUES "
+ "<foreach collection = 'infoList' item='record' separator=',' > "
+ " (#{record.serviceName}, "
+ " #{record.channel},"
+ " #{record.thirdName},"
+ " #{record.requestTimes},"
+ " #{record.channelResponseCount}, "
+ " #{record.channelRequestTimes}, "
+ " #{record.htttpOKTimes}, "
+ " #{record.clientErrorTimes}, "
+ " #{record.serverErrorTimes}, "
+ " #{record.date}) "
+ "</foreach> "
+ "</script>")
boolean bathInsertStatitisticsInfo(@Param("infoList") Set<StatitisticsInfo> infoList);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM