数据库连接

一、第一种方法 

1.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
       xmlns:p="http://www.springframework.org/schema/p"
       >

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:user = "root"
p:password="123"
p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://127.0.0.1:3306/demo"
p:maxPoolSize="300"
p:minPoolSize="2"
p:initialPoolSize="2"
>
</bean>

</beans>

测试:

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

DataSource ds = (DataSource) ctx.getBean("dataSource");

System.out.println(ds.getConnection());


二、第二种方法:

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
       xmlns:p="http://www.springframework.org/schema/p"
       >

<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<!-- 列出你需要的读取的属性列表 -->
<list>
<value>jdbc.properties</value>
</list>
</property>
</bean>


<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:user = "${user}"
p:password="${password}"
p:driverClass="${driverClass}"
p:jdbcUrl="${jdbcUrl}"
p:maxPoolSize="${maxPoolSize}"
p:minPoolSize="${minPoolSize}"
p:initialPoolSize="${initialPoolSize}"
>
</bean>
</beans>

2.classpath:jdbc.properties

user = root
password=123
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/demo
maxPoolSize=300
minPoolSize=2
initialPoolSize=2

3.测试

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

DataSource ds = (DataSource) ctx.getBean("dataSource");

System.out.println(ds.getConnection());


三、第三种方法

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
       xmlns:p="http://www.springframework.org/schema/p"
       >

<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="locations">
<!-- 列出你需要的读取的属性列表 -->
<list>
<value>jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
>
</bean>

</beans>

2.jdbc.propertites

dataSource.user = root
dataSource.password=123
dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://127.0.0.1:3306/demo
dataSource.maxPoolSize=300
dataSource.minPoolSize=2
dataSource.initialPoolSize=2

3.测试

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

DataSource ds = (DataSource) ctx.getBean("dataSource");

System.out.println(ds.getConnection());


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值