Spring(八)----Spring的JDBC

本文介绍Spring框架中的JDBC模板技术,包括IOC和AOP两大核心概念,并详细讲解了如何使用JdbcTemplate类进行数据库操作,涉及连接池配置、基本CRUD操作等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring的两个核心:IOC和AOP。

IOC,以前自己new对象,现在交给spring的IOC容器,配置就OK。

AOP,面向切面编程。事务的管理(Spring提供方式,采用AOP的技术的方式)。

一、Spring框架的JDBC模板技术概述

Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单
  • 提供了JDBC模板:JdbcTemplate类
  • Spring框架可以整合Hibernate框架,也提供了模板类:HibernateTemplate类

二、演示JDBC的模板类

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

CREATE DATABASE spring_day03;
USE spring_day03;
CREATE TABLE t_account (
	id INT PRIMARY KEY auto_increment,
	NAME VARCHAR (20),
	money DOUBLE
);
2. 引入开发的jar包
先引入IOC基本的6个jar包
再引入Spring-aop的jar包
最后引入JDBC模板需要的jar包
  • MySQL数据库的驱动包
  • Spring-jdbc.jar
  • Spring-tx.jar
spring-jdbc.jar包含jdbc模板

模板可以操作数据库的话,就需要有连接。连接可以从连接池中获取。所以,我们的模板需要有连接池。JdbcTemplate需要配置一个DataSource。

3.JdbcTemplate的使用

public class Demo1 {
	/**
	 * 演示模板类
	 */
	@Test
	public void run1() {
		// spring提供了内置的连接池DriverManagerDataSource。如果,不想使用,可以整合其他连接池。
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql:///spring_day03");
		dataSource.setUsername("root");
		dataSource.setPassword("123456");

		// 创建模板类
		JdbcTemplate template = new JdbcTemplate();
		//设置连接池
		template.setDataSource(dataSource);
		//完成操作。主键是自动增长的,所以设置为null
		template.update("insert into t_account values (null, ?, ?)", "guanxi", 1000);
	}
}
上面的例子,我们的dataSource和jdbcTemplate都是new出来的,我们用spring容器来管理。

你看,我们的DataSource有几个属性是通过set方法的设值的。那么,在spring的配置文件里面,我们就可以用property标签来给他们设值。

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" 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">

	<!-- 先配置连接池。spring管理内置的连接池 -->
	<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="123456" />
	</bean>

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

测试类

@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, ?, ?)", "meimei", 1000);
	}
}

三、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
编写配置文件

<?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">

	<!-- 先配置连接池:内置的连接池 -->
	<!-- <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="123456" />
	</bean> -->
	
	<!-- 配置DBCP的连接池 -->
	<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="123456" />
	</bean>

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

测试类和上一个例子的测试类是一样的。

2. 管理C3P0连接池
先引入C3P0的jar包
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar

<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="123456" />
</bean>

四、Spring框架的JDBC模板的简单操作

实体类:
public class Account {

	private Integer id;
	private String name;
	private Double money;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Double getMoney() {
		return money;
	}

	public void setMoney(Double money) {
		this.money = money;
	}

	@Override
	public String toString() {
		return "Account [id=" + id + ", name=" + name + ", money=" + money + "]";
	}
}
ResultSet转成对象的类:
public class BeanMapper implements RowMapper<Account> {

	@Override
	public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
		Account ac = new Account();
		ac.setId(rs.getInt("id"));
		ac.setName(rs.getString("name"));
		ac.setMoney(rs.getDouble("money"));
		return ac;
	}
}
测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1_1 {

	@Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;

	@Test
	// 插入操作
	public void demo1() {
		jdbcTemplate.update("insert into t_account values (null,?,?)", "冠希", 10000);
	}

	@Test
	// 修改操作
	public void demo2() {
		jdbcTemplate.update("update t_account set name=?,money =? where id = ?", "kenneth", 10000, 3);
	}

	@Test
	// 删除操作
	public void demo3() {
		jdbcTemplate.update("delete from t_account where id = ?", 3);
	}

	@Test
	// 查询一条记录
	public void demo4() {
		Account account = jdbcTemplate.queryForObject("select * from t_account where id = ?", new BeanMapper(), 1);
		System.out.println(account);
	}

	@Test
	// 查询所有记录
	public void demo5() {
		List<Account> list = jdbcTemplate.query("select * from t_account", new BeanMapper());
		for (Account account : list) {
			System.out.println(account);
		}
	}
}

源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值