美文网首页
SpringBoot集成DataSource

SpringBoot集成DataSource

作者: 磨陀货_ | 来源:发表于2019-10-22 16:30 被阅读0次

1.导包

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>


    <dependencies>
        <!-- 添加JDBC依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

         <!-- mysql 数据库驱动. -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--第三方数据依赖 druid依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.20</version>
        </dependency>
    </dependencies>

2.自动配置方式

application.properties写法

spring.datasource.url=jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=utf8
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=121212

spring.datasource.type=com.zaxxer.hikari.HikariDataSource

*.yml写法

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql:///ssm
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

3.手动配置方式

*.yml

jdbc:
    username: root
    password: 123456
    url: jdbc:mysql:///ssm
    driver-class-name: com.mysql.jdbc.Driver

    @Bean
    @ConfigurationProperties(prefix = "jdbc")
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        return dataSource ;
    }

相关文章

网友评论

      本文标题:SpringBoot集成DataSource

      本文链接:https://www.haomeiwen.com/subject/jroqvctx.html