第一种:
配置文件:
spring.data.mongodb.ysdm.database=database
spring.data.mongodb.ysdm.url=IP:port
spring.data.mongodb.ysdm.password=password
spring.data.mongodb.ysdm.username=username
spring.data.mongodb.ysdm.authentication-database=admin
抽象类:
@Getter
@Setter
public abstract class AbstractMongoConfig {
private String database;
private String username;
private String password;
private String url;
private String authenticationDatabase;
/*
* Method that creates MongoDbFactory Common to both of the MongoDb
* connections
*/
MongoDbFactory mongoDbFactory() {
String uriStr = String.format("mongodb://%s:%s@%s/%s", username, password, url, authenticationDatabase);
MongoClientURI uri = new MongoClientURI(uriStr);
MongoClient mongoClient = new MongoClient(uri);
return new SimpleMongoDbFactory(mongoClient,database);
}
/*
* Factory method to create the MongoTemplate
*/
abstract MongoTemplate getMongoTemplate();
}
链接mongodb库,指定链接串,如果多个链接,写多个类即可
@Configuration@ConfigurationProperties(prefix = "spring.data.mongodb.ysdm")
public class YsdwMongoConfig extends AbstractMongoConfig {
@Override
@Bean(name = "ysdmMongoTemplate")
public MongoTemplate getMongoTemplate() {
return new MongoTemplate(mongoDbFactory());
}
}
第二种:
配置文件:
mongodb.uri.mongo1=mongodb://name:password@IP:PORT/DATABASE?authSource=admin
初始化,得到实现类
@Configuration
@Getter
@Setter
@ConfigurationProperties(prefix="mongodb.uri")
public class MongoTemplateConfig {
private String mongo1;
@Bean(name = "mongo1MongoTemplate")
public MongoTemplate getMongo1MongoTemplate()
{
return new MongoTemplate(mongoDbFactory(mongo1));
}
private MongoDbFactory mongoDbFactory(String uri) {
return new SimpleMongoClientDbFactory(uri);
}
}
链接oracle配置(如果只有一个数据库不需要这样写,默认配置即可):
配置文件:
yspay.datasource.jdbc-url=jdbc:oracle:thin:@IP:PORT:orcl
yspay.datasource.username=username
yspay.datasource.password=password
yspay.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
yspay.datasource.max-idle=10
yspay.datasource.max-wait=10000
yspay.datasource.min-idle=5
yspay.datasource.initial-size=5
初始化类:
@Configuration
public class MulitDataSourceConfig {
@Bean(name = "yspayDataSource")
@ConfigurationProperties(prefix = "yspay.datasource")
public DataSource getYspayDataSource(){
return DataSourceBuilder.create().build();
}
}
jdbc类:
@Repository
public class JdbcTemplateConfig
{
public @Bean(value = "yspayJdbcTemplate") JdbcTemplate yspayJdbcTemplate(
@Qualifier("yspayDataSource") DataSource dataSource)
{
return new JdbcTemplate(dataSource);
}}