(Spring)JDBC模板技术

Spring框架的JDBC模板技术概述

    1、Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变得简单

    2、提供了JDBC模板,Spring框架提供的

         *   JdbcTemplate类

    3、Spring框架可以整合Hibernate框架,也提供了模板类

         *   HibernateTemplate类


演示JDBC的模板类

     步骤一:创建数据库的表结构

create database spring_day03;
use spring_day03;
create table t_account(
    id int primary key auto_increment,
    name varchar(32),
    money double
);

     步骤二:引入开发的jar包

                  *  先引入IoC基本的6个jar包    

                        

                  *  测试需要导的包

                         

                  *  再引入Spring-aop的jar包

                      

                  *  最后引入JDBC模板需要的jar包 

                      >  MySQL数据的驱动包

                      >  Spring-jdbc.jar

                      >  Spring-tx.jar

                        

     步骤三:编写测试代码(自己来new对象的方式)

/**
 * 演示JDBC的模板类
 */
public class Demo1 {
	@Test
	public void run1() {
		// Spring框架提供了内置的连接池,若不想使用内置的,也可使用整合的其他的连接池
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://localhost:3306/spring_day03");
		dataSource.setUsername("root");
		dataSource.setPassword("1234");
		
		// 创建模板类
		JdbcTemplate template = new JdbcTemplate();
		// 设置连接池
		template.setDataSource(dataSource);
		// 操作数据库
		template.update("insert into t_account values(null,?,?)", "小泽",10000);
		
	}
}

使用Spring框架来管理模板类

1、上面编写的代码是new的方式,但应该把这些类交给Spring框架来管理

2、修改步骤如下:

      步骤一:Spring管理内置的连接池

<!-- 先配置连接池 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
	<property name="url" value="jdbc:mysql://localhost:3306/spring_day03"></property>
	<property name="username" value="root"></property>
	<property name="password" value="1234"></property>
</bean>

      步骤二:Spring管理模板类

<!-- 配置JDBC的模板类 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource"></property>
</bean>

      步骤三:编写测试程序

/**
 * 测试JDBC的模板类,使用IoC的方式
 * @author Administrator
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1_1 {

	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Test
	public void run1() {
		jdbcTemplate.update("insert into t_account values(null,?,?)", "小苍",10000);
	}
}

Spring框架管理开源的连接池

1、管理DBCP连接池

     *   先引入DBCP的2个jar包

         >  com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar

         >  com.springsource.org.apache.commons.pool-1.5.3.jar

            

     *   编写配置文件    

<!-- 配置DBCP的连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
	<property name="url" value="jdbc:mysql://localhost:3306/spring_day03"></property>
	<property name="username" value="root"></property>
	<property name="password" value="1234"></property>
</bean>
	
<!-- 配置JDBC的模板类 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource"></property>
</bean>
        测试代码:
/**
 * 测试JDBC的模板类,使用IoC的方式
 * @author Administrator
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1_1 {
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Test
	public void run1() {
		jdbcTemplate.update("insert into t_account values(null,?,?)", "小明",10000);
	}
}

2、管理C3P0连接池

     *   先引入C3P0的jar包

         com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar

        

     *   编写配置文件

<!-- 配置C3P0的连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
	<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_day03"></property>
	<property name="user" value="root"></property>
	<property name="password" value="1234"></property>
</bean>
	
<!-- 配置JDBC的模板类 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource"></property>
</bean>

     测试代码:

/**
 * 测试JDBC的模板类,使用IoC的方式
 * @author Administrator
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1_1 {
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Test
	public void run1() {
		jdbcTemplate.update("insert into t_account values(null,?,?)", "小明",10000);
	}
}


下一篇:Spring框架的JDBC模板的简单操作

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值