spring的配置文件 以及Spring 连接数据池

spring

api

多个用id 一个用.class

getBean第一种通过id获取 

第二种获取类的只要是这个类都获取 

Spring 连接数据池

    @Test
    //通过Spring配置文件 连接c3p0数据池

    public void test4() throws Exception{

        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        ComboPooledDataSource dateSource = (ComboPooledDataSource) applicationContext.getBean("dateSource");
        Connection connection = dateSource.getConnection();
        System.out.println(connection);
        connection.close();

    }


    @Test
    //手动创建 druid 数据池 通过解析jdbc方法
    public void test3() throws Exception{
ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");//解析jdbc.properties
String driver = jdbc.getString("driver");//获取jdbc对应的driver得到参数
String url = jdbc.getString("url");
String username = jdbc.getString("username");
String password = jdbc.getString("password");
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName(driver);//得到参数
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
Connection connection=dataSource.getConnection();
System.out.println(connection);
connection.close();
 } @Test //手动创建 druid 数据池 public void test2() throws Exception{ DruidDataSource dataSource=new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/db_14"); dataSource.setUsername("root"); dataSource.setPassword("123456"); Connection connection=dataSource.getConnection(); System.out.println(connection); connection.close(); } @Test //手动创建 c3p0 数据池 public void test1() throws Exception{ ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource(); comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/db_14"); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("123456"); //配置连接数据库 Connection connection=comboPooledDataSource.getConnection(); System.out.println(connection); //获取连接  connection.close(); }

spring配置jdbc.properties文件

<?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">
<!--
Spirng中配置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"/>
<!--其次通过<context:property-placeholder location="classpath:jdbc.properties"/> 加载文件 -->
    <bean id="dateSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driver}"></property>
        <property name="jdbcUrl" value="${url}"></property>
        <property name="user" value="${username}"></property>
        <property name="password" value="${password}"></property>
    </bean>

</beans>
原文地址:https://www.cnblogs.com/ziwang520/p/15598317.html