首先,让我们来看下数据库建表语句:
CREATE TABLE behavior_reports (id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '报告ID',report_type VARCHAR(50) NOT NULL COMMENT '报告类型(daily, weekly, monthly)',start_date DATE NOT NULL COMMENT '开始日期',end_date DATE NOT NULL COMMENT '结束日期',total_users INT DEFAULT 0 COMMENT '总用户数',active_users INT DEFAULT 0 COMMENT '活跃用户数',total_visits INT DEFAULT 0 COMMENT '总访问量',avg_duration INT DEFAULT 0 COMMENT '平均停留时长(秒)',bounce_rate DECIMAL(5,2) DEFAULT 0 COMMENT '跳出率(%)',conversion_rate DECIMAL(5,2) DEFAULT 0 COMMENT '转化率(%)',retention_rate DECIMAL(5,2) DEFAULT 0 COMMENT '留存率(%)',report_data JSON COMMENT '详细报告数据',created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',INDEX idx_report_type (report_type),INDEX idx_date_range (start_date, end_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='行为分析报告表';
然后,引入依赖opencsv
<dependency><groupId>com.opencsv</groupId><artifactId>opencsv</artifactId><version>5.7.1</version></dependency></dependencies>
实现步骤
1、创建一个工具类CsvImportUtil
package com.useranalysis.util;import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import com.useranalysis.entity.BehaviorReport;
import com.useranalysis.mapper.BehaviorReportMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;/*** CSV 数据导入工具类* 用于将 CSV 文件数据导入到数据库中*/
@Slf4j
@Component
@RequiredArgsConstructor
public class CsvImportUtil {private final BehaviorReportMapper behaviorReportMapper;/*** 导入行为分析报告数据* @param csvFilePath CSV 文件路径* @return 导入的记录数*/public int importBehaviorReports(String csvFilePath) {int importCount = 0;//1、借助CSVReader读取CSV文件try (CSVReader reader = new CSVReader(new FileReader(csvFilePath))) {// 2、跳过CSV文件的表头reader.readNext();//3、逐行解析 CSV 文件,把每行数据转换为 BehaviorReport 对象。String[] line;List<BehaviorReport> reports = new ArrayList<>();//4、把解析后的 BehaviorReport 对象添加到列表中while ((line = reader.readNext()) != null) {try {BehaviorReport report = parseBehaviorReport(line);reports.add(report);importCount++;//5、每100条记录批量插入一次if (reports.size() >= 100) {behaviorReportMapper.batchInsert(reports);reports.clear();}} catch (Exception e) {log.error("解析行数据失败: {}", String.join(",", line), e);}}//6、插入剩余不足100条的记录if (!reports.isEmpty()) {behaviorReportMapper.batchInsert(reports);}log.info("成功导入 {} 条行为分析报告数据", importCount);} catch (IOException | CsvValidationException e) {log.error("导入 CSV 文件失败: {}", csvFilePath, e);throw new RuntimeException("导入 CSV 文件失败", e);}//7、返回导入的记录数return importCount;}/*** 解析单行 CSV 数据为 BehaviorReport 对象* @param line CSV 行数据* @return BehaviorReport 对象*/private BehaviorReport parseBehaviorReport(String[] line) {DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");DateTimeFormatter timestampFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//创建 BehaviorReport 对象BehaviorReport report = new BehaviorReport();//设置 BehaviorReport 对象的属性report.setId(Long.parseLong(line[0]));report.setReportType(line[1]);report.setStartDate(LocalDate.parse(line[2], dateFormatter));report.setEndDate(LocalDate.parse(line[3], dateFormatter));report.setTotalUsers(Integer.parseInt(line[4]));report.setActiveUsers(Integer.parseInt(line[5]));report.setTotalVisits(Integer.parseInt(line[6]));report.setAvgDuration(Integer.parseInt(line[7]));report.setBounceRate(new BigDecimal(line[8]));report.setConversionRate(new BigDecimal(line[9]));report.setRetentionRate(new BigDecimal(line[10]));report.setReportData(line[11]);report.setCreatedAt(LocalDateTime.parse(line[12], timestampFormatter));//返回 BehaviorReport 对象return report;}
}
2、创建实体类BehaviorReport
package com.useranalysis.entity;import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;import java.time.LocalDate;
import java.time.LocalDateTime;
import java.math.BigDecimal;@Data
@NoArgsConstructor
@AllArgsConstructor
public class BehaviorReport {private Long id;private String reportType;private LocalDate startDate;private LocalDate endDate;private Integer totalUsers;private Integer activeUsers;private Integer totalVisits;private Integer avgDuration;private BigDecimal bounceRate;private BigDecimal conversionRate;private BigDecimal retentionRate;private String reportData;private LocalDateTime createdAt;
}
3、创建mapper接口BehaviorReportMapper
package com.useranalysis.mapper;import com.useranalysis.entity.BehaviorReport;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;import java.util.List;@Mapper
public interface BehaviorReportMapper {/*** 批量插入行为分析报告数据* @param reports 报告数据列表* @return 插入的记录数*/int batchInsert(@Param("reports") List<BehaviorReport> reports);
}
4、创建xml映射BehaviorReportMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.useranalysis.mapper.BehaviorReportMapper"><insert id="batchInsert" parameterType="java.util.List">INSERT INTO behavior_reports (id, report_type, start_date, end_date, total_users, active_users,total_visits, avg_duration, bounce_rate, conversion_rate,retention_rate, report_data, created_at) VALUES <foreach collection="reports" item="report" separator=",">(#{report.id}, #{report.reportType}, #{report.startDate},#{report.endDate}, #{report.totalUsers}, #{report.activeUsers},#{report.totalVisits}, #{report.avgDuration}, #{report.bounceRate},#{report.conversionRate}, #{report.retentionRate}, #{report.reportData},#{report.createdAt})</foreach></insert>
</mapper>
使用方法
1、首先,确保已经创建了数据库表
2、在需要导入数据的地方调用
@Autowired
private CsvImportUtil csvImportUtil;// 在需要导入数据的地方调用
String csvFilePath = "src/main/resources/static/CSV_test/behavior_reports.csv";
int importCount = csvImportUtil.importBehaviorReports(csvFilePath);
csv直接导入数据库中
1、创建数据库表
2、编写测试类或controller接口
测试类CsvImportTest
package com.useranalysis;import com.useranalysis.util.CsvImportUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;@SpringBootTest
@ActiveProfiles("test")
public class CsvImportTest {@Autowiredprivate CsvImportUtil csvImportUtil;@Testpublic void testImportCsv() {// CSV文件路径String csvFilePath = "src/main/resources/static/CSV_test/behavior_reports.csv";// 导入数据int importCount = csvImportUtil.importBehaviorReports(csvFilePath);// 打印导入结果System.out.println("成功导入 " + importCount + " 条记录");}
}
controller接口CsvImportController
package com.useranalysis.controller;import com.useranalysis.util.CsvImportUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/api/csv")
@RequiredArgsConstructor
public class CsvImportController {private final CsvImportUtil csvImportUtil;@PostMapping("/import")public ResponseEntity<String> importCsv() {try {String csvFilePath = "src/main/resources/static/CSV_test/behavior_reports.csv";int importCount = csvImportUtil.importBehaviorReports(csvFilePath);return ResponseEntity.ok("成功导入 " + importCount + " 条记录");} catch (Exception e) {return ResponseEntity.internalServerError().body("导入失败:" + e.getMessage());}}
}
3、使用命令执行
运行测试类
mvn test -Dtest=CsvImportTest
通过api接口导入
curl -X POST http://localhost:8080/api/csv/import