1、数据库中表的结构如下
2、excel中的数据如下
3、定义一个与excel表中对应的实体类
@Data
public class SubjectData {
@ExcelProperty(index = 0)
private String oneSubjectName;
@ExcelProperty(index = 1)
private String twoSubjectName;
}
4、controller
@RestController
public class EduSubjectController {
@Autowired
EduSubjectService eduSubjectService;
// 获取上传的Excel文件,从文件中读取数据保存到数据库中
@PostMapping("addSubject")
public R addSubject(MultipartFile file){
eduSubjectService.addSubject(file,eduSubjectService);
return R.ok();
}
}
5、service
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {