Last active
July 20, 2017 04:14
-
-
Save jkupcho/a423bd26509adb08844642ad16e16115 to your computer and use it in GitHub Desktop.
Populate a Data Repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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