<!-- for jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<!-- for migrations of table to db -->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
If you are using Flyway for db migration, create a folder db/migrations inside resource folder
Name the migration V[number]__[name of migration].sql
Example : V1__InitTables.sql
Configuring HikariDataSource
@ConfigurationpublicclassDataSouceConfig { @Bean @Primary// to get the required environment variable from configuration @ConfigurationProperties(prefix ="app.datasource.main")publicHikariDataSourcehikariDataSource() {returnDataSourceBuilder.create().type(HikariDataSource.class).build(); } @BeanpublicJdbcTemplatejdbcTemplate(HikariDataSource hikariDataSource) {returnnewJdbcTemplate(hikariDataSource); }}
Queries
// select statementString sql ="SELECT * FROM table;";returnjdbcTemplate.query(sql,newTRowMapper());// insert statementString sql ="""insert into table (col1, col2) values (?, ?);""";returnjdbcTemplate.update(sql, value1, value2);// delete statementreturnjdbcTemplate.update("delete from table where id = ?", id);// with conditionString sql ="SELECT * FROM table where id = ?;";List<T> movies =jdbcTemplate.query(sql,newTRowMapper(), id);returnmovies.stream().findFirst();