5.Spring配置数据源

1. Spring 配置数据源

1.1 数据源(连接池)的作用

  • 提高程序性能
  • 事先实例化数据源,初始化部分连接资源
  • 使用连接资源时从数据源中获取
  • 使用完毕后将连接资源归还给数据源

常见的数据源:DBCP、C3P0、BoneCP、Druid等

1.2 传统(手动)配置数据源

1)数据源信息与代码耦合

1.c3p0

ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT%2B8");
dataSource.setUser("root");
dataSource.setPassword("664223");
Connection conn = dataSource.getConnection();

System.out.println(conn);

conn.close();

2.Druid

DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT%2B8");
dataSource.setUsername("root");
dataSource.setPassword("664223");
DruidPooledConnection conn = dataSource.getConnection();

System.out.println(conn);

conn.close();

2)使用properties配置文件

1.设置配置文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=664223

2.c3p0

//读取配置文件
ResourceBundle rb = ResourceBundle.getBundle("jdbc");
String driver = rb.getString("jdbc.driver");
String url = rb.getString("jdbc.url");
String username = rb.getString("jdbc.username");
String password = rb.getString("jdbc.password");

//创建数据源对象,设置连接参数
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
Connection conn = dataSource.getConnection();

System.out.println(conn);

conn.close();

3.Druid

//读取配置文件
ResourceBundle rb = ResourceBundle.getBundle("jdbc");
String driver = rb.getString("jdbc.driver");
String url = rb.getString("jdbc.url");
String username = rb.getString("jdbc.username");
String password = rb.getString("jdbc.password");

//创建数据源对象,设置连接参数
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
DruidPooledConnection conn = dataSource.getConnection();

System.out.println(conn);

conn.close();

1.3 Spring配置数据源

Spring容器可以完成dataSource配置

1).xml文件配置

<bean id="dataSource" class="数据源来源">
    <property name="driver名" value="com.mysql.cj.jdbc.Driver"></property>
    <property name="url名" value="jdbc:mysql://localhost:3306/test?useSSL=false&amp;serverTimezone=GMT%2B8"></property>
    <property name="username名" value="root"></property>
    <property name="password名" value="664223"></property>
</bean>

2).java文件

ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = app.getBean(DataSource.class);
Connection conn = dataSource.getConnection();

System.out.println(conn);

conn.close();

1.4 抽取jdbc.properties配置文件

applicationContext.xml加载jdbc.properties配置文件获得连接信息

步骤:

1)引入context命名空间和约束路径:

  • 命名空间:xmlns:context=“http://www.springframework.org/schema/context”
  • 约束路径:“http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd”

2)通过SpEL获取value信息

<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

1.5 知识要点

Spring 容器加载properties文件

<context:property-placeholder location="xx.properties"/>
<property name="name" value="${key}"
上一篇:Linux 内核中红黑树实现分析


下一篇:【编程语言】编码读取失败问题