1、直接配置数据库信息
    (1)配置德鲁伊连接池
    (2)引入德鲁伊连接池依赖 jar 包

    1. <!--直接配置连接池-->
    2. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    3. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    4. <property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>
    5. <property name="username" value="root"></property>
    6. <property name="password" value="root"></property>
    7. </bean>

    2、引入外部属性文件配置数据库连接池
    (1)创建外部属性文件,properties 格式文件,写数据库信息

    1. prop.url=jdbc:mysql://localhost:3306/userDb
    2. prop.username=root
    3. prop.password=root
    4. prop.driverClassName=com.mysql.jdbc.Driver

    (2)把外部 properties 属性文件引入到 spring 配置文件中
    * 引入 context 名称空间

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:p="http://www.springframework.org/schema/p"
    5. xmlns:util="http://www.springframework.org/schema/util"
    6. xmlns:context="http://www.springframework.org/schema/context"
    7. xsi:schemaLocation="http://www.springframework.org/schema/beans
    8. http://www.springframework.org/schema/beans/spring-beans.xsd
    9. http://www.springframework.org/schema/util
    10. http://www.springframework.org/schema/util/spring-util.xsd
    11. http://www.springframework.org/schema/context
    12. http://www.springframework.org/schema/context/spring-context.xsd">

    在 spring 配置文件使用标签引入外部属性文件

    1. <!--引入外部属性文件-->
    2. <context:property-placeholder location="classpath:jdbc.properties"/>
    3. <!--配置连接池-->
    4. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    5. <property name="driverClassName" value="${prop.driverClassName}"></property>
    6. <property name="url" value="${prop.url}"></property>
    7. <property name="username" value="${prop.username}"></property>
    8. <property name="password" value="${prop.password}"></property>
    9. </bean>