Spring1.6——简单使用JdbcTemplate

本文介绍如何使用Spring的JdbcTemplate结合c3p0连接池进行数据库操作。包括配置数据源、实现DAO层查询功能及使用properties文件管理配置等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAVA框架学习文章索引点这里
使用JdbcTemplate:
1,选用c3p0连接池作为数据源:
Dao层:

package com.i_c3p0;

import org.springframework.jdbc.core.JdbcTemplate;

public class PersonDao {
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public int getNum() {
        String sql = "select count(id) from person";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }
}

配置文件:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://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
                           ">
    <!-- 创建数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="user" value="root"></property>  
        <property name="password" value="123456"></property>
    </bean>
    <!-- 将数据源注入到模板 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="personDao" class="com.i_c3p0.PersonDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
</beans>

测试类:

package com.i_c3p0;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo {
    @Test
    public void test1() {
        String xmlPath = "com/i_c3p0/beans.xml";
        ApplicationContext appcont = new ClassPathXmlApplicationContext(xmlPath);
        PersonDao personDao = (PersonDao) appcont.getBean("personDao");
        System.out.println(personDao.getNum());
    }
}

结果:

2

2,使用JdbcDaoSupport

package com.i_c3p0_autotemplate;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class PersonDao extends JdbcDaoSupport{

    public int getNum() {
        String sql = "select count(id) from person";
        return this.getJdbcTemplate().queryForObject(sql, Integer.class);
    }
}
    <!-- 创建数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="user" value="root"></property>  
        <property name="password" value="123456"></property>
    </bean>
    <!--将数据源注册到模板-->
    <bean id="personDao" class="com.i_c3p0_autotemplate.PersonDao">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

测试类及结果同上
加载properties配置文件
properties配置文件

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/test
user=root
password=123456

xml配置文件

    <!-- 加载文件,classpath代表src下 -->
    <context:property-placeholder location="classpath:com/i_c3p0_properties/data"/>
    <!-- 创建数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}"></property>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="user" value="${user}"></property>  
        <property name="password" value="${password}"></property>
    </bean>
    <!--将数据源注册到模板-->
    <bean id="personDao" class="com.i_c3p0_properties.PersonDao">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值