Spring的配置数据源连接数据库

2022-10-12 09:38:41

数据源的开发步骤

  1. 导入数据源的坐标和数据库驱动坐标
  2. 创建数据源对象
  3. 设置数据源的基本连接数据
  4. 使用数据源获取连接资源和归还连接资源
public class DataSourceText {

    //测试手动创建c3p0数据源
    @Test
    public void test1() throws Exception {
        ComboPooledDataSource dataSource= new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/text01");
        dataSource.setUser("root");
        dataSource.setPassword("root");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();

        
    }
    
    //测试手动创建driud数据源
    @Test
    public void test02() throws SQLException {
        DruidDataSource dataSource =new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/text01");
        dataSource.setUsername("root");
        dataSource.setPassword("root");

        DruidPooledConnection connection=dataSource.getConnection();
        System.out.println(connection);
        connection.close();

    }
}

抽取配置文件 jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/text01
jdbc.username=root
jdbc.password=root
public class DataSourceText {

    //测试手动创建c3p0数据源
    @Test
    public void test1() throws Exception {

        //读取配置文件
        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.setPassword(password);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setDriverClass(driver);

        //创建连接对象
        Connection connection=dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

  
}

Spring产生DataSource对象

注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/text01"/>
        <property name="password" value="root"/>
        <property name="user" value="root"/>
    </bean>
</beans>
 //Spring容器产生数据源对象
    @Test
    public void test03() throws SQLException {
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = (DataSource) app.getBean("dataSource");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

在 applicationContext.xml 文件中,加载properties 文件  实现对jdbc.properties 的抽取

 在原来的基础上添加了

xmlns:context="http://www.springframework.org/schema/context"

 http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd

<context:property-placeholder location="classpath:jdbc.properties"/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 利用命名空间在applicationContext.xml 文件中,加载properties 文件实现对jdbc.properties 的抽取-->
    <context:property-placeholder location="classpath:jdbc.properties"/>


    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="user" value="${jdbc.username}"/>
    </bean>
</beans>
  • 作者:孤舟一落叶
  • 原文链接:https://blog.csdn.net/weixin_58463882/article/details/123297046
    更新时间:2022-10-12 09:38:41