SpringBoot 之數據訪問
阿新 • • 發佈:2018-06-11
exc jdb In driver ash prefix connect bili url
1. Spring Boot 與 JDBC
- 默認使用
org.apache.tomcat.jdbc.pool.DataSource
數據源;
// application.yml spring: datasource: username: root password: root url: jdbc:mysql:///jpa driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource # 用於指定數據源 # 數據源其他配置 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true # 配置監控統計攔截的 filters, 'wall' 用於防火墻 filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 // 配置Druid的屬性,DruidConfig.java @Configuration public class DruidConfig{ @ConfigurationProperties(prefix="spring.datasource") @Bean public DataSource druid(){ return new DruidDataSource(); } // 配置Druid的監控 // 1. 配置一個管理後臺的Servlet @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); Map<String, String> initParams = new HashMap<>(); initParams.put("loginUsername", "admin"); // Druid 後臺登錄名 initParams.put("loginPassword", "root"); initParams.put("allow", ""); // 默認就是允許所有訪問 return bean; } // 2. 配置一個Web監控的filter @Bean public FilterRegistrationBean webStatFilter(){ FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); Map<String, String> initParams = new HashMap<>(); initParams.put("exclusions", "*.js,*.css,/druid/*"); // 不攔截哪些請求 bean.setInitParameters(initParams); bean.setUrlPatterns(Arrays.asList("/*")); // 攔截所有請求 return bean; } }
參考資料:
- SpringBoot 數據訪問
SpringBoot 之數據訪問