赞
踩
目录
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jdbc</artifactId>
- </dependency>
数据库驱动?
为什么导入JDBC场景,官方不导入驱动?官方不知道我们接下要操作什么数据库。
注意:数据库版本要和驱动版本对应
- 默认版本:<mysql.version>8.0.22</mysql.version>
-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <!-- <version>5.1.49</version>-->
- </dependency>
- 想要修改版本
- 1、直接依赖引入具体版本(maven的就近依赖原则)
- 2、重新声明版本(maven的属性的就近优先原则)
- <properties>
- <java.version>1.8</java.version>
- <mysql.version>5.1.49</mysql.version>
- </properties>
1、自动配置的类
- @Configuration(proxyBeanMethods = false)
- @Conditional(PooledDataSourceCondition.class)
- @ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
- @Import({ DataSourceConfiguration.Hikari.class, DataSourceConfiguration.Tomcat.class,
- DataSourceConfiguration.Dbcp2.class, DataSourceConfiguration.OracleUcp.class,
- DataSourceConfiguration.Generic.class, DataSourceJmxConfiguration.class })
- protected static class PooledDataSourceConfiguration
- spring:
- datasource:
- url: jdbc:mysql://localhost:3306/db_account
- username: root
- password: 123456
- driver-class-name: com.mysql.jdbc.Driver
- @Slf4j
- @SpringBootTest
- class Boot05WebAdminApplicationTests {
-
- @Autowired
- JdbcTemplate jdbcTemplate;
-
-
- @Test
- void contextLoads() {
-
- Long aLong = jdbcTemplate.queryForObject("select count(*) from account_tbl", Long.class);
- log.info("记录总数:{}",aLong);
- }
-
- }
整合第三方技术的两种方式
1、创建数据源
- 导入druid的依赖
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.1.17</version>
- </dependency>
-
2、StatViewServlet
StatViewServlet的用途包括:
- <servlet>
- <servlet-name>DruidStatView</servlet-name>
- <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>DruidStatView</servlet-name>
- <url-pattern>/druid/*</url-pattern>
- </servlet-mapping>
3、StatFilter
用于统计监控信息;如SQL监控、URI监控
- 需要给数据源中配置如下属性;可以允许多个filter,多个用,分割;如:
-
- <property name="filters" value="stat,slf4j" />
系统中所有filter:
别名 | Filter类名 |
default | com.alibaba.druid.filter.stat.StatFilter |
stat | com.alibaba.druid.filter.stat.StatFilter |
mergeStat | com.alibaba.druid.filter.stat.MergeStatFilter |
encoding | com.alibaba.druid.filter.encoding.EncodingConvertFilter |
log4j | com.alibaba.druid.filter.logging.Log4jFilter |
log4j2 | com.alibaba.druid.filter.logging.Log4j2Filter |
slf4j | com.alibaba.druid.filter.logging.Slf4jLogFilter |
commonlogging | com.alibaba.druid.filter.logging.CommonsLogFilter |
慢SQL记录配置
- <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
- <property name="slowSqlMillis" value="10000" />
- <property name="logSlowSql" value="true" />
- </bean>
-
- 使用 slowSqlMillis 定义慢SQL的时长
上面的配置也可以通过配置类直接编码实现
-
- @Configuration
- public class MyDatasourceConfig {
- @ConfigurationProperties("spring.datasource")
- @Bean
- public DataSource dataSource() throws SQLException {
- DruidDataSource druidDataSource = new DruidDataSource();
- //加入监控功能
- druidDataSource.setFilters("stat,wall");
- return druidDataSource;
- }
-
-
- /*
- * 配置Druid的监控页功能
- */
- @Bean
- public ServletRegistrationBean statViewServlet() {
- StatViewServlet statViewServlet = new StatViewServlet();
- ServletRegistrationBean<StatViewServlet> servletServletRegistrationBean = new ServletRegistrationBean<StatViewServlet>(statViewServlet,"/druid/*");
- servletServletRegistrationBean.addInitParameter("loginUsername","admin");
- servletServletRegistrationBean.addInitParameter("loginPassword","123456");
- return servletServletRegistrationBean;
- }
-
- /*
- * webStatFilter 用于采集web-jdbc关联监控的数据
- */
- @Bean
- public FilterRegistrationBean webStatFilter() {
- WebStatFilter webStatFilter = new WebStatFilter();
- FilterRegistrationBean<WebStatFilter> webStatFilterFilterRegistrationBean = new FilterRegistrationBean<>(webStatFilter);
- webStatFilterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
- webStatFilterFilterRegistrationBean.addInitParameter("exclusions","*.js,*.css,/druid/*");
- return webStatFilterFilterRegistrationBean;
- }
- }
1、引入druid-starter
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- <version>1.1.17</version>
- </dependency>
2、分析自动配置
- private static final String FILTER_STAT_PREFIX = "spring.datasource.druid.filter.stat";
- private static final String FILTER_CONFIG_PREFIX = "spring.datasource.druid.filter.config";
- private static final String FILTER_ENCODING_PREFIX = "spring.datasource.druid.filter.encoding";
- private static final String FILTER_SLF4J_PREFIX = "spring.datasource.druid.filter.slf4j";
- private static final String FILTER_LOG4J_PREFIX = "spring.datasource.druid.filter.log4j";
- private static final String FILTER_LOG4J2_PREFIX = "spring.datasource.druid.filter.log4j2";
- private static final String FILTER_COMMONS_LOG_PREFIX = "spring.datasource.druid.filter.commons-log";
- private static final String FILTER_WALL_PREFIX = "spring.datasource.druid.filter.wall";
3、配置示例
spring: datasource: url: jdbc:mysql://localhost:3306/db_account username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver druid: aop-patterns: com.atguigu.admin.* #监控SpringBean filters: stat,wall # 底层开启功能,stat(sql监控),wall(防火墙) stat-view-servlet: # 配置监控页功能 enabled: true login-username: admin login-password: admin resetEnable: false web-stat-filter: # 监控web enabled: true urlPattern: /* exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*' filter: stat: # 对上面filters里面的stat的详细配置 slow-sql-millis: 1000 logSlowSql: true enabled: true wall: enabled: true config: drop-table-allow: false
SpringBoot配置示例
https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。