Spring JDBC

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文件
  1. 设置扫描器范围
  2. 加载properties 配置文件
  3. 配置成c3p0 数据源
  4. 配置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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值