JdbcTemplate and Config datasource

PHOTO EMBED

Fri Jan 26 2024 06:44:32 GMT+0000 (Coordinated Universal Time)

Saved by @KhanhDu #springframework

// an application that accesses a relational database with JDBC, you’ve probably configured Spring’s JdbcTemplate as a bean in the Spring application context
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
  return new JdbcTemplate(dataSource);
}

// configure a DataSource bean so that the dependency will be met
@Bean
public DataSource dataSource() {
  return new EmbeddedDatabaseBuilder()
          .setType(EmbeddedDatabaseType.H2)
          .addScripts('schema.sql', 'data.sql')
          .build();
}
content_copyCOPY