Insert Data using Spring Command line interace (while initialize your application first time)
Step 1:
Create CourseJdbcRepository.java to insert your dummy hardcoded data into DB
@Repository
public class CourseJdbcRepository {
@Autowired
private JdbcTemplate springjdbcTemplate;
//""" will be known as text block
//(this allows us to retain the formatting for your queries which allow to easy to understand)
private static String INSERT_QUERY=
"""
insert into course (id,name,author)
values(1,'Learn Java', 'khanDev')
""";
public void insert() {
springjdbcTemplate.update(INSERT_QUERY);
}
}
Step 2:
CourseJdbcCommandLineRunner.java to call the insert() method while first time load the application
@Component
public class CourseJdbcCommandLineRunner implements CommandLineRunner {
@Autowired
CourseJdbcRepository courseJdbcRepository;
@Override
public void run(String... args) throws Exception {
courseJdbcRepository.insert();
}
}
Comments
Post a Comment