Spring Boot insert sample data into database upon startup

后端 未结 3 1054
广开言路
广开言路 2020-12-15 08:19

What is the right way for creating test data upon server startup and inserting them into the database (I\'m using a JPA/JDBC backed Postgres instance).

Preferably in

3条回答
  •  自闭症患者
    2020-12-15 08:52

    You can do like this

        @SpringBootApplication
        public class H2Application {
    
            public static void main(String[] args) {
                SpringApplication.run(H2Application.class, args);
            }
    
            @Bean
            CommandLineRunner init (StudentRepo studentRepo){
                return args -> {
                    List names = Arrays.asList("udara", "sampath");
                    names.forEach(name -> studentRepo.save(new Student(name)));
                };
            }
        }
    

提交回复
热议问题