How to User Path variable @ PathVariable is used for add the variable in the path of your URL and use into the code @RestController @RequestMapping (path = "/by" ) public class ByWorldController { @GetMapping (path = "/hello-world/path-variable/{name}/{gender}" ) public HelloWorldBean helloWorldPathVariable( @PathVariable String name , @PathVariable String gender ) { if ( gender . equalsIgnoreCase ( "m" )) { return new HelloWorldBean(String. format ( "Hello Mr %s" , name )); } else if ( gender . equalsIgnoreCase ( "f" )) { return new HelloWorldBean(String. format ( "Hello Mrs %s" , name )); } ...
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