摘要
spring cloud config server配置好了数据库连接信息,这个项目读取config,获取连接信息。这里以mybtis作为列子。从服务器读取jdbc信息后,运行mybatis程序。
确认服务是否成功
http://localhost:8888/demo-config/test
{"name":"demo-config","profiles":["test"],"label":"master","version":"02d28ad4925aa9bd1bf8a48d2edbf04ce61aa45a","propertySources":[{"name":"https://git.oschina.net/penghaozhong/demo.git/demo-config-repo/demo-config-test.properties","source":{"jdbc.url":"jdbc:mysql://localhost:3306/demo?characterEncoding=UTF-8","jdbc.username":"root","jdbc.driver":"com.mysql.jdbc.Driver","jdbc.password":"xxxxxx","jdbc.type":"mysql"}}]}
2. 配置读取配置文件
在bootstrap.properties中添加读取配置管理的地址。
3. 读取配置文件属性,这里采用@ConfigurationProperties
/**
* 读取数据库配置文件
* @author penghaozhong
*
*/
@ConfigurationProperties(prefix = DataSourceProperties.PREFIX, ignoreUnknownFields = false)
public class DataSourceProperties {
public DataSourceProperties() {
super();
}
//对应配置文件里的配置键
public final static String PREFIX="jdbc";
private String type;
private String driver;
private String url;
private String username;
private String password;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4. 配置mybatis
@Configuration
@MapperScan("com.phz.test.spring.cloud.demo")
@EnableConfigurationProperties(DataSourceProperties.class)
@EnableTransactionManagement
public class MybatisDataSource {
// mybaits mapper xml搜索路径
private final static String MAPPERLOCATIONS = "classpath:/mappings/**/*.xml";
private final static String CONFIGLOCATION = "classpath:/mybatis-config.xml";
@Autowired
private DataSourceProperties dataSourceProperties;
private DruidDataSource datasource = null;
@Bean(destroyMethod = "close")
public DataSource dataSource(){
datasource = new DruidDataSource();
datasource.setUrl(dataSourceProperties.getUrl());
datasource.setDbType(dataSourceProperties.getType());
datasource.setDriverClassName(dataSourceProperties.getDriver());
datasource.setUsername(dataSourceProperties.getUsername());
datasource.setPassword(dataSourceProperties.getPassword());
return datasource;
}
@PreDestroy
public void close() {
if(datasource != null){
datasource.close();
}
}
@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(MAPPERLOCATIONS));
sqlSessionFactoryBean.setConfigLocation(resolver.getResource(CONFIGLOCATION));
sqlSessionFactoryBean.setTypeAliasesPackage("com.phz.test.spring.cloud.demo.entity");
return sqlSessionFactoryBean.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
}