Spring配置数据源(xml加载properties文件)

1.数据源(连接池的作用)

数据源(连接池)是提高程序性能如出现的

事先实例化数据源,初始化部分连接资源

使用连接资源时从数据源中获取

使用完毕后将连接资源归还给数据源

2 数据源的开发步骤

1) 导入数据源的坐标和数据库的驱动坐标

(2)创建数据源对象

(3)设置数据源的基本链接数据
 
 (4)使用数据源获取链接资源和归还资源

3 C3p0链接测试和Druid从properties中获取

 @Test
    public void Test2() throws Exception{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDataSourceName("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jahg");
        dataSource.setUser("root");
        dataSource.setPassword("517818li");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
    }

Druid
@Test
    //Druid测试
    public void Test() throws Exception{
        //读取配置文件(方便解耦)
        ResourceBundle rd = ResourceBundle.getBundle("jdbc");
        String driver = rd.getString("jdbc.driver");
        String url = rd.getString("jdbc.url");
        String username = rd.getString("jdbc.username");
        String password = rd.getString("jdbc.password");
        //获取链接
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        System.out.println(druidDataSource.getConnection());
    }

4 Spring配置数据源和用Xml加载proterties文件

1 Sprin配置数据源

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

DataSource有无参构造方法,而Spring默认就是通过无参构造方法实例化对象的
DataSource要想使用需要通过set方法设置数据库连接信息,而Spring可以通过set方法进行字符串注入

设置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:3306/test"/>
        <property name="user" value="root"/>
    <property name="password" value="root"/>
</bean>

创建链接
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) applicationContext.getBean("dataSource");
Connection connection = dataSource.getConnection();
System.out.println(connection);

2 xml加载proterties方法

开发步骤

1. 引入context命名空间和约束路径
2.<context:property-placeholder location="xx.properties"/>
3.<property name="" value="${key}"/>
applicationContext.xml加载jdbc.properties配置文件获得连接信息
首先,需要引入context命名空间和约束路径:

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

Bean标签设置
<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"
		>
	<context:property-placeholder location="classpath:jdbc.properties"/>
	<bean id="dataSource" 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>
</beans>

链接方法测试:
 public void Test3() throws Exception{
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Connection connection = applicationContext.getBean(DataSource.class).getConnection();
        System.out.println(connection);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值