导包:
Spring的JDBC模板的使用
一、默认连接池
创建数据库
create database spring4;
use spring4;
create table account(id int primary key auto_increment,name varchar(20),money double);
使用JDBC模板保存数据
创建一个测试类
package com.rick.jdbc.demo1;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
/*
* JDBC模板的使用
*/
public class JdbcDemo1 {
@Test
public void demo1() {
//创建连接池
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
driverManagerDataSource.setUrl("jdbc:mysql:///spring4");
driverManagerDataSource.setUsername("root");
driverManagerDataSource.setPassword("000000");
//创建JDBC模板
JdbcTemplate jdbcTemplate = new JdbcTemplate(driverManagerDataSource);
jdbcTemplate.update("insert into account values (null,?,?)","张三",100000);
}
}
点击运行就能发现数据库中多了一条数据
这样的弊端是每次都得重新创建,重新new
我们可以将连接池和模板交给Spring管理(控制反转)
导入aop .jar(应为在spring4中用ioc也需要引入aop的包,spring3就不用了)
引入Spring的配置文件
<?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>
<property name="url" value="jdbc:mysql:///spring4"></property>
<property name="username" value="root"></property>
<property name="password" value="000000"></property>
</bean>
<!-- 配置spring的JDBC的模板 -->
<bean id="jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref = "dataSource"></property>
</bean>
</beans>
创建测试类TestDemo2
package com.rick.jdbc.demo1;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)//让测试运行于Spring测试环境
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcDemo2 {
@Resource(name="jdbcTemplate")//注入
private JdbcTemplate jdbcTemplate;
@Test
public void demo2() {
jdbcTemplate.update("insert into account values (null,?,?)","王五",100000);
}
}
运行!然后数据库中多了一条记录,运行成功
二、使用开源的数据库连接池-DBCP的使用
导包:添加两个dbcp的依赖包
将配置文件中的内置连接池的代码改成以下代码即可,其他不变
<!-- 配置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:///spring4"></property>
<property name="username" value="root"></property>
<property name="password" value="000000"></property>
</bean>
三、使用开源的数据库连接池-DBCP的使用(重要)
导包:c3p0只需要一个jar包
将配置文件中的内置连接池的代码改成以下代码即可,其他不变(注意name和上面的内置和dbcp是不一样的)
<!-- 配置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:///spring4"></property>
<property name="user" value="root"></property>
<property name="password" value="000000"></property>
</bean>
一般在开发的时候这些连接池的东西不会直接写在配置文件中
抽取配置到属性文件
定义一个属性文件 jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring4
idbc.username =root
jdbc.password=000000
在spring配置文件中引入属性文件两种方式
- bean标签引入(很少使用)
- context标签引入
<!-- 第一种bean标签引入(不常用) -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<!-- 第二种context标签引入 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
修改配置文件
ok!下面
使用JDBC的模板完成CRUD的操作
封装数据: