Spring数据源对象管理

目录

数据源对象管理

加载properties文件

实现Druid管理

实现C3P0管理


 

数据源对象管理

加载properties文件

resources下创建一个jdbc.properties文件,并添加对应的属性键值对

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db
jdbc.username=root
jdbc.password=roo

开启context命名空间

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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
            ">

这样在spring中就可以使用一个全新的命名空间 context

加载properties配置文件

<context:property-placeholder location="jdbc.properties"/>

完成属性注入

使用${key}来读取properties配置文件中的内容并完成属性注入 

<!--    3.使用属性占位符${}读取properties文件中的属性-->
<!--    说明:idea自动识别${}加载的属性值,需要手工点击才可以查阅原始书写格式-->
    <bean class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
        <property name="name" value="${username}"/>
    </bean>

多个properties配置文件需要被加载

<!--    1.开启context命名空间-->
<!--    2.使用context空间加载properties文件-->
    <context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
    <context:property-placeholder location="jdbc.properties,jdbc2.properties" system-properties-mode="NEVER"/>
<!--    classpath:*.properties  :   设置加载当前工程类路径中的所有properties文件-->
<!--    system-properties-mode属性:是否加载系统属性-->
    <context:property-placeholder location="*.properties" system-properties-mode="NEVER"/>

    <!--classpath*:*.properties  :  设置加载当前工程类路径和当前工程所依赖的所有jar包中的所有properties文件-->
    <context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/>

标准格式 

 <context:property-placeholder location="classpath:*.properties" system-properties-mode="NEVER"/> 

实现Druid管理

导入druid的依赖

从mvn的仓库https://mvnrepository.com/中进行搜索

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

配置第三方bean

第三方类 DruidDataSource

<!--    管理DruidDataSource对象-->
    <bean class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/spring_db"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

根据以上的,使用${key}来读取properties配置文件中的内容并完成属性注入  

从IOC容器中获取对应的bean对象

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            DataSource dataSource = (DataSource) ctx.getBean("dataSource");
            System.out.println(dataSource);
    }

运行程序

实现C3P0管理

导入C3P0的依赖,和mysql

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
<!-- mysql驱动 -->
<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.49</version>
</dependency>

        这里需要注意:mysql的版本需要5.1.49 否则报错The last packet successfully received from the server was 927 milliseconds a 连接数据库超时  下面的conn=dataSource.getConnection() =null。Spring原始注解

配置第三方bean

根据以上的,使用${key}来读取properties配置文件中的内容并完成属性注入

运行程序

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值