关于spring:如何通过-Spring-框架进行JDBC操作

5次阅读

共计 5867 个字符,预计需要花费 15 分钟才能阅读完成。

通过 Spring 框架如何进行 JDBC 操作呢?

Spring 整合 JDBC 的形式

  • 增加依赖
  • 编写配置文件 db.properties
  • bean.xml 配置批改
  • 配置数据源
  • 模板类配置
  • 测试整合后果

案例实操

增加依赖

数据库驱动 jar 包

mysql-connector-java-5.1.25-bin.jar

数据库连接池相干 jar 包

c3p0-0.9.5.2.jar、mchange-commons-java-0.2.11.jar

Spring jdbc 相干 jar

spring-jdbc-4.3.2.RELEASE.jar、spring-tx-4.3.2.RELEASE.jar

<!– spring 框架坐标依赖增加 –>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!– aop –>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!– mysql 驱动包 –>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<!– c3p0 连接池 –>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!– spring jdbc –>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!– springs 事务 –>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

配置文件 db.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

mysql8 版本以上
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/user?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
jdbc.user=root
jdbc.password=root

以下为可选配置

initialPoolSize=20
maxPoolSize=100
minPoolSize=10
maxIdleTime=600
acquireIncrement=5
maxStatements=5
idleConnectionTestPeriod=60

bean.xml 配置批改

加载 properties 文件配置

<!– 加载 properties 配置文件 –>
<context:property-placeholder location=”db.properties” />

<?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:task=”http://www.springframework.org/schema/task”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…d”>
<!– 加载 properties 配置文件 –>
<context:property-placeholder location=”db.properties” />
<bean id=”dataSource” 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.user}”></property>
<property name=”password” value=”${jdbc.password}”></property>
</bean>
<bean id=”jdbcTemplate” class=”org.springframework.jdbc.core.JdbcTemplate”>
<property name=”dataSource” ref=”dataSource”/>
</bean>
</beans>

配置数据源

因为建设数据库连贯是一个十分耗时耗资源的行为,所以通过连接池事后 同数据库建设一些连贯,放在内存中,应用程序须要建设数据库连贯时间接到连 接池中申请一个就行,用完后再放回去。

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 数据源 –>
<bean id=”dataSource” class=”com.mchange.v2.c3p0.ComboPooledDataSource”>

<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>

</bean>

C3P0 其余额定配置(对应的值在 db.properties 文件中指定)

<!– 指定连接池中保留的最大连接数. Default:15–>
<property name=”maxPoolSize” value=”${maxPoolSize}”/>
<!– 指定连接池中保留的最小连接数 –>
<property name=”minPoolSize” value=”${minPoolSize}”/>
<!– 指定连接池的初始化连接数 取值应在 minPoolSize 与 maxPoolSize 之 间.Default:3–>
<property name=”initialPoolSize” value=”${initialPoolSize}”/>
<!– 最大闲暇工夫,60 秒内未应用则连贯被抛弃。若为 0 则永不抛弃。Default:0–>
<property name=”maxIdleTime” value=”${maxIdleTime}”/>
<!– 当连接池中的连贯耗尽的时候 c3p0 一次同时获取的连接数. Default:3–>
<property name=”acquireIncrement” value=”${acquireIncrement}”/>
<!– JDBC 的规范, 用以控制数据源内加载的 PreparedStatements 数量。
但因为预缓存的 statements 属于单个 connection 而不是整个连接池所以设置这个参数须要
思考到多方面的因数. 如果 maxStatements 与 maxStatementsPerConnection 均为 0, 则缓存
被敞开。Default:0–>
<property name=”maxStatements” value=”${maxStatements}”/>
<!– 每 60 秒查看所有连接池中的闲暇连贯.Default:0 –>
<property name=”idleConnectionTestPeriod” value=”${idleConnectionTestPeriod}”/>

对于 dbcp 数据源配置如下:

<!– 配置 dbcp 数据源 –>
<bean id=”myDataSource” class=”org.apache.commons.dbcp2.BasicDataSource”>

<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
<!-- 连接池启动时的初始值 --> 
<property name="initialSize" value="1"/> 
<!-- 最大闲暇值. 当通过一个顶峰工夫后,连接池能够缓缓将曾经用不到的连贯缓缓开释一部分,始终缩小到 maxIdle 为止 --> 
<property name="maxIdle" value="2"/> 
<!-- 最小闲暇值. 当闲暇的连接数少于阀值时,连接池就会预申请一些连贯,以防止洪峰来时再申请而造成的性能开销 --> 
<property name="minIdle" value="1"/> 

</bean>

模板类配置

Spring 把 JDBC 中反复的操作建设成了一个模板类:org.springframework.jdbc.core.JdbcTemplate,配置文件中退出

<!– jdbcTemplate 配置 –>
<bean id=”jdbcTemplate” class=”org.springframework.jdbc.core.JdbcTemplate”>

<property name="dataSource" ref="dataSource"></property>

</bean>

测试整合后果

通过 junit 测试 jdbcTemplate bean 是否获取到

public class TestSpringJdbc {

private JdbcTemplate jdbcTemplate;
@Before
    public void init(){ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
    jdbcTemplate=(JdbcTemplate) ctx.getBean("jdbcTemplate");
}
@Test
public void test() {String sql="select count(1) from account";
    Integer total= jdbcTemplate.queryForObject(sql, Integer.class);
    System.out.println("总计路数:"+total);
}

}

扩大

JDBC 事务

如果应用程序中间接应用 JDBC 来进行长久化,此时应用 DataSourceTransactionManager 来处理事务边界。为了应用 DataSourceTransactionManager,须要应用如下的 XML 将其拆卸到应用程序的上下文定义中:

<bean id=”transactionManager”

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />

</bean>

实际上,DataSourceTransactionManager 是通过调用 java.sql.Connection 来治理事务,而后者是通过 DataSource 获取到的。通过调用连贯的 commit()办法来提交事务,同样,事务失败则通过调用 rollback()办法进行回滚。

正文完
 0