Spring Boo数据访问JDBC

一、SpringBoot 访问JDBC原理

  我们可以参考源代码:SpringBoot2中默认的数据源是使用HikariDataSource

/**
* Hikari DataSource configuration.
*/
@ConditionalOnClass(HikariDataSource.class)
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.zaxxer.hikari.HikariDataSource", matchIfMissing = true)

  同时可以支持其他类型的数据源: 

org.apache.tomcat.jdbc.pool.DataSource  
"com.zaxxer.hikari.HikariDataSource"  
org.apache.commons.dbcp2.BasicDataSource

  对于其他公司如阿里巴巴的druid数据源,通过查看源码,可以自行在配置文件中配置

/**
* Generic DataSource configuration. 通过数据源配置
*/
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type")
static class Generic { @Bean
public DataSource dataSource(DataSourceProperties properties) {
return properties.initializeDataSourceBuilder().build();
} } 在application.xml|.yml中添加如下配置:

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource

 

  

  1.关于SpringBoot自动建表、执行Sql语句

  通过查看DataSourceInitializer这个类源代码:

  

/**  建表结构
* Create the schema if necessary.
* @return {@code true} if the schema was created
* @see DataSourceProperties#getSchema()
*/
public boolean createSchema() {
List<Resource> scripts = getScripts("spring.datasource.schema",
this.properties.getSchema(), "schema");
if (!scripts.isEmpty()) {
if (!isEnabled()) {
logger.debug("Initialization disabled (not running DDL scripts)");
return false;
}
String username = this.properties.getSchemaUsername();
String password = this.properties.getSchemaPassword();
runScripts(scripts, username, password);
}
return !scripts.isEmpty();
}   查看getScripts()方法
private List<Resource> getScripts(String propertyName, List<String> resources,
String fallback) {
if (resources != null) {
return getResources(propertyName, resources, true);
}
String platform = this.properties.getPlatform();
List<String> fallbackResources = new ArrayList<>();
fallbackResources.add("classpath*:" + fallback + "-" + platform + ".sql");    //.sql文件存放在这路径下
fallbackResources.add("classpath*:" + fallback + ".sql");              //.sql也可以使用者方式存放
return getResources(propertyName, fallbackResources, false);
}
 

  

//执行Sql语句原理
private void runScripts(List<Resource> resources, String username, String password) {
if (resources.isEmpty()) {
return;
}
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());
}
for (Resource resource : resources) {
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);
}

  

默认创建表:  

schema-*.sql、data-*.sql
默认规则:schema.sql,schema-all.sql;
可以使用
schema:
- classpath:指定名称.sql
指定位置

  2.不使用默认的数据源,使用阿里的数据源。

  通过查看源代码可以知道,默认的数据源中是没有druid连接池的,但是默认的连接池都有放在容器中,所以我们可以手写一个配合类,并放在容器中即可,容器会自动加载我们的。

    2.1首先在pom.xml中依赖druid的依赖包

    2.2写一个配置类,可以参照DataSourceConfiguration

      根据以下配置就可以配置好Druid数据库

@Configuration
public class config { @Bean
@ConditionalOnProperty(name = "spring.datasource.type")
public DataSource dataSource(){
return new DruidDataSource();
}

  ·效果:

  Spring Boo数据访问JDBC

  

  

·

上一篇:Mycat 分布式事务的实现


下一篇:利用nginx添加账号密码验证