Help with Spring jdbcTemplate
I created a Class:
package com.market.config;
import javax.activation.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
@Configuration
public class BdConfig {
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
And I have doubts how to make a method to get this dataSource configured in my application.properties:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/market
spring.datasource.username=root
spring.datasource.password=
0
Author: Joao Spirit, 2019-07-31
1 answers
Oi,
You need to configure the datasource before
So
@Configuration
public class BdConfig {
@Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("oracle.jdbc.driver.OracleDriver");
dataSourceBuilder.url("jdbc://blablabla");
dataSourceBuilder.username("usuario");
dataSourceBuilder.password("senha");
return dataSourceBuilder.build();
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
Anyway depending on the version of spring data you are using you don't need to configure JdbcTemplate manually, just inject it into your repository or service using autowired like this
@Repository
public class MeuRepositorio {
@Autowired
JdbcTemplate jdbcTemplate;
0
Author: André, 2019-07-31 21:36:07