spring--数据库连接
Posted 祈欢
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring--数据库连接相关的知识,希望对你有一定的参考价值。
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() ); } }
以上是关于spring--数据库连接的主要内容,如果未能解决你的问题,请参考以下文章
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段
错误:E/RecyclerView:未连接适配器;跳过片段上的布局
What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段