@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