你已經發現,可以從application.properties
配置此你可以找到所有可能的屬性here。
請注意,從Spring Boot 1.4開始,每個數據源供應商都有數據源屬性,這些數據源供應商是開箱即用的。有spring.datasource.dbcp.*
,spring.datasource.tomcat.*
等。請參閱1.4 docs
如果這還不夠,並且您需要非常具體的東西,則可以自己聲明數據源bean。下面是Tomcat數據源的示例:
@Bean
public DataSource dataSource(){
PoolProperties p = new PoolProperties();
p.setUrl("jdbc:mysql://localhost:3306/mysql");
p.setDriverClassName("com.mysql.jdbc.Driver");
p.setUsername("root");
p.setPassword("password");
p.setJmxEnabled(true);
p.setTestWhileIdle(false);
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(100);
p.setInitialSize(10);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(10);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
p.setJdbcInterceptors(
"org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
"org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
DataSource datasource = new DataSource();
datasource.setPoolProperties(p);
return datasource ;
}
問題是我使用jpa存儲庫名稱約定來讀取/寫入數據庫。我怎樣才能得到sqlconnection實例? –