1.数据访问流程:
2.搭建数据库开发场景:
2.1.导入JDBC场景:
2.2.分析自动导入的内容:
2.3.分析为何没有导入数据库驱动:
- 1.因为人家也不知道我要用啥数据库,所以在自己决定用什么数据库后,就自己引入数据库驱动就可以了
- 2.SpringBoot对引入的数据库有默认的版本库,想要修改版本可以有以下两个方式:
直接依赖引入具体版本(
maven的就近依
赖原则)
xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
重新声明版本(
maven的属性的
就近优先原则
xml
<properties>
<java.version>1.8</java.version>
<mysql.version>5.1.49</mysql.version>
</properties>
3.分析自动配置类:
3.1.数据源的自动配置类:DataSourceAutoConfiguration
-
1.修改数据源相关的配置:
spring.datasource
-
2.数据库连接池的配置是自己容器中
没有DataSource才自动配置
的;底层默认配置好的连接池是:HikariDataSource
java@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
-
3.对
DataSourceConfiguration
点入进去看看:
3.2. 事务管理器的自动配置类:DataSourceTransactionManagerAutoConfiguration:
3.3. JdbcTemplate的自动配置类:JdbcTemplateAutoConfiguration:
可以来对数据库进行crud
-
可以修改这个配置项
@ConfigurationProperties(prefix = "spring.jdbc")
来修改JdbcTemplate -
@Bean
、@Primary
JdbcTemplate容器中有这个组件
3.4. jndi的自动配置类:JndiDataSourceAutoConfiguration
3.5.分布式事务相关的配置类:XA DataSourceAutoConfiguration:
4.修改配置项
yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/db_account
username: root
password: ROOT
driver-class-name: com.mysql.jdbc.Driver
5.做个小测试
6.更改数据源为Druid后产生的自动配置:
-
2.引入场景:
-
3.分析自动配置:
-
1.扩展配置项:spring.datasource.druid
-
2.
DruidSpringAopConfiguration.class
, 监控SpringBean的;配置项:spring.datasource.druid.aop-patterns
-
3.
DruidStatViewServletConfiguration.class
, 监控页的配置:spring.datasource.druid.stat-view-servlet;默认开启
-
4.
DruidWebStatFilterConfiguration.class
, web监控配置;spring.datasource.druid.web-stat-filter;默认开启
-
5.
DruidFilterConfiguration.class})
:所有Druid自己filter的配置:
xmlprivate 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";