本文整理汇总了Java中org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder类的典型用法代码示例。如果您正苦于以下问题:Java DataSourceBuilder类的具体用法?Java DataSourceBuilder怎么用?Java DataSourceBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DataSourceBuilder类属于org.springframework.boot.autoconfigure.jdbc包,在下文中一共展示了DataSourceBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: dataSource
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
@Primary
@Bean(destroyMethod = "close")
public DataSource dataSource() {
final EmbeddedMysql embeddedMysql = embeddedMysql(); // make sure embeddedMySql is started.
Map<String, String> params = ImmutableMap.<String, String>builder()
.put("profileSQL", String.valueOf(false))
.put("generateSimpleParameterMetadata", String.valueOf(true))
.build();
final String url = String.format("jdbc:mysql://localhost:%d/%s?%s",
embeddedMysql.getConfig().getPort(),
SCHEMA_NAME,
Joiner.on("&").withKeyValueSeparator("=").join(params));
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.username(embeddedMysql.getConfig().getUsername());
dataSourceBuilder.password(embeddedMysql.getConfig().getPassword());
dataSourceBuilder.driverClassName(com.mysql.jdbc.Driver.class.getName());
dataSourceBuilder.url(url);
return dataSourceBuilder.build();
}
示例2: postgresJdbcTemplate
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
@Bean
public JdbcTemplate postgresJdbcTemplate(
@Value("${postgres.jdbcUrl}") String postgresJdbcUrl,
@Value("${postgres.jdbcDriver}") String postgresJdbcDriver,
@Value("${postgres.jdbcUser}") String postgresJdbcUser,
@Value("${postgres.jdbcPassword}") String postgresJdbcPassword) {
DataSource targetDataSource = DataSourceBuilder
.create()
.driverClassName(postgresJdbcDriver)
.url(postgresJdbcUrl)
.username(postgresJdbcUser)
.password(postgresJdbcPassword)
.build();
return new JdbcTemplate(targetDataSource);
}
示例3: getFirstFunctionalInstance
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
/**
*
* @return экземпляр JDBCTemplate Для работы с тестовой базой данных
*/
public Database getFirstFunctionalInstance(){
if (firstFunctionalInstance == null) {
LOG.debug("Инициируем новый инстанс базы данных");
DataSource ds = DataSourceBuilder
.create()
.username(databaseUser)
.password(databasePassword)
.url("jdbc:sqlserver://192.168.20.28:49484;instance=maindb")
.driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
.build();
this.firstFunctionalInstance = new Database(ds);
return firstFunctionalInstance;
}
else {
LOG.debug("Инстанс базы данных уже инициирован, возвращаем существующий");
return firstFunctionalInstance;}
}
示例4: getLoadInstance
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
/**
*
* @return экземпляр JDBCTemplate Для работы с базой данных нагрузочного стенда
*/
public Database getLoadInstance(){
if (loadInstance == null) {
LOG.debug("Инициируем новый инстанс базы данных");
DataSource ds = DataSourceBuilder
.create()
.username(databaseUser)
.password(databasePassword)
.url("jdbc:sqlserver://192.168.21.9;instance=maindb")
.driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
.build();
this.loadInstance = new Database(ds);
return loadInstance;
}
else {
LOG.debug("Инстанс базы данных уже инициирован, возвращаем существующий");
return loadInstance;}
}
示例5: runScripts
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
private void runScripts(List<Resource> resources, String username, String password) {
if (resources.isEmpty()) {
return;
}
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.setContinueOnError(this.properties.isContinueOnError());
populator.setSeparator(this.properties.getSeparator());
if (this.properties.getSqlScriptEncoding() != null) {
populator.setSqlScriptEncoding(this.properties.getSqlScriptEncoding().name());
}
for (Resource resource : resources) {
populator.addScript(resource);
}
DataSource dataSource = this.dataSource;
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
dataSource = DataSourceBuilder.create(this.properties.getClassLoader())
.driverClassName(this.properties.determineDriverClassName())
.url(this.properties.determineUrl()).username(username)
.password(password).build();
}
DatabasePopulatorUtils.execute(populator, dataSource);
}
示例6: readonlyDataSource
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
@Bean
@ConfigurationProperties(prefix="spring.datasource.readonly")
public DataSource readonlyDataSource() throws SQLException {
DruidDataSource dataSource = (DruidDataSource)DataSourceBuilder
.create()
.type(DruidDataSource.class)
.build();
// dataSource.setFilters("wall");
/*
WallFilter wallFilter = new WallFilter();
wallFilter.setLogViolation(true);
wallFilter.setThrowException(false);
dataSource.getProxyFilters().add(wallFilter);
*/
return dataSource;
}
示例7: dataSource
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
@Bean(destroyMethod = "close")
@ConditionalOnExpression("#{!environment.acceptsProfiles('" + Constants.SPRING_PROFILE_CLOUD + "') && !environment.acceptsProfiles('" + Constants.SPRING_PROFILE_HEROKU + "')}")
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public DataSource dataSource(DataSourceProperties dataSourceProperties) {
log.debug("Configuring Datasource");
if (dataSourceProperties.getUrl() == null) {
log.error("Your database connection pool configuration is incorrect! The application" +
" cannot start. Please check your Spring profile, current profiles are: {}",
Arrays.toString(env.getActiveProfiles()));
throw new ApplicationContextException("Database connection pool is not configured correctly");
}
HikariDataSource hikariDataSource = (HikariDataSource) DataSourceBuilder
.create(dataSourceProperties.getClassLoader())
.type(HikariDataSource.class)
.driverClassName(dataSourceProperties.getDriverClassName())
.url(dataSourceProperties.getUrl())
.username(dataSourceProperties.getUsername())
.password(dataSourceProperties.getPassword())
.build();
if (metricRegistry != null) {
hikariDataSource.setMetricRegistry(metricRegistry);
}
return hikariDataSource;
}
示例8: configurationDataSource
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
@Bean
@org.springframework.boot.context.properties.ConfigurationProperties(prefix = "c2mon.server.configuration.jdbc")
public DataSource configurationDataSource() {
String url = properties.getJdbc().getUrl();
String username = properties.getJdbc().getUsername();
String password = properties.getJdbc().getPassword();
// A simple inspection is done on the JDBC URL to deduce whether to create an in-memory
// in-process database, start a file-based externally visible database or connect to
// an external database.
if (url.contains("hsql")) {
return new HsqlDatabaseBuilder()
.url(url)
.username(username)
.password(password)
.addScript(new ClassPathResource("sql/config-schema-hsqldb.sql")).build();
} else {
return DataSourceBuilder.create().build();
}
}
示例9: historyDataSource
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
@Bean
@ConfigurationProperties("c2mon.server.history.jdbc")
public DataSource historyDataSource() {
String url = properties.getJdbc().getUrl();
String username = properties.getJdbc().getUsername();
String password = properties.getJdbc().getPassword();
if (url.contains("hsql")) {
return new HsqlDatabaseBuilder()
.url(url)
.username(username)
.password(password)
.addScript(new ClassPathResource("sql/history-schema-hsqldb.sql")).build();
} else {
return DataSourceBuilder.create().build();
}
}
示例10: build
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
public DataSource build() {
DataSource dataSource;
if (url == null || url.contains("hsqldb:mem")) {
// Start an in-process, in-memory HSQL server
dataSource = new EmbeddedDatabaseBuilder().setType(HSQL).setName("c2mondb").build();
} else if (url.contains("hsql://")) {
// Start an externally visible, file-based HSQL server
HsqlServer.start("file:///tmp/c2mondb", "c2mondb");
url += ";sql.syntax_ora=true;hsqldb.default_table_type=cached;hsqldb.cache_rows=1000;hsqldb.result_max_memory_rows=2000;hsqldb.cache_size=100";
dataSource = DataSourceBuilder.create().url(url).username(username).password(password).build();
} else {
throw new RuntimeException("The given URL was not a valid HSQL JDBC URL!");
}
if (!scripts.isEmpty()) {
ResourceDatabasePopulator populator = new ResourceDatabasePopulator(scripts.toArray(new Resource[scripts.size()]));
populator.setContinueOnError(true);
DatabasePopulatorUtils.execute(populator, dataSource);
}
return dataSource;
}
示例11: cacheDataSource
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
@Bean
@ConfigurationProperties(prefix = "c2mon.server.cachedbaccess.jdbc")
public DataSource cacheDataSource() {
String url = properties.getJdbc().getUrl();
String username = properties.getJdbc().getUsername();
String password = properties.getJdbc().getPassword();
// A simple inspection is done on the JDBC URL to deduce whether to create an in-memory
// in-process database, start a file-based externally visible database or connect to
// an external database.
if (url.contains("hsql")) {
return new HsqlDatabaseBuilder()
.url(url)
.username(username)
.password(password)
.addScript(new ClassPathResource("sql/cache-schema-hsqldb.sql")).build();
} else {
return DataSourceBuilder.create().build();
}
}
示例12: buildDataSource
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
/**
* 创建DataSource
*/
@SuppressWarnings("unchecked")
public DataSource buildDataSource(Map<String, Object> dsMap) {
try {
Object type = dsMap.get("type");
if (type == null)
type = DATASOURCE_TYPE_DEFAULT;// 默认DataSource
Class<? extends DataSource> dataSourceType;
dataSourceType = (Class<? extends DataSource>) Class.forName((String) type);
String driverClassName = dsMap.get("driverClassName").toString();
String url = dsMap.get("url").toString();
String username = dsMap.get("username").toString();
String password = dsMap.get("password").toString();
DataSourceBuilder factory = DataSourceBuilder.create().driverClassName(driverClassName).url(url)
.username(username).password(password).type(dataSourceType);
return factory.build();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
示例13: buildDataSource
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
/**
* 创建DataSource
*
* @param type
* @param driverClassName
* @param url
* @param username
* @param password
* @return
* @author SHANHY
* @create 2016年1月24日
*/
@SuppressWarnings("unchecked")
public DataSource buildDataSource(Map<String, Object> dsMap) {
try {
Object type = dsMap.get("type");
if (type == null)
type = DATASOURCE_TYPE_DEFAULT;// 默认DataSource
Class<? extends DataSource> dataSourceType;
dataSourceType = (Class<? extends DataSource>) Class.forName((String) type);
String driverClassName = dsMap.get("driver-class-name").toString();
String url = dsMap.get("url").toString();
String username = dsMap.get("username").toString();
String password = dsMap.get("password").toString();
DataSourceBuilder factory = DataSourceBuilder.create().driverClassName(driverClassName).url(url)
.username(username).password(password).type(dataSourceType);
return factory.build();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
示例14: dataSourceProd
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
@Bean
public DataSource dataSourceProd() {
String databaseUrl = env.getProperty("database.url");
String databaseUsername = env.getProperty("database.username");
String databasePassword = env.getProperty("database.password");
if(databaseUrl == null || databaseUsername == null || databasePassword == null) {
logger.log(Level.ALL, "Using local H2 database");
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
}
logger.log(Level.ALL, "Using database " + databaseUrl);
return DataSourceBuilder.create()
.url(databaseUrl)
.username(databaseUsername)
.password(databasePassword)
.build();
}
示例15: defaultDataSource
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; //导入依赖的package包/类
@Bean
@Profile("default")
public DataSource defaultDataSource() {
String jdbcUrl = dataSourceUrl;
if (jdbcUrl.isEmpty()) {
jdbcUrl = "jdbc:hsqldb:file:" + getDatabaseDirectory() +
";create=true;hsqldb.tx=mvcc;hsqldb.applog=1;hsqldb.sqllog=0;hsqldb.write_delay=false";
}
return DataSourceBuilder.create()
.username(dataSourceUsername)
.password(dataSourcePassword)
.url(jdbcUrl)
.driverClassName(dataSourceDriverClassName)
.build();
}