Spring数据源配置

Spring配置数据源

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

  • 数据源(连接池)是提高程序性能出现的
  • 实现实例化数据源,初始化部分连接资源
  • 使用连接资源时从数据源中获取
  • 使用完毕后将连接资源归还给数据源

常见的数据源(连接池):DBCP C3P0 BoneCP Druid等

1.2数据源的手动创建

@Test
//测试c3p0数据源
public void test1(){
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    try {
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/root");
        dataSource.setUser("root");
        dataSource.setPassword("1234");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Test
//测试druid数据源
public void test2(){
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/root");
    dataSource.setUsername("root");
    dataSource.setPassword("1234");
    try {
        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    } catch (Exception throwables) {
        throwables.printStackTrace();
    }
}

读取配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/root
jdbc.username=root
jdbc.password=1234
@Test
public void test3()throws Exception{
    ResourceBundle rb = ResourceBundle.getBundle("jdbc");
    String dirver = 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(dirver);
    dataSource.setJdbcUrl(url);
    dataSource.setUser(username);
    dataSource.setPassword(password);
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
    connection.close();
}

Spring配置数据源

可以将DataSource的创建权交有Spring容器去完成

<bean id="dataSouce" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/root"></property>
    <property name="user" value="root"></property>
    <property name="password" value="1234"></property>
</bean>
@Test
public void test4()throws Exception{
    ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
    DataSource dataSource = app.getBean(DataSource.class);
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
    connection.close();
}

抽取jdbc配置文件

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

首先,需要引入context命名空间和约束路径

  • 命名空间:

    xmlns:context="http://www.springframework.org/schema/context"
    
  • 约束路径

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  • 加载外部properties文件

    <context:property-placeholder location="classpath:jdbc.properties"/>
    
  • <bean id="dataSouce" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    
上一篇:Spring Boot 配置属性


下一篇:hsql+mysql多数据源动态切换