Spring JDBC
==Spring 框架除了提供 IOC 与 AOP 核心功能外,同样提供了基于 JDBC 的数据访
问功能,使得访问持久层数据更加方便,==使用 Spring JDBC 环境,首先需要一套
Spring 整合 JDBC 环境,整合步骤如下:
1. SpringJDBC 环境搭建
1.1 引入依赖(添加坐标)
junit junit 4.12 test org.springframework spring-test 4.3.2.RELEASE test org.springframework spring-context 4.3.2.RELEASE org.aspectj aspectjweaver 1.8.9 mysql mysql-connector-java 5.1.39 c3p0 c3p0 0.9.1.2 org.springframework spring-jdbc 4.3.2.RELEASE org.springframework spring-tx 4.3.2.RELEASE配置文件(jdbc.properties)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_jdbc?useUnicode=true&characterEncod
ing=utf8
jdbc.user=root
jdbc.password=root
以下为可选配置
initialPoolSize=20
maxPoolSize=100
minPoolSize=10
maxIdleTime=600
acquireIncrement=5
maxStatements=5
idleConnectionTestPeriod=60
bean.xml 配置修改
加载 properties 文件配置
配置数据源
由于建立数据库连接是一个非常耗时耗资源的行为,所以通过连接池预先同数据库建
立一些连接,放在内存中,应用程序需要建立数据库连接时直接到连接池中申请一个就行,
用完后再放回去。
C3P0 与 dbcp 二选一即可
DBCP(DataBase connection pool),数据库连接池。是 apache 上的一个 java 连接
池项目,也是 tomcat 使用的连接池组件。单独使用 dbcp 需要 2 个包:commons- dbcp.jar,commons-pool.jar dbcp,没有自动回收空闲连接的功能. C3P0 是一个开源的 JDBC 连接池,它实现了数据源,支持 JDBC3 规范和 JDBC2 的
标准扩展。目前使用它的开源项目有 Hibernate,Spring 等。c3p0 有自动回收空闲连接
功能
C3P0 数据源配置
C3P0 其他额外配置(对应的值在 db.properties 文件中指定)对于 dbcp 数据源配置如下:
模板类配置
Spring 把 JDBC 中重复的操作建立成了一个模板类:
org.springframework.jdbc.core.JdbcTemplate ,配置文件中加入
1.2 添加db.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_jdbc?useUnicode=true&characterEncoding=utf8
jdbc.user=root
jdbc.password=root
1.3 添加spring.xml文件
- 设置扫描器范围
- 加载properties 配置文件
- 配置成c3p0 数据源
- 配置JdbcTemplate 模板
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--设置扫描器范围-->
<context:component-scan base-package="com.shsxt"/>
<!-- 加载properties 配置文件 -->
<context:property-placeholder location="db.properties"/>
<!-- 配置c3p0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- jdbcTemplate 配置 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
1.4 测试JDBC
@Test
public void testQueryCount() {
// 得到Spring上下文环境
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
// 得到模板类 JdbcTemplate对象
JdbcTemplate jdbcTemplate = (JdbcTemplate) ac.getBean("jdbcTemplate");
Integer count = jdbcTemplate.queryForObject("select count(1) from account",Integer.class);
Integer count1 = jdbcTemplate.queryForObject("select count(1) from account where userId = ?",Integer.class,1);
System.out.println(count+"-------"+count1);
}
简易版: 使用注解方式
@RunWith(SpringJUnit4ClassRunner.class)
将junit测试加到spring环境中
@ContextConfiguration(locations = {“classpath:spring.xml”})
设置要加载的资源文件
1. 定义父类
@RunWith(SpringJUnit4ClassRunner.class) // 将junit测试加到spring环境中
@ContextConfiguration(locations = {"classpath:spring.xml"}) // 设置要加载的资源文件
public class TestBase {
}
2. 继承父类
public class SpringJdbcTest extends TestBase {
@Resource
private JdbcTemplate jdbcTemplate;
@Test
public void testQueryCount() {
Integer count = jdbcTemplate.queryForObject("select count(1) from account",Integer.class);
Integer count1 = jdbcTemplate.queryForObject("select count(1) from account where userId = ?",Integer.class,1);
System.out.println(count+"-------"+count1);
}