1.引入jar包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
</dependencies>
2.配置类
@Configuration
public class DataSourceConfig {
@Resource
private PropertiesConfig propertiesConfig;
@Bean("jdbcTemplate")
public NamedParameterJdbcTemplate dsJdbcTemplate(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl(propertiesConfig.getDs().getUrl());
druidDataSource.setUsername(propertiesConfig.getDs().getUsername());
druidDataSource.setPassword(propertiesConfig.getDs().getPassword());
return new NamedParameterJdbcTemplate(druidDataSource);
}
}
3.使用
@Resource
private NamedParameterJdbcTemplate jdbcTemplate;