20、Springboot 与数据访问(JDBC/自动配置)
简介:
对于数据访问层,无论是SQL还是NOSQL,Spring Boot默认采用整合
Spring Data的方式进行统一处理,添加大量自动配置,屏蔽了很多设置。引入
各种xxxTemplate,xxxRepository来简化我们对数据访问层的操作。对我们来
说只需要进行简单的设置即可。我们将在数据访问章节测试使用SQL相关、
NOSQL在缓存、消息、检索等章节测
整合最基本的JDBC数据源:
pom.xml
引入web、jdbc、mysql
切记吧mysql的《scope》属性去掉
连接jdbc
会自动装配到容器中
spring: datasource: username: root password: 1234 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/users?serverTimezone=GMT
测试:
默认使用:
数据的相关配置都在: 类中
1.x默认使用的是
数据源的自动配置:
1)、
里面都是数据源,根据诶室创建使用数据源,有默认的使用
@ConditionalOnClass({HikariDataSource.class}) @ConditionalOnMissingBean({DataSource.class}) @ConditionalOnProperty( name = {"spring.datasource.type"}, havingValue = "com.zaxxer.hikari.HikariDataSource", matchIfMissing = true ) static class Hikari { Hikari() { } @Bean @ConfigurationProperties( prefix = "spring.datasource.hikari" ) public HikariDataSource dataSource(DataSourceProperties properties) { HikariDataSource dataSource = (HikariDataSource)DataSourceConfiguration.createDataSource(properties, HikariDataSource.class); if (StringUtils.hasText(properties.getName())) { dataSource.setPoolName(properties.getName()); } return dataSource; } }
可以使用
spring.datasource.type指定自定义的数据源类型;
2)、默认支持一下数据源:
@ConditionalOnMissingBean({DataSource.class}) @ConditionalOnProperty( name = {"spring.datasource.type"} ) static class Generic { Generic() { } @Bean public DataSource dataSource(DataSourceProperties properties) { //使用DataSourceBuilder创建数据源,利用反射创建响应type的数据源,并且绑定相关属性 return properties.initializeDataSourceBuilder().build(); } }
4)、
private List<Resource> getScripts(String propertyName, List<String> resources, String fallback) { if (resources != null) { return this.getResources(propertyName, resources, true); } else { String platform = this.properties.getPlatform(); List<String> fallbackResources = new ArrayList(); fallbackResources.add("classpath*:" + fallback + "-" + platform + ".sql"); fallbackResources.add("classpath*:" + fallback + ".sql"); return this.getResources(propertyName, fallbackResources, false); } }
platform:
private void runScripts(List<Resource> resources, String username, String password) { if (!resources.isEmpty()) { 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()); } Iterator var5 = resources.iterator(); while(var5.hasNext()) { Resource resource = (Resource)var5.next(); 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); } }
可以执行建表或者插入数据的.sql文件
配置执行 schema.sql 脚本
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/users?serverTimezone=GMT spring.datasource.username=root spring.datasource.password=1234 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.schema=classpath:/dept.sql spring.datasource.initialization-mode=always
5)、
@Configuration @ConditionalOnClass({DataSource.class, JdbcTemplate.class}) @ConditionalOnSingleCandidate(DataSource.class) @AutoConfigureAfter({DataSourceAutoConfiguration.class}) @EnableConfigurationProperties({JdbcProperties.class}) public class JdbcTemplateAutoConfiguration { public JdbcTemplateAutoConfiguration() { } @Configuration @Import({JdbcTemplateAutoConfiguration.JdbcTemplateConfiguration.class}) static class NamedParameterJdbcTemplateConfiguration { NamedParameterJdbcTemplateConfiguration() { } @Bean @Primary @ConditionalOnSingleCandidate(JdbcTemplate.class) @ConditionalOnMissingBean({NamedParameterJdbcOperations.class}) public NamedParameterJdbcTemplate namedParameterJdbcTemplate(JdbcTemplate jdbcTemplate) { return new NamedParameterJdbcTemplate(jdbcTemplate); } } @Configuration static class JdbcTemplateConfiguration { private final DataSource dataSource; private final JdbcProperties properties; JdbcTemplateConfiguration(DataSource dataSource, JdbcProperties properties) { this.dataSource = dataSource; this.properties = properties; } @Bean @Primary @ConditionalOnMissingBean({JdbcOperations.class}) public JdbcTemplate jdbcTemplate() { JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource); Template template = this.properties.getTemplate(); jdbcTemplate.setFetchSize(template.getFetchSize()); jdbcTemplate.setMaxRows(template.getMaxRows()); if (template.getQueryTimeout() != null) { jdbcTemplate.setQueryTimeout((int)template.getQueryTimeout().getSeconds()); } return jdbcTemplate; } } }