Spring - 11 通过Spring框架来管理数据库连接池

124 篇文章 1 订阅
96 篇文章 0 订阅

Spring框架中提供了很多持久层的模板类,下面来介绍以下:

 

  • 首先为工程引入spring6个基本包与aop的包,然后引入JDBC模板需要的jar包

            * MySQL数据库的驱动包mysql-connector-java-5.1.7-bin.jar
            * Spring-jdbc.jar
            * Spring-tx.jar -- 事务用的jar包

  • 创建一个数据库表

create database spring_day03;
    use spring_day03;
    create table t_account(
    id int primary key auto_increment,
    name varchar(20),
    money double
);
  • 在applicationContext.xml中配置连接池与dataSource的bean
<?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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<!-- 配置datasource 内置JDBC
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql:///spring_day03"/>
		<property name="username" value="root"/>
		<property name="password" value="password"/>
	</bean>
	 -->
	 
 	<!-- 配置datasource 内置DBCP 属性除了class与JDBC完全一样
 	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    	<property name="url" value="jdbc:mysql:///spring_day03"/>
    	<property name="username" value="root"/>
    	<property name="password" value="password"/>
    </bean>
    -->
    <!-- 配置datasource 内置C3P0 属性除了name值与与JDBC不同需要注意-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    	<property name="driverClass" value="com.mysql.jdbc.Driver"/>
    	<property name="jdbcUrl" value="jdbc:mysql:///spring_day03"/>
    	<property name="user" value="root"/>
    	<property name="password" value="password"/>
    </bean>
	
	<!-- 配置spring jdbc模板类 
		注意property 属性使用ref引用定义好的数据源datasource
	-->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
</beans>
  • 测试代码
package demo;

import javax.activation.DataSource;
import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:applicationContext.xml")
public class Demo1 {
	
	/*
	 * 使用Spring框架内置的连接池和模板类JdbcTemplate来添加数据
	 */
	@Test
	public void demo1() {
		//Spring框架内置的连接池
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql:///spring_day03");
		dataSource.setUsername("root");
		dataSource.setPassword("Aa123456");
		//spring模板类
		JdbcTemplate template = new JdbcTemplate(dataSource);
		//添加数据
		template.update("insert into t_account values (null,?,?)", "alex", 50000);
		
	}
	
	
	/*
	 * 使用spring 来管理JdbcTemplate和数据源
	 */
	//注解的方式获得jdbcTemplate,无需set方法
	@Resource(name="jdbcTemplate")
	private JdbcTemplate template;
	//测试类
	@Test
	public void demo2() {
		template.update("insert into t_account values (null,?,?)", "alex1", 50000);
	}
	
	
	/*
	 * 使用spring管理第三方开元框架连接池DBCP
	 * 引入一下两个包
	 * com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
	 * com.springsource.org.apache.commons.pool-1.5.3.jar
	 * 只需要在applicationContext中配置即可,测试代码可不用变动
	 */
	@Test
	public void demo3() {
		template.update("insert into t_account values (null,?,?)", "alex3", 50000);
	}
	
	/*
	 * 使用spring管理第三方开元框架连接池C3P0
	 * 引入com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
	 * 只需要在applicationContext中配置即可,测试代码可不用变动
	 */
	@Test
	public void demo4() {
		template.update("insert into t_account values (null,?,?)", "alex4", 50000);
	}
	
	
}
  • 查看数据库结果

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值