Spring JdbcTemplate的基本使用
一、jdbcTemplate的概述
它是spring框架中提供的一个对象,是对原始繁杂的jdbc 对象的简单封装。
二、使用步骤:
1、导入坐标
<!-- jdbcTemplate坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- 业务功能管理-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
3、创建JdbcTemplate对象
我们进行创建它的对象时还是需要我们先一步进行操作连接数据池。在这创建好后,在去创建JdbcTemplate的对象,并把数据池传入连接
JdbcTemplate template = new JdbcTemplate();
template.setDataSource(druidDataSource);
4、操作数据库
更新操作(添加,删除,修改)使用:
template.update("update spring_res set name=? where id=?","封","1");
查询操作(查询全部,条件查询,数量):
/**
* 查询全部使用query方法 使用new BeanPropertyRowMapper<User>(User.class),
* 将帮我们自动封装成一个list集合
*/
在上面中我们可以发现在第三条中的测试用例用原始的方法来操作数据库很繁琐,而spring框架提供了一些封装方法,来简化操作。这在第四条中也可以清楚的看到。
Spring 简化JdbcTemplate:
1、在spring核心配置文件中配置JdbcTemplate
<!-- 加载外部的properties文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 把数据库链接的部分进行分开,进一步解耦-->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource" >
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 进行扫描包下的注解-->
<context:component-scan base-package="com.qinfeng"/>
<!-- jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="druidDataSource"/>
</bean>
而在我们使用外部文件的db.properties时,需要使用到context标签,这就需要我们在spring核心文件的跟标签中进行添加
添加context标签:
<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"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
2、使用注解注入JdbcTemplate
3、测试使用
/**
* 按条件查询是依旧使用new BeanPropertyRowMapper<User>(User.class)
* 但是我们按条件查询时,需要进行传值,需要使用
* queryForObject 将会返回我们查询的一个对象实例
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码