• spring(4)数据库连接


    jdbc.properties

    user=root
    password=root
    driverClassName=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/test
    initialSize=5
    maxActive=10
    
     
    <?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-4.3.xsd">
        
        <!-- 加載配置文件 -->
        <context:property-placeholder
            location="classpath:jdbc.properties" />
    
        <bean id="dataSource"
            class="com.alibaba.druid.pool.DruidDataSource">
            <property name="username" value="${user}" />
            <property name="password" value="${password}" />
            <property name="driverClassName" value="${driverClassName}" />
            <property name="url" value="${url}" />
            <property name="initialSize" value="${initialSize}" />
            <property name="maxActive" value="${maxActive}" />
    </bean>
    </beans>
    package com.cn.test;
    
    import javax.sql.DataSource;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class DataSourceTest {
        @Test
        public void test1() throws Exception {
            
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            
            DataSource dataSource = (DataSource) applicationContext.getBean("dataSource");
            
            System.out.println( dataSource.getConnection() );
            
        }
    }
  • 相关阅读:
    Java.Annotations
    Tools.Eclipse.HowToImportAnAndroidLibraryProjectIntoWorkspace
    Android.Tools.Eclipse hangs at the Android SDK Content Loader
    .Net之用户控件笔记
    sql语句之静态行转列(转)
    sql语句之查看数据库及数据表创建日期
    sql语句之UNION,联合查询
    Pyhton不能输入中文注释
    js获取referrer中的参数
    angularJS插入html及更换iframe的src
  • 原文地址:https://www.cnblogs.com/ywqtro/p/12263615.html
Copyright © 2020-2023  润新知