Spring的JDBC Template
Spring的JDBC Template(JDBC模板)简化JDBC API开发,使用上和Apache公司的DBUtils框架非常类似)
快速入门实例
1、创建项目后,导入Spring基础核心开发包、数据库驱动包以及日志记录相关包
导入JDBC模板开发包:spring-jdbc-3.2.7.RELEASE.jar、spring-tx-3.2.7.RELEASE.jar以及mySql的驱动
2、创建applicationContext.xml
3、编写一个测试类
package com.js.demo1; import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource; public class SpringJDBCTemplateTest { @Test
public void demo1(){ //创建连接池,这里使用的是Spring自带的连接池
DriverManagerDataSource dataSource = new DriverManagerDataSource();
//设置参数
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/springjdbc");
dataSource.setUsername("root");
dataSource.setPassword("123456"); //使用JDBC的模板,传入DataSource,带参构造器或者setter方法均可
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))");
System.out.println("创建 table user...");
}
}
默认连接池、DBCP连接池、C3P0池的配置
1、在src目录下新建属性文件,文件名任意,我这里取jdbc.properties,文件前后不能有多余空格。
2、将参数写入属性文件中,属性名可以任意,但是不能和连接池的默认属性冲突,比如C3P0连接池赋值为user的不能是${jdbc.username}:
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/springjdbc?characterEncoding=utf-8
jdbc.name = root
jdbc.password = 123456
设置参数到属性文件的两种方式
<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
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/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 第1种,配置属性文件:配置bean,暂时注释 --> <!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"></property> </bean> -->
<!-- 第2种,配置属性文件:引入context约束,然后解析属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置Spring默认连接池 -->
<bean id="defaultDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.name}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 定义JDBC的模板类 -->
<bean id="defaultJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="defaultDataSource"></property>
</bean>
</beans>
1、默认连接池,实现类 DriverManagerDataSource
1、导入JDBC模板开发包:spring-jdbc-3.2.7.RELEASE.jar、spring-tx-3.2.7.RELEASE.jar以及mySql的驱动
2、配置Spring默认连接池以及JDBC模板类
<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
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/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 第1种,配置属性文件:配置bean,暂时注释 -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean> -->
<!-- 第2种,配置属性文件:引入context约束,然后解析属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置Spring默认连接池 -->
<bean id="defaultDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.name}"></property>
<property name="username" value="${jdbc.name}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 定义JDBC的模板类 -->
<bean id="defaultJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="defaultDataSource"></property>
</bean>
</beans>
3、编写测试类
package com.js.demo1; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /**
* 默认连接池配置测试
* * @author hdb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class DefaultDataSourceTest { @Autowired
@Qualifier("defaultJdbcTemplate")
private JdbcTemplate jdbcTemplate; @Test
public void demo2(){
jdbcTemplate.execute("create table user1 (id int primary key auto_increment,name varchar(20))");
System.out.println("创建 table user1...");
}
}
2、DBCP数据源 BasicDataSource
1、导入JDBC模板开发包:spring-jdbc-3.2.7.RELEASE.jar、spring-tx-3.2.7.RELEASE.jar、commons-dbcp-1.4.jar、commons-pool-1.6.jar以及mySql的驱动
2、配置DBCP连接池
<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
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/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 第1种,配置属性文件:配置bean,暂时注释 -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean> -->
<!-- 第2种,配置属性文件:引入context约束,然后解析属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置DBCP连接池 -->
<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.name}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 定义JDBC的模板类 -->
<bean id="dbcpJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dbcpDataSource"></property>
</bean>
</beans>
3、编写测试类
package com.js.demo1; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /**
* DBCP连接池配置测试
* @author hdb
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class DbcpDataSourceTest { @Autowired
@Qualifier("dbcpJdbcTemplate")
private JdbcTemplate jdbcTemplate; @Test
public void demo2(){
jdbcTemplate.execute("create table user2 (id int primary key auto_increment,name varchar(20))");
System.out.println("创建 table user2...");
}
}
3、C3P0数据源 ComboPooledDataSource(重点掌握)
1、导入JDBC模板开发包:spring-jdbc-3.2.7.RELEASE.jar、spring-tx-3.2.7.RELEASE.jar、com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar以及mySql的驱动
2、配置C3P0连接池
<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
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/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 第1种,配置属性文件:配置bean,暂时注释 -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean> -->
<!-- 第2种,配置属性文件:引入context约束,然后解析属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0连接池 -->
<bean id="C3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.name}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 定义JDBC的模板类 -->
<bean id="C3p0JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="C3p0DataSource"></property>
</bean>
</beans>
3、编写测试类
package com.js.demo1; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /**
* C3P0连接池配置测试
* @author hdb
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class C3p0DataSourceTest { @Autowired
@Qualifier("C3p0JdbcTemplate")
private JdbcTemplate jdbcTemplate; @Test
public void demo2(){
jdbcTemplate.execute("create table user3 (id int primary key auto_increment,name varchar(20))");
System.out.println("创建 table user3...");
}
}
Spring的JDBC Template的更多相关文章
- SSM 实训笔记 -11- 使用 Spring MVC + JDBC Template 实现筛选、检索功能(maven)
SSM 实训笔记 -11- 使用 Spring MVC + JDBC Template 实现筛选.检索功能(maven) 本篇是新建的一个数据库,新建的一个完整项目. 本篇内容: (1)使用 Spri ...
- Spring之JDBC Template
时间:2017-2-5 18:16 --Spring对不同持久化技术的支持Spring为各种支持的持久化技术都提供了简单操作的模板和回调.ORM持久化技术: JDBC: org.s ...
- Spring中jdbc Template使用
http://1358440610-qq-com.iteye.com/blog/1826816
- Spring框架学习10——JDBC Template 实现数据库操作
为了简化持久化操作,Spring在JDBC API之上提供了JDBC Template组件. 1.添加依赖 添加Spring核心依赖,MySQL驱动 <!--Spring核心基础依赖--> ...
- Unit06: Spring对JDBC的 整合支持 、 Spring+JDBC Template、Spring异常处理
Unit06: Spring对JDBC的 整合支持 . Spring+JDBC Template .Spring异常处理 1. springmvc提供的异常处理机制 我们可以将异常抛给spring框架 ...
- spring学习笔记之---JDBC Template
JDBC Template(简化持久化操作) (一)创建项目 (1)Maven配置 <dependencies> <dependency> <groupId>ju ...
- spring+jdbc+template+transaction实现
使用spring和jdbc模板事务实现 1.创建实体类: Role package com.wbg.sjt.entity; public class Role { private int id; pr ...
- spring 整合JDBC
使用Spring提供的三个JDBC模板类(JdbcTemplate.NamedParameterJdbcTemplate.SimpleJdbcTemplate)操作数据库 一.JdbcTemplate ...
- Spring第七篇【Spring的JDBC模块】
前言 上一篇Spring博文主要讲解了如何使用Spring来实现AOP编程,本博文主要讲解Spring的对JDBC的支持- 对于JDBC而言,我们肯定不会陌生,我们在初学的时候肯定写过非常非常多的JD ...
随机推荐
- Python:笔记(7)——yield关键字
Python:笔记(7)——yield关键字 yield与生成器 所谓生成器是一个函数,它可以生成一个值的序列,以便在迭代中使用.函数使用yield关键字可以定义生成器对象. 一个例子 我们调用该函数 ...
- Linux 安装配置 Nginx
前言 准备用flask做一个自己的博客网站,打算用Nginx来部署,所以在阿里云的服务器上安装Nginx,参考了很多教程,现在将步骤以及自己遇到的坑写下来,希望能对别人有所帮助. 我用的服务器是阿里云 ...
- hdu5073 贪心
这题说的是给了 n个值每个值 然后 他们的品均值 作为中点 然后每个点到中点的均值的平方 和最小值是多少 有 k 个点可以重新 放过位置 , 这样我们 应该 会选 最近的那个 n-k个点 然后 取他们 ...
- android,结合Timer和TimerTask实现定时任务
当我们需要每隔一段时间执行一个任务的时候,就需要使用TimerTask了,下面是入门的例子, 值得注意的是Timer.TimerTask,cancel之后就需要重新声明一个对象,否则会报错的哦~ pa ...
- 587. Erect the Fence(凸包算法)
问题 给定一群树的坐标点,画个围栏把所有树围起来(凸包). 至少有一棵树,输入和输出没有顺序. Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]] Output: ...
- 服务器环境配置nginx / php / php-fpm(一)
登陆,升级应用,查询和关闭selinux yum update getenforce setenforce 0 vi /etc/selinux 添加非root用户 adduser deploy pas ...
- 使用idea创建JavaWeb项目
[第一步] File---New---Project [第二步] 选择Java Enterprise版本,然后配置tomcat 注意:这里关联的tomcat home指的是tomcat的解压目录(bi ...
- 高级Bash脚本编程(一)
高级Bash脚本编程 Bash 它是能力很强的计算机语言,被称为解释性语言或脚本语言,它可以调用所有的UNIX命令和工具再加上公共程序. Bash中的特殊字符 注释(#) (除#!外,#!是用于指定当 ...
- String StringBuilder StringBuffer 对比 总结得非常好
转自:http://www.iteye.com/topic/522167 作者:每次上网冲杯Java时,都能看到关于String无休无止的争论.还是觉得有必要让这个讨厌又很可爱的String美眉,赤裸 ...
- 递归--练习10--noi1696逆波兰表达式
递归--练习10--noi1696逆波兰表达式 一.心得 递归大法好 二.题目 1696:逆波兰表达式 总时间限制: 1000ms 内存限制: 65536kB 描述 逆波兰表达式是一种把运算符前置 ...