Skip to content

Instantly share code, notes, and snippets.

@jkupcho
Last active July 20, 2017 04:14
Show Gist options
  • Save jkupcho/a423bd26509adb08844642ad16e16115 to your computer and use it in GitHub Desktop.
Save jkupcho/a423bd26509adb08844642ad16e16115 to your computer and use it in GitHub Desktop.
Populate a Data Repository
@Component
public class PopulateRepository implements CommandLineRunner {
@Value("classpath:path/to/file.csv")
private Resource csvResource;
private final DataRepository dataRepository;
@Autowired
public PopulateRepository(DataRepository dataRepository) {
this.dataRepository = dataRepository;
}
public void run(String... args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(csvResource.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
// ... parse the csv line into an object ...
Data data = parseCsvLine(line);
dataRepository.save(data);
}
reader.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment