springboot与jdbc操作

一、查看操作使用的数据源

首先搭建工程向导
1、使用快捷方式搭建后插入的xml文件:
在这里插入图片描述
2、在linux远程工具连接上,创建一个jdbc数据库
在这里插入图片描述
3、在全局配置application.yml,配置参数:
在这里插入图片描述

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://192.168.219.5:3306/jdbc
    driver-class-name: com.mysql.cj.jdbc.Driver

4、在src的test目录下:

SpringbootDataJdbcApplicationTests.java

在这里插入图片描述

package com.study.springboot;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@SpringBootTest
class SpringbootDataJdbcApplicationTests {
    @Autowired
    DataSource dataSource;

    
    @Test
    void contextLoads() throws SQLException {
       //Spring Boot 2.0.4 默认使用 com.zaxxer.hikari.HikariDataSource 数据源
        // 而以前版本,
        //如 Spring Boot 1.5 默认使用 org.apache.tomcat.jdbc.pool.DataSource 作为数据源;org.apache.tomcat.jdbc.pool.DataSource
        System.out.println(dataSource.getClass());
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }
}

5、运行SpringbootDataJdbcApplicationTests类,单元测试,控制台打印出来的数据源信息

D:\java\jdk\JDK8_64\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:D:\java\idea201902\config-pathclass\lib\idea_rt.jar=56323:D:\java\idea201902\config-pathclass\bin -Dfile.encoding=UTF-8 -classpath D:\java\idea201902\config-pathclass\lib\idea_rt.jar;D:\java\idea201902\config-pathclass\plugins\junit\lib\junit-rt.jar;D:\java\idea201902\config-pathclass\plugins\junit\lib\junit5-rt.jar;C:\Users\HP\.m2\repository\org\junit\platform\junit-platform-launcher\1.5.2\junit-platform-launcher-1.5.2.jar;C:\Users\HP\.m2\repository\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;C:\Users\HP\.m2\repository\org\junit\platform\junit-platform-engine\1.5.2\junit-platform-engine-1.5.2.jar;C:\Users\HP\.m2\repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;C:\Users\HP\.m2\repository\org\junit\platform\junit-platform-commons\1.5.2\junit-platform-commons-1.5.2.jar;D:\java\jdk\JDK8_64\jre\lib\charsets.jar;D:\java\jdk\JDK8_64\jre\lib\deploy.jar;D:\java\jdk\JDK8_64\jre\lib\ext\access-bridge-64.jar;D:\java\jdk\JDK8_64\jre\lib\ext\cldrdata.jar;D:\java\jdk\JDK8_64\jre\lib\ext\dnsns.jar;D:\java\jdk\JDK8_64\jre\lib\ext\jaccess.jar;D:\java\jdk\JDK8_64\jre\lib\ext\jfxrt.jar;D:\java\jdk\JDK8_64\jre\lib\ext\localedata.jar;D:\java\jdk\JDK8_64\jre\lib\ext\nashorn.jar;D:\java\jdk\JDK8_64\jre\lib\ext\sunec.jar;D:\java\jdk\JDK8_64\jre\lib\ext\sunjce_provider.jar;D:\java\jdk\JDK8_64\jre\lib\ext\sunmscapi.jar;D:\java\jdk\JDK8_64\jre\lib\ext\sunpkcs11.jar;D:\java\jdk\JDK8_64\jre\lib\ext\zipfs.jar;D:\java\jdk\JDK8_64\jre\lib\javaws.jar;D:\java\jdk\JDK8_64\jre\lib\jce.jar;D:\java\jdk\JDK8_64\jre\lib\jfr.jar;D:\java\jdk\JDK8_64\jre\lib\jfxswt.jar;D:\java\jdk\JDK8_64\jre\lib\jsse.jar;D:\java\jdk\JDK8_64\jre\lib\management-agent.jar;D:\java\jdk\JDK8_64\jre\lib\plugin.jar;D:\java\jdk\JDK8_64\jre\lib\resources.jar;D:\java\jdk\JDK8_64\jre\lib\rt.jar;D:\java\springboot-data-jdbc\target\test-classes;D:\java\springboot-data-jdbc\target\classes;C:\Users\HP\.m2\repository\org\springframework\boot\spring-boot-starter-data-jdbc\2.2.0.RELEASE\spring-boot-starter-data-jdbc-2.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\boot\spring-boot-starter-jdbc\2.2.0.RELEASE\spring-boot-starter-jdbc-2.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\com\zaxxer\HikariCP\3.4.1\HikariCP-3.4.1.jar;C:\Users\HP\.m2\repository\org\springframework\spring-jdbc\5.2.0.RELEASE\spring-jdbc-5.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\data\spring-data-relational\1.1.0.RELEASE\spring-data-relational-1.1.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\data\spring-data-commons\2.2.0.RELEASE\spring-data-commons-2.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\spring-tx\5.2.0.RELEASE\spring-tx-5.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\spring-context\5.2.0.RELEASE\spring-context-5.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\spring-beans\5.2.0.RELEASE\spring-beans-5.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\slf4j\slf4j-api\1.7.28\slf4j-api-1.7.28.jar;C:\Users\HP\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.2.0.RELEASE\spring-boot-starter-web-2.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\boot\spring-boot-starter\2.2.0.RELEASE\spring-boot-starter-2.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\boot\spring-boot\2.2.0.RELEASE\spring-boot-2.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.2.0.RELEASE\spring-boot-autoconfigure-2.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.2.0.RELEASE\spring-boot-starter-logging-2.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\HP\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\HP\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.12.1\log4j-to-slf4j-2.12.1.jar;C:\Users\HP\.m2\repository\org\apache\logging\log4j\log4j-api\2.12.1\log4j-api-2.12.1.jar;C:\Users\HP\.m2\repository\org\slf4j\jul-to-slf4j\1.7.28\jul-to-slf4j-1.7.28.jar;C:\Users\HP\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\HP\.m2\repository\org\yaml\snakeyaml\1.25\snakeyaml-1.25.jar;C:\Users\HP\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.2.0.RELEASE\spring-boot-starter-json-2.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.10.0\jackson-databind-2.10.0.jar;C:\Users\HP\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.10.0\jackson-annotations-2.10.0.jar;C:\Users\HP\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.10.0\jackson-core-2.10.0.jar;C:\Users\HP\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.10.0\jackson-datatype-jdk8-2.10.0.jar;C:\Users\HP\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.10.0\jackson-datatype-jsr310-2.10.0.jar;C:\Users\HP\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.10.0\jackson-module-parameter-names-2.10.0.jar;C:\Users\HP\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.2.0.RELEASE\spring-boot-starter-tomcat-2.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.27\tomcat-embed-core-9.0.27.jar;C:\Users\HP\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.27\tomcat-embed-el-9.0.27.jar;C:\Users\HP\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.27\tomcat-embed-websocket-9.0.27.jar;C:\Users\HP\.m2\repository\org\springframework\boot\spring-boot-starter-validation\2.2.0.RELEASE\spring-boot-starter-validation-2.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\jakarta\validation\jakarta.validation-api\2.0.1\jakarta.validation-api-2.0.1.jar;C:\Users\HP\.m2\repository\org\hibernate\validator\hibernate-validator\6.0.17.Final\hibernate-validator-6.0.17.Final.jar;C:\Users\HP\.m2\repository\org\jboss\logging\jboss-logging\3.4.1.Final\jboss-logging-3.4.1.Final.jar;C:\Users\HP\.m2\repository\com\fasterxml\classmate\1.5.0\classmate-1.5.0.jar;C:\Users\HP\.m2\repository\org\springframework\spring-web\5.2.0.RELEASE\spring-web-5.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\spring-webmvc\5.2.0.RELEASE\spring-webmvc-5.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\spring-aop\5.2.0.RELEASE\spring-aop-5.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\spring-expression\5.2.0.RELEASE\spring-expression-5.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\mysql\mysql-connector-java\8.0.18\mysql-connector-java-8.0.18.jar;C:\Users\HP\.m2\repository\org\springframework\boot\spring-boot-starter-test\2.2.0.RELEASE\spring-boot-starter-test-2.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\boot\spring-boot-test\2.2.0.RELEASE\spring-boot-test-2.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\boot\spring-boot-test-autoconfigure\2.2.0.RELEASE\spring-boot-test-autoconfigure-2.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\com\jayway\jsonpath\json-path\2.4.0\json-path-2.4.0.jar;C:\Users\HP\.m2\repository\net\minidev\json-smart\2.3\json-smart-2.3.jar;C:\Users\HP\.m2\repository\net\minidev\accessors-smart\1.2\accessors-smart-1.2.jar;C:\Users\HP\.m2\repository\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;C:\Users\HP\.m2\repository\jakarta\xml\bind\jakarta.xml.bind-api\2.3.2\jakarta.xml.bind-api-2.3.2.jar;C:\Users\HP\.m2\repository\jakarta\activation\jakarta.activation-api\1.2.1\jakarta.activation-api-1.2.1.jar;C:\Users\HP\.m2\repository\org\junit\jupiter\junit-jupiter\5.5.2\junit-jupiter-5.5.2.jar;C:\Users\HP\.m2\repository\org\junit\jupiter\junit-jupiter-api\5.5.2\junit-jupiter-api-5.5.2.jar;C:\Users\HP\.m2\repository\org\junit\jupiter\junit-jupiter-params\5.5.2\junit-jupiter-params-5.5.2.jar;C:\Users\HP\.m2\repository\org\junit\jupiter\junit-jupiter-engine\5.5.2\junit-jupiter-engine-5.5.2.jar;C:\Users\HP\.m2\repository\org\mockito\mockito-junit-jupiter\3.1.0\mockito-junit-jupiter-3.1.0.jar;C:\Users\HP\.m2\repository\org\assertj\assertj-core\3.13.2\assertj-core-3.13.2.jar;C:\Users\HP\.m2\repository\org\hamcrest\hamcrest\2.1\hamcrest-2.1.jar;C:\Users\HP\.m2\repository\org\mockito\mockito-core\3.1.0\mockito-core-3.1.0.jar;C:\Users\HP\.m2\repository\net\bytebuddy\byte-buddy\1.10.1\byte-buddy-1.10.1.jar;C:\Users\HP\.m2\repository\net\bytebuddy\byte-buddy-agent\1.10.1\byte-buddy-agent-1.10.1.jar;C:\Users\HP\.m2\repository\org\objenesis\objenesis\2.6\objenesis-2.6.jar;C:\Users\HP\.m2\repository\org\skyscreamer\jsonassert\1.5.0\jsonassert-1.5.0.jar;C:\Users\HP\.m2\repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;C:\Users\HP\.m2\repository\org\springframework\spring-core\5.2.0.RELEASE\spring-core-5.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\spring-jcl\5.2.0.RELEASE\spring-jcl-5.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\springframework\spring-test\5.2.0.RELEASE\spring-test-5.2.0.RELEASE.jar;C:\Users\HP\.m2\repository\org\xmlunit\xmlunit-core\2.6.3\xmlunit-core-2.6.3.jar com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5 com.study.springboot.SpringbootDataJdbcApplicationTests,contextLoads
10:00:22.907 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
10:00:22.949 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
10:00:23.020 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.study.springboot.SpringbootDataJdbcApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
10:00:23.065 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.study.springboot.SpringbootDataJdbcApplicationTests], using SpringBootContextLoader
10:00:23.071 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.study.springboot.SpringbootDataJdbcApplicationTests]: class path resource [com/study/springboot/SpringbootDataJdbcApplicationTests-context.xml] does not exist
10:00:23.071 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.study.springboot.SpringbootDataJdbcApplicationTests]: class path resource [com/study/springboot/SpringbootDataJdbcApplicationTestsContext.groovy] does not exist
10:00:23.072 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.study.springboot.SpringbootDataJdbcApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
10:00:23.073 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.study.springboot.SpringbootDataJdbcApplicationTests]: SpringbootDataJdbcApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
10:00:23.178 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.study.springboot.SpringbootDataJdbcApplicationTests]
10:00:23.422 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [D:\java\springboot-data-jdbc\target\classes\com\study\springboot\SpringbootDataJdbcApplication.class]
10:00:23.424 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.study.springboot.SpringbootDataJdbcApplication for test class com.study.springboot.SpringbootDataJdbcApplicationTests
10:00:23.716 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.study.springboot.SpringbootDataJdbcApplicationTests]: using defaults.
10:00:23.717 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
10:00:23.754 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@6392827e, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@2ed2d9cb, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@d5b810e, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@43dac38f, org.springframework.test.context.support.DirtiesContextTestExecutionListener@342c38f8, org.springframework.test.context.transaction.TransactionalTestExecutionListener@c88a337, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@5d0a1059, org.springframework.test.context.event.EventPublishingTestExecutionListener@485966cc, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@1de76cc7, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@54bff557, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@593aaf41, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@5a56cdac, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@7c711375]
10:00:23.763 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@6404f418 testClass = SpringbootDataJdbcApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@3e11f9e9 testClass = SpringbootDataJdbcApplicationTests, locations = '{}', classes = '{class com.study.springboot.SpringbootDataJdbcApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@4b5a5ed1, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@20d28811, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@7880cdf3, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@470f1802], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
10:00:23.984 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=-1}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.0.RELEASE)

2019-11-05 10:00:24.889  INFO 17436 --- [           main] c.s.s.SpringbootDataJdbcApplicationTests : Starting SpringbootDataJdbcApplicationTests on DESKTOP-EVJGASP with PID 17436 (started by HP in D:\java\springboot-data-jdbc)
2019-11-05 10:00:24.891  INFO 17436 --- [           main] c.s.s.SpringbootDataJdbcApplicationTests : No active profile set, falling back to default profiles: default
2019-11-05 10:00:27.176  INFO 17436 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-05 10:00:28.350  INFO 17436 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-05 10:00:29.491  INFO 17436 --- [           main] c.s.s.SpringbootDataJdbcApplicationTests : Started SpringbootDataJdbcApplicationTests in 5.475 seconds (JVM running for 8.115)

class com.zaxxer.hikari.HikariDataSource
2019-11-05 10:00:30.164  INFO 17436 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-11-05 10:00:31.970  INFO 17436 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
HikariProxyConnection@1824423245 wrapping com.mysql.cj.jdbc.ConnectionImpl@1e3e1014

2019-11-05 10:00:32.022  INFO 17436 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2019-11-05 10:00:32.031  INFO 17436 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2019-11-05 10:00:32.035  INFO 17436 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

Process finished with exit code 0

在这里插入图片描述

二、查看数据源的源码文件原理

1、在spring-boot-autoconfigure-2.2.0.RELEASE.jar下找到jdbc
在这里插入图片描述
在这里插入图片描述
2、在DataSourceConfiguration是文件查看数据源的源码
在这里插入图片描述
在DataSourceConfiguration导入了jdbc的连接池:
在这里插入图片描述
自动配置原理:
org.springframework.boot.autoconfigure.jdbc:

1、SpringBoot默认可以支持:

org.apache.tomcat.jdbc.pool.DataSource、HikariDataSource、BasicDataSource、

2、、自定义数据源类型

@ConditionalOnMissingBean(DataSource.class) 
@ConditionalOnProperty(name ="spring.datasource.type") 
static class Generic {
 @Bean    
 public DataSource dataSource(DataSourceProperties properties) {        //使用DataSourceBuilder创建数据源,利用反射创建响应type的数据源,并且绑定相关属性       return properties.initializeDataSourceBuilder().build();
    }
 }

3、DataSourceInitializer:ApplicationListener;
作用:
1)、runSchemaScripts();运行建表语句;
2)、runDataScripts();运行插入数据的sql语句;
默认只需要将文件命名为:

schema‐.sql、data‐.sql
默认规则:schema.sql,schema‐all.sql;
可以使用
schema:
‐ classpath:department.sql 指定位置

操作步骤:
a、在resource目录下拷贝一份department.sql文件后,
在这里插入图片描述
第一种方式:
需要重命名:department.sq改成schema或schema-all
源码规则:
在这里插入图片描述
再启动主程序

数据表就加载到jdbc的数据表来了
在这里插入图片描述
第二种方式:
拷贝的sql文件不需重命名,只需要再全局配置文件指定即可,需要添加配置参数:

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://192.168.219.5:3306/jdbc
    driver-class-name: com.mysql.cj.jdbc.Driver

    schema:
        - classpath:department.sql
    initialization-mode: always

4、操作数据库:自动配置了JdbcTemplate操作数据库
在这里插入图片描述
演示方法:
a、首先创建controller类

HelloController.java

package com.study.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

@Controller
public class HelloController {
    @Autowired
    JdbcTemplate jdbcTemplate;
    
    @ResponseBody
    @GetMapping("/query")
    public Map<String,Object>map(){
        List<Map<String,Object>> list=jdbcTemplate.queryForList("select * from department");
        return  list.get(0);
    }
}

b、在jdbc数据库的department数据表插入一条数据
在这里插入图片描述
c、启动主程序
在这里插入图片描述
注意:
department文件还在,启动服务,访问是拿不到数据的,因为sql文件还在resource目录下,所以它会重新创建一个数据表就没有数据的,所以启动前需要把department文件删掉再启动
在这里插入图片描述
访问:
http://localhost:8080/query
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值