Spring(八)----Spring的JDBC

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);
		}
	}
}

源码下载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spring-boot-starter-jdbcSpring Boot框架中的一个starter,它提供了与JDBC(Java Database Connectivity)相关的依赖和配置,使得在Spring Boot应用中使用JDBC变得更加简单和方便。 具体来说,spring-boot-starter-jdbc包含了以下功能: 1. 自动配置:它会根据classpath中的依赖自动配置DataSource(数据源)和JdbcTemplate(JDBC模板)等相关的bean。 2. 数据源配置:它提供了默认的数据源配置,可以通过在application.properties或application.yml文件中进行配置,例如指定数据库的URL、用户名、密码等。 3. JdbcTemplate支持:它提供了JdbcTemplate的自动配置,可以通过注入JdbcTemplate对象来执行SQL语句,简化了与数据库的交互操作。 使用spring-boot-starter-jdbc可以让你更加专注于业务逻辑的开发,而无需过多关注底层的数据库连接和操作细节。 如果你想在Spring Boot项目中使用JDBC,可以按照以下步骤进行配置和使用: 1. 在pom.xml文件中添加spring-boot-starter-jdbc依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> ``` 2. 在application.properties或application.yml文件中配置数据源相关信息,例如: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 ``` 3. 在你的代码中注入JdbcTemplate对象,并使用它执行SQL语句,例如: ```java @Autowired private JdbcTemplate jdbcTemplate; public void queryData() { String sql = "SELECT * FROM users"; List<Map<String, Object>> result = jdbcTemplate.queryForList(sql); // 处理查询结果 } ``` 总结一下,spring-boot-starter-jdbcSpring Boot框架中用于简化JDBC操作的一个starter,它提供了自动配置和默认的数据源配置,使得在Spring Boot应用中使用JDBC变得更加方便和高效。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值