IOC配置Druid数据库连接池

IOC配置Druid数据库连接池

引入Druid数据库连接池

在Maven中引入依赖

    <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.10</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.47</version>
    </dependency>

配置外部文件

配置db.propertie

jdbc.username=root
jdbc.url=jdbc:mysql://localhost:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=utf8
jdbc.password=12345678
jdbc.driverClass=com.mysql.jdbc.Driver

使用bean配置信息

<!--    引用外部文件-->

    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

<!--    配置bean-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

        <property name="driverClassName" value="${jdbc.driverClass}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

测试连接

测试使用Druid数据库连接池连接

   //测试使用Druid数据库连接池连接
    @Test
    public void test6() throws SQLException {
        DataSource dataSource = ioc.getBean("dataSource", DataSource.class);
        System.out.println(dataSource.getConnection());
    }

IOC配置Druid数据库连接池

上一篇:springboot 整合阿里的数据库连接池druid


下一篇:SprinBoot-Web-整合