zoukankan      html  css  js  c++  java
  • Spring系列 之数据源的配置 数据库 数据源 连接池的区别

    Spring系列之数据源的配置

    数据源,连接池,数据库三者的区别

    连接池:这个应该都学习过,比如c3p0,druid等等,连接池的作用是为了提高程序的效率,因为频繁的去创建,关闭数据库连接,会对性能有很大的消耗,所以就有了连接池,连接池顾名思义是存储多个连接的池子,池子中的连接都是创建好的,我们只要拿来使用即可,不用的时候就归还给连接池,这就大大减少了关闭创建连接的时间,提高了效率
    数据库:存储数据的地方
    数据源:数据源顾名思义是数据的来源,存储了连接数据库所需要的信息,也可以说是用于管理数据库连接池,并不存储真正的数据,仅仅记录了连接哪个数据库,怎么连接。如果把数据库比作一个文件的话,那么数据源存储的就是文件的名称,可以通过文件名称来找到对应的文件,算是一个抽象的映射,一个数据库对应一个数据源,数据源可能是一个连接,也可能是一个连接池
    如果你是玫瑰,他就是牛粪

    在这里插入图片描述

    呸呸呸,说错了
    如果数据是水,数据库就是水库,数据源就是管道,终端用户看到的数据集是管道里流出来的水。

    在这里插入图片描述
    Spring功能这么强大,怎么可能少的了数据源呢

    Spring配置数据源

    配置步骤
    1.导入数据源的坐标与数据库驱动坐标
    2.创建数据源对象
    3.设置数据源的基本连接信息
    4.使用数据源获取连接或归还连接
    需要导入的坐标信息
    junit

    <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
    

    druid

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.9</version>
    </dependency>
    

    c3p0

    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
    </dependency>
    

    spring—context

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.3.RELEASE</version>
    </dependency>
    

    mysql

      <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
    

    我先手动配置一波,等一下再用Spring容器经行配置,大家就能看到二者的巨大差别了
    手动配置数据源

    druid

     public void main() throws Exception{
            //创建数据源
            DruidDataSource druidDataSource = new DruidDataSource();
            //设置连接参数
            druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
            druidDataSource.setUrl("jdbc:mysql://localhost:3309/one");
            druidDataSource.setUsername("root");
            druidDataSource.setPassword("1234");
            //获取连接对象
            DruidPooledConnection connection = druidDataSource.getConnection();
            System.out.println(connection);
        }
    

    c3p0

     public void test2() throws Exception{
            //创建数据源
            ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
            //设置连接参数
            comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
            comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3309/one");
            comboPooledDataSource.setUser("root");
            comboPooledDataSource.setPassword("1234");
            //获取连接对象
           comboPooledDataSource.getConnection();
            System.out.println(comboPooledDataSource);
        }
    

    为了降低耦合性之前我们是通过读取配置文件的方法,这里我给大家重新复习一下

    首先抽取要配置的信息到配置文件
    右端的字符串注意不要加双引号,否则会报错,因为他默认就是字符串

    jdbc.Driver=com.mysql.jdbc.Driver
    jdbc.Url=jdbc:mysql://localhost:3309/one
    jdbc.Username=root
    jdbc.Password=1234
    

    再读取配置文件来创建连接池

    public void test3() throws  Exception{
    //加载路径下的properties
            ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
            //创建数据源
            ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
            //设置连接参数
            comboPooledDataSource.setDriverClass(bundle.getString("jdbc.Driver"));
            comboPooledDataSource.setJdbcUrl(bundle.getString("jdbc.Url"));
            comboPooledDataSource.setUser(bundle.getString("jdbc.Username"));
            comboPooledDataSource.setPassword(bundle.getString("jdbc.Password"));
            //获取连接对象
            comboPooledDataSource.getConnection();
            System.out.println(comboPooledDataSource);
        }
    

    这样的方式很好的降低了耦合性

    重点来了,下面我们来讲讲如何使用Spring来配置数据源

    在这里插入图片描述

    Spring配置数据源

    将DataSource的创建权交给Spring容器去完成
    DataSource有无参构造方法,Spring默认就是通过无参构造方法实例化对象
    DataSource要想使用需要通过set方法设置数据库连接信息,Spring可以通过set方法进行注入

    在Spring容器中配置Bean

     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"/>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3309/one"/>
            <property name="user" value="root"/>
            <property name="password" value="1234"/>
        </bean>
    

    到容器中获取资源

    public void two() throws SQLException {
            ClassPathXmlApplicationContext classPathXmlApplicationContext = new       ClassPathXmlApplicationContext("applicationcontext.xml");
            DataSource datasource = (DataSource)classPathXmlApplicationContext.getBean("datasource");
            Connection connection = datasource.getConnection();
            System.out.println(connection);
    
        }
    

    上面的方法是不是还不够方便,我们可以用更方便的,即读取配置文件的方法
    我们首先引入命名空间与约束路径

    命名空间: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"/>

    配置Bean

    <bean id="dataSource" 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配置源的一些知识,有志同道合的伙伴可以关注我或者私信我加好友一同学习章,共勉

  • 相关阅读:
    提高网站访问速度的34个方法
    ASP.NET MVC3学习笔记四(Controller)
    ASP.NET MVC3 读书笔记一(Razor视图)
    DataSet DataTable DataReader dataAdapter区别
    EF Code First(约定配置)
    asp.net中APPlication、Session和Cookie的区别
    ASP.NET MVC3 读书笔记三(数据注解Dataannotation和验证)
    一步步构建大型网站架构
    ASP.NET MVC3 读书笔记二(HtmlHelper)
    Sql 查询语句总结
  • 原文地址:https://www.cnblogs.com/pjhaymy/p/13700115.html
Copyright © 2011-2022 走看看