Spring JDBC
Spring JDBC是Spring 提供的JDBC编程组件,可以简化JDBC的编程。
Spring JDBC 专门为DAO模式而设计。一般用于实现DAO接口。
1.有两种使用方式:
- 将DAO继承JdbcDaoSupport(已经过时了)
- 直接在DAO中注入JdbcTemplate对象
2.JdbcTemplate
JdbcTemplate 是 Spring JDBC 的核心API,封装了数据库访问的各种方法包括:
- queryForObject()
- query()
- update()
- execute()
3.JdbcTemplate 使用步骤如下:
1) 导入Spring JDBC 相关的包
- JDBC 驱动
- JDBC 连接池(DBCP、C3P0)
- Spring JDBC 包
2) 利用XML配置文件创建 连接池对象
- 设置数据库连接参数
- 连接池管理策略参数
3) 利用XML配置文件创建 JdbcTemplate对象
- 注入连接池对象
4)利用 @Resource 注解,在Dao中注入JdbcTemplate对象。
5)在Dao中调用JdbcTemolate 对象的CRUD方法
代码如下:
springmvc.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 配置组件扫描 支持 注解 -->
<context:component-scan
base-package="cn.tedu"/>
<util:properties id="jdbc"
location="classpath:db.properties"/>
<!-- 配置数据源,连接到数据库 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="#{jdbc.driver}"/>
<property name="url"
value="#{jdbc.url}"/>
<property name="username"
value="#{jdbc.user}"/>
<property name="password"
value="#{jdbc.pwd}"/>
<property name="initialSize"
value="#{jdbc.initSize}"/>
</bean>
<!-- 配置JDBC Template -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"
ref="dataSource"/>
</bean>
</beans>
TestCase.java
package cn.tedu.test;
import java.util.Properties;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class TestCase {
@Test
public void testJdbc(){
String cfg = "spring-mvc.xml";
ApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
Properties jdbc = ctx.getBean("jdbc",Properties.class);
System.out.println(jdbc);
DataSource ds = ctx.getBean("dataSource",DataSource.class);
System.out.println(ds);
JdbcTemplate tmpl = ctx.getBean("jdbcTemplate",JdbcTemplate.class);
System.out.println(tmpl);
String sql="select 8 as a from dual";
Integer a = tmpl.queryForObject(sql, Integer.class);
System.out.println(a);
}
}