首页 > 代码库 > spring boot自动配置之jdbc

spring boot自动配置之jdbc

1.DataSource配置

1.1 默认配置application.xml

spring.datasource.url=jdbc:mysql://localhost/testspring.datasource.username=rootspring.datasource.password=****
spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.type=com.zaxxer.hikari.HikariDataSource

需要在pom.xml加入依赖(我使用了mybatis+mysql)

      <dependency>        <groupId>org.mybatis</groupId>        <artifactId>mybatis</artifactId>        <version>${mybatis.version}</version>      </dependency>      <dependency>        <groupId>org.mybatis</groupId>        <artifactId>mybatis-spring</artifactId>        <version>${mybatis-spring.version}</version>      </dependency>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>      <groupId>mysql</groupId>      <artifactId>mysql-connector-java</artifactId>    </dependency>   

   <dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <exclusions>
      <exclusion>
        <artifactId>tools</artifactId>
        <groupId>com.sun</groupId>
      </exclusion>
    </exclusions>
  </dependency>

看一下

/** * Actual DataSource configurations imported by {@link DataSourceAutoConfiguration}. * * @author Dave Syer * @author Phillip Webb * @author Stephane Nicoll */abstract class DataSourceConfiguration {    @SuppressWarnings("unchecked")    protected <T> T createDataSource(DataSourceProperties properties,            Class<? extends DataSource> type) {        return (T) properties.initializeDataSourceBuilder().type(type).build();    }    /**     * Tomcat Pool DataSource configuration.     */    @ConditionalOnClass(org.apache.tomcat.jdbc.pool.DataSource.class)    @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "http://www.mamicode.com/org.apache.tomcat.jdbc.pool.DataSource", matchIfMissing = true)    static class Tomcat extends DataSourceConfiguration {        @Bean        @ConfigurationProperties(prefix = "spring.datasource.tomcat")        public org.apache.tomcat.jdbc.pool.DataSource dataSource(                DataSourceProperties properties) {            org.apache.tomcat.jdbc.pool.DataSource dataSource = createDataSource(                    properties, org.apache.tomcat.jdbc.pool.DataSource.class);            DatabaseDriver databaseDriver = DatabaseDriver                    .fromJdbcUrl(properties.determineUrl());            String validationQuery = databaseDriver.getValidationQuery();            if (validationQuery != null) {                dataSource.setTestOnBorrow(true);                dataSource.setValidationQuery(validationQuery);            }            return dataSource;        }    }    /**     * Hikari DataSource configuration.     */    @ConditionalOnClass(HikariDataSource.class)    @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "http://www.mamicode.com/com.zaxxer.hikari.HikariDataSource", matchIfMissing = true)    static class Hikari extends DataSourceConfiguration {        @Bean        @ConfigurationProperties(prefix = "spring.datasource.hikari")        public HikariDataSource dataSource(DataSourceProperties properties) {            return createDataSource(properties, HikariDataSource.class);        }    }    /**     * DBCP DataSource configuration.     */    @ConditionalOnClass(org.apache.commons.dbcp2.BasicDataSource.class)    @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "http://www.mamicode.com/org.apache.commons.dbcp2.BasicDataSource", matchIfMissing = true)    static class Dbcp2 extends DataSourceConfiguration {        @Bean        @ConfigurationProperties(prefix = "spring.datasource.dbcp2")        public org.apache.commons.dbcp2.BasicDataSource dataSource(                DataSourceProperties properties) {            return createDataSource(properties,                    org.apache.commons.dbcp2.BasicDataSource.class);        }    }    /**     * Generic DataSource configuration.     */    @ConditionalOnMissingBean(DataSource.class)    @ConditionalOnProperty(name = "spring.datasource.type")    static class Generic {        @Bean        public DataSource dataSource(DataSourceProperties properties) {            return properties.initializeDataSourceBuilder().build();        }    }}

 2.自定义DataSource

1.1 application.xml配置文件

spring:  application:    name: data-multidatasource    datasource:        type: com.zaxxer.hikari.HikariDataSource        url: jdbc:mysql://localhost:3306/test        username: sa        password: ****  second-datasource:    driver-class-name: org.hsqldb.jdbc.JDBCDriver    url: jdbc:hsqldb:mem:db2    username: sa    password:****

1.2 自定义DataSource配置

@Configurationpublic class SencondDataSourceConfiguration {    @Bean    @ConfigurationProperties(prefix = "spring.datasource")    public DataSource newDataSource() {        return DataSourceBuilder.create().build();    }    @Bean(name = "secondDatasource")    @ConfigurationProperties(prefix = "spring.second-datasource")    public DataSource secondDataSource() {        return DataSourceBuilder.create().build();    }}

3.工作机制

待续

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

参考文件:

【1】http://www.liaoxuefeng.com/article/001484212576147b1f07dc0ab9147a1a97662a0bd270c20000

 

spring boot自动配置之jdbc