TienChin 渠道管理-渠道导入

发布时间 2023-09-17 23:46:34作者: BNTang

ChannelController

@PostMapping("/importTemplate")
void importTemplate(HttpServletResponse response) {
    ExcelUtil<Channel> util = new ExcelUtil<>(Channel.class);
    util.importTemplateExcel(response, "渠道数据");
}

@Log(title = "渠道管理", businessType = BusinessType.IMPORT)
@PreAuthorize("hasPermission('tienchin:channel:import')")
@PostMapping("/importData")
AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
    ExcelUtil<Channel> util = new ExcelUtil<>(Channel.class);
    List<Channel> channelList = util.importExcel(file.getInputStream());
    return AjaxResult.success(iChannelService.importChannel(channelList, updateSupport));
}

IChannelService

/**
 * 导入渠道数据
 *
 * @param channelList   渠道数据列表
 * @param updateSupport 是否更新支持,如果已存在,则进行更新数据
 * @return {@code boolean} {@code true} 导入成功 {@code false} 导入失败
 */
boolean importChannel(List<Channel> channelList, boolean updateSupport);

ChannelServiceImpl

@Override
@Transactional(rollbackFor = Exception.class)
public boolean importChannel(List<Channel> channelList, boolean updateSupport) {
    String username = SecurityUtils.getUsername();
    LocalDateTime currentTime = LocalDateTime.now();

    List<Channel> channels = channelList
            .stream()
            .peek(channel -> {
                if (updateSupport) {
                    channel.setUpdateBy(username);
                    channel.setUpdateTime(currentTime);
                } else {
                    channel.setCreateBy(username);
                    channel.setCreateTime(currentTime);
                    channel.setChannelId(null);
                }
            }).collect(Collectors.toList());

    if (updateSupport) {
        return updateBatchById(channels);
    } else {
        return saveBatch(channels);
    }
}

!> 修复若依框架导入数据 Byte 类型数据报错的问题

更改 ReflectUtils.java 中的 invokeMethodByName 方法:

img

...

else if (cs[i] == Byte.class) {
    args[i] = Convert.toByte(args[i]);
}

...

配置 MySQL 批量插入

# 批量插入
&rewriteBatchedStatements=true

配置在 MySQL 的连接地址后面即可:

img

因为 MyBatisPlus 当中的批量插入,并没有达到我的预料效果,所以我们需要进行配置,配置方式如上。