1、application.properties中进行数据库配置
spring.datasource.single.url=xxxx?useUnicode=true&characterEncoding=utf-8
spring.datasource.single.username=xxx
spring.datasource.single.password=xxx
spring.datasource.single.type=com.alibaba.druid.pool.DruidDataSource
2、JdbcTemplateConfig配置
@Configuration
public class JdbcTemplateConfig {
@Bean
@ConfigurationProperties(prefix = “spring.datasource.single”)
DataSource dataSourceSingle() {
return DruidDataSourceBuilder.create().build();
}
@Bean
JdbcTemplate jdbcSingle(@Qualifier(“dataSourceSingle”) DataSource dsSingle) {
return new JdbcTemplate(dsSingle);
}
}
3、初始化
@Autowired
@Qualifier(“jdbcSingle”)
public JdbcTemplate jdbcSingle;
4、使用
JdbcTemplate类中提供了官方的增删改查。
query
queryForMap
queryForObject
queryForList
queryForRowSet
update(包含新增、更新和删除)