一开始整合JdbcTemplate的时候,报错Failed to determine a suitable driver class
这是因为spring-boot-starter-jdbc根据自动配置约束自己去配置数据源,而项目中并没有dataSource
相关的配置,所以出错。
然后需要首先在启动类中排除数据源自动配置
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
然后加入dataSource的配置类
@Configuration
public class DataSourceConfig{
@Autowired
private Environment env;
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(env.getProperty("spring.datasourse.url"));
dataSource.setUsername(env.getProperty("spring.datasourse.username"));
dataSource.setPassword(env.getProperty("spring.datasourse.password"));
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
return dataSource;
}
}
至此,问题解决