我在我的应用程序中使用带有mybatis和mysql数据库的spring boot,我想知道如何使用@configuration注释在我的应用程序中配置数据库。
这是我的示例配置文件。
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@Configuration
public class DatabaseConfig {
@Value("${spring.datasource.driver-class-name}")
private String DB_DRIVER;
@Value("${spring.datasource.username}")
private String DB_USERNAME;
@Value("${spring.datasource.password}")
private String DB_PASSWORD;
@Value("${spring.datasource.url}")
private String DB_URL;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = null;
try {
dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(DB_DRIVER);
dataSource.setUrl(DB_URL);
dataSource.setUsername(DB_USERNAME);
dataSource.setPassword(DB_PASSWORD);
} catch (Exception e) {
e.getMessage();
}
return dataSource;
}
}