@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);
posted @ 2018-12-25 10:16  金州恶汉拉文  阅读(8631)  评论(0编辑  收藏  举报