当前位置: 首页 > article >正文

Spring 框架的JDBC 模板技术

一、JDBC 模板技术概述

1、什么模板技术?

  Spring 框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单。

2、template 模板

  都是 Spring 框架来提供XxxTemplate,比如Spring框架提供了 JDBC 模板。

  JdbcTemplate 类,Connection 表示连接,管理事务 Statement ResultSet。

二、JDBC 的模板类使用

  Jdbc 模板类的使用,创建 maven 工程,引入坐标依赖。

<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.12</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>aopalliance</groupId><artifactId>aopalliance</artifactId><version>1.0</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.13</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.2.RELEASE</version></dependency>
</dependencies>

    其中,Spring框架的JDBC模板是否能使用取决于Spring-jdbc包。

    编写测试代码(自己来 new 对象的方式)

package com.qcbyjy.test;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;public class Demo1 {
/**
* 使用 new 对象方式完成
*/@Testpublic void run1(){// 创建连接池对象,Spring 框架内置了连接池对象DriverManagerDataSource dataSource = new DriverManagerDataSource();// 设置 4 个参数dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql:///spring_db");dataSource.setUsername("root");dataSource.setPassword("2005");// 提供模板,用有参构造器创建对象JdbcTemplate template = new JdbcTemplate(dataSource);// 完成数据的增删改查template.update("insert into account values (null,?,?)","熊大",1000);}
}

   在上述代码中,通过new关键字使用有参构造器创建template对象,而这个参数传的是连接池对象dataSource,其中包括了四个参数。

  此外,除了有参构造器创建对象,还可以使用set方法。

  所有的修改都用update,所有的查询都用select。

三、使用 Spring 框架来管理模板类

刚才编写的代码使用的是 new 的方式,应该把这些类交给 Spring 框架来管理。

Spring 管理内置的连接池

<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-be
ans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/springcontext.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.
xsd">
<!--配置连接池-->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"
/>
<property name="url" value="jdbc:mysql:///spring_db" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!--配置 jdbc 模板-->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
编写测试方法
package com.qcbyjy.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* 达内教育 -- 腾讯课程认证机构
* 史招阳
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext_jdbc.xml")
public class Demo1_1 {
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 测试的方式
*/
@Test
public void run1(){
jdbcTemplate.update("insert into account values (null,?,?)"," 熊二",500);
}
}

四、Spring 框架管理开源的连接池

配置开源的连接池,使用 Druid 开源的连接池,引入坐标如下

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
将数据库连接的信息配置到属性文件中
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_db
jdbc.username=root
jdbc.password=root
完成核心配置
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-be
ans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/springcontext.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.
xsd">
<!--配置连接池,使用的是 Spring 框架内置的连接池
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"
/>
<property name="url" value="jdbc:mysql:///spring_db" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
-->
<!--使用开源连接池
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"
/>
<property name="url" value="jdbc:mysql:///spring_db" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
-->
<!--加载属性文件
<bean id="placeholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderC
onfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
-->
<!--第二种写法:使用提供标签的方式-->
<context:property-placeholder
location="classpath:jdbc.properties" />
<!--加载属性的文件-->
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--配置 jdbc 模板-->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>

五、Spring 框架的 JDBC 模板的简单操作

增删改查代码编写

import com.qcbyjy.demo1.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext_jdbc.xml")
public class Demo1_1 {
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 测试的方式
*/
@Test
public void run1(){
jdbcTemplate.update("insert into account values (null,?,?)"," 熊四",800);
}
/**
* 修改
*/
@Test
public void run2(){
jdbcTemplate.update("update account set name = ?,money = ? where
id = ?","光头强",100,7);
}
/**
* 删除
*/
@Test
public void run3(){
jdbcTemplate.update("delete from account where id = ?",7);
}
/**
* 通过 id 查询
*/
@Test
public void run4(){
Account account = jdbcTemplate.queryForObject("select * from
account where id = ?", new BeanMapper(), 6);
System.out.println(account);
}
/**
* 查询所有的数据
*/
@Test
public void run5(){
List<Account> list = jdbcTemplate.query("select * from account",
new BeanMapper());
for (Account account : list) {
System.out.println(account);
}
}
}
/**
* 实现类,用来进行数据封装的
*/
class BeanMapper implements RowMapper<Account>{
/**
* 是一行一行进行数据封装的
* @param resultSet
* @param i
* @return
* @throws SQLException
*/
@Override
public Account mapRow(ResultSet resultSet, int i) throws
SQLException {
Account account = new Account();
account.setId(resultSet.getInt("id"));
account.setName(resultSet.getString("name"));
account.setMoney(resultSet.getDouble("money"));
return account;
}
}

相关文章:

Spring 框架的JDBC 模板技术

一、JDBC 模板技术概述 1、什么模板技术&#xff1f; Spring 框架中提供了很多持久层的模板类来简化编程&#xff0c;使用模板类编写程序会变的简单。 2、template 模板 都是 Spring 框架来提供XxxTemplate&#xff0c;比如Spring框架提供了 JDBC 模板。 JdbcTemplate 类&…...

【游戏设计】游戏玩法与游戏机制

在游戏设计中&#xff0c;“玩法”&#xff08;Gameplay&#xff09;和“机制”&#xff08;Game Mechanic&#xff09;是两个频繁出现但容易混淆的概念。许多新手开发者、设计师甚至玩家常常将两者混为一谈。本文将通过定义、对比和案例解析的方式&#xff0c;清晰地阐明二者的…...

Spring的资源Resource和ResourceLoader

两者区别和联系 Resource 和ResourceLoader 都是 Spring 框架中用于资源访问的接口 Resource 是“资源本身”&#xff0c;ResourceLoader 是“资源工厂/加载器”&#xff0c;负责创建 Resource。 ​ Resource:Spring 统一抽象的“资源”对象,可以表示文件、类路径下的文件、U…...

字节跳动旗下火山引擎都覆盖哪些领域

首先&#xff0c;我需要确认火山引擎的主要业务范围。根据之前的资料&#xff0c;火山引擎是字节跳动的企业技术服务平台&#xff0c;可能包括云服务、人工智能、大数据分析等。不过需要更详细的信息&#xff0c;比如具体的产品和服务&#xff0c;覆盖的行业等。 接下来&#x…...

【AI实战】从“苦AI”到“爽AI”:Magentic-UI 把“人类-多智能体协作”玩明白了!

Hello&#xff0c;亲爱的小伙伴们&#xff01;你是否曾经在深夜里&#xff0c;为了自动化点外卖、筛机票、抓网页数据焦头烂额&#xff1f;有没有幻想过哪天能出个“贴心AI管家”&#xff0c;一键点菜、搞定事务、自动操作网页&#xff0c;比你还懂你&#xff1f;更关键——还让…...

LeetCode面试经典150题梳理

link&#xff1a;https://leetcode.cn/studyplan/top-interview-150/ 日期题号备注2025.5.2288. 合并两个有序数组 - 力扣&#xff08;LeetCode&#xff09;通过双指针法从后向前合并来解决&#xff0c;避免覆盖nums1中的元素2025.5.2327. 移除元素 - 力扣&#xff08;LeetCode…...

ABP VNext + Orleans:Actor 模型下的分布式状态管理最佳实践

ABP VNext Orleans&#xff1a;Actor 模型下的分布式状态管理最佳实践 &#x1f680; &#x1f4da; 目录 ABP VNext Orleans&#xff1a;Actor 模型下的分布式状态管理最佳实践 &#x1f680;一、引言&#xff1a;分布式系统的状态挑战 &#x1f4a1;二、架构图与技术栈 &am…...

Linux之 SPI 驱动框架- spi-mem 框架

一、框架变更的历程 1.1 旧框架图 1.2 新框架图 那么问题来了&#xff0c; 为什么要开发新的 SPI 存储器接口&#xff1f; 有了这个新的框架&#xff0c; SPI NOR 和SPI NAND 都可以基于相同的SPI控制器驱动进行支持了。m25p80 驱动将被修改成&#xff0c;使用spi-mem 接口&a…...

振动分析 - 献个宝

1.一个自制的振动能量分析工具 这个分析工具似乎真的定位到了故障的具体位置。 1.1对一组实验室虚拟信号的分析结果: 1.2 对现场真实数据的分析结果 依照边频带的调制,和边频的缝隙宽度,基本定位到问题。 追加几份待看的文档: 齿轮结构的频谱特征 - 知乎使用 FFT 获得…...

从脑电图和大脑记录中学习稳健的深度视觉表征

从脑电图和大脑记录中学习稳健的深度视觉表征 印度&#xff0c;印度&#xff0c;印度&#xff0c;印度大脑实验室&#xff0c;印度 例如&#xff0c;达拉普&#xff0c;克普拉萨德&#xff0c;山&#xff0c;山&#xff0c;新的。ac .在 摘要 解码人类大脑一直是新机器人科学家…...

【论文阅读】——D^3-Human: Dynamic Disentangled Digital Human from Monocular Vi

文章目录 摘要1 引言2 相关工作3 方法3.1 HmSDF 表示3.2 区域聚合3.3. 变形场3.4. 遮挡感知可微分渲染3.5 训练3.5.1 训练策略3.5.2 重建损失3.5.3 正则化限制 4. 实验4.1 定量评估4.2 定性评价4.3 消融研究4.4 应用程序 5 结论 摘要 我们介绍 D 3 D^{3} D3人&#xff0c;一种…...

高分辨率北半球多年冻土数据集(2000-2016)

关键数据集分类&#xff1a;冰冻圈数据集时间分辨率&#xff1a;10 year < x < 100 year空间分辨率&#xff1a;1km - 10km共享方式&#xff1a;开放获取数据大小&#xff1a;339.79 MB数据时间范围&#xff1a;2000-01-01 — 2016-12-31元数据更新时间&#xff1a;2022-…...

Prompt Tuning:轻量级大模型微调全攻略

Prompt Tuning(提示调优)步骤金额流程 传统的 Prompt Tuning(提示调优) 是一种轻量级的大模型微调技术,核心是通过优化连续的提示向量(而非模型参数)来适配特定任务。 一、核心步骤概述 准备任务与数据 明确任务类型(如分类、问答等),准备输入文本和目标标签。加载…...

【VBA 字典的引用和调用方法】

如何引用字典对象。在VBA中&#xff0c;字典不是内置的&#xff0c;所以需要引用Microsoft Scripting Runtime库。 在 VBA 中使用 Dictionary&#xff08;字典&#xff09;对象可以方便地存储键值对&#xff08;Key-Item&#xff09;数据&#xff0c;以下是引用方法和常用参数介…...

基于开源AI智能名片链动2+1模式S2B2C商城小程序的管理与运营策略研究

摘要&#xff1a;本文通过分析开源AI智能名片链动21模式S2B2C商城小程序的技术架构与商业逻辑&#xff0c;探讨其在企业管理与运营中的实践价值。结合案例研究&#xff0c;论证该模式如何通过清晰的目标设定、动态反馈机制和资源整合能力&#xff0c;提升团队执行力与客户粘性。…...

储能电站:风光储一体化能源中心数字孪生

在 “双碳” 目标引领下&#xff0c;我国能源产业加速向清洁低碳、绿色化转型&#xff0c;风能、太阳能等可再生能源的开发利用成为关键。然而&#xff0c;风能和太阳能的波动性、间歇性与随机性&#xff0c;给大规模接入电网带来挑战。储能技术的兴起&#xff0c;为解决这一难…...

iOS 直播特殊礼物特效实现方案(Swift实现,超详细!)

特殊礼物特效是提升直播互动体验的关键功能&#xff0c;下面我将详细介绍如何在iOS应用中实现各种高级礼物特效。 基础特效类型 1.1 全屏动画特效 class FullScreenAnimationView: UIView {static func show(with gift: GiftModel, in view: UIView) {let effectView FullS…...

9. 现代循环神经网络

文章目录 9.1. 门控循环单元&#xff08;GRU&#xff09;9.1.1. 门控隐状态9.1.1.1. 重置门和更新门9.1.1.2. 候选隐状态9.1.1.3. 隐状态 9.1.2. 从零开始实现9.1.2.1. 初始化模型参数9.1.2.2. 定义模型 9.1.3. 简洁实现9.1.4. 小结 9.2. 长短期记忆网络&#xff08;LSTM&#…...

视频太大?用魔影工厂压缩并转MP4,画质不打折!

在日常生活中&#xff0c;我们常常需要将视频文件转换成不同的格式以适应各种设备或平台的播放需求。魔影工厂作为一款功能强大且操作简单的视频转换工具&#xff0c;深受用户喜爱。本文中简鹿办公将手把手教你如何使用魔影工厂将视频转换为MP4格式&#xff0c;并进行个性化设置…...

Python中tqdm进度条工具和enumerate函数的使用详解

tqdm进度条工具 tqdm 是 Python 中一个非常流行的 进度条显示工具库&#xff0c;常用于迭代操作的可视化&#xff0c;比如训练神经网络、批量数据处理等任务。 一、tqdm 是什么&#xff1f; tqdm 全称是 taqaddum&#xff08;阿拉伯语&#xff0c;意为“进展”&#xff09;&a…...

最宽温度范围文本格式PT1000分度表-200~850度及PT1000铂电阻温度传感器计算公式

常用PT铂电阻温度传感器 该图片来自网络&#xff0c;在此对图片作者表示感谢。 白色陶瓷面为测温面。 近距离图片。 常用的有PT100、PT500、PT1000&#xff0c;不常用的还有 PT50、PT200、PT10000等&#xff0c;PT代表铂电阻&#xff0c;后面的数字是零摄氏度时电阻值&#…...

基于Netty架构的充电桩系统设计:服务器运维如何更好保障稳定性?

Netty是一个异步事件驱动的网络应用框架&#xff0c;用于快速开发高性能、高可靠性的网络服务器和客户端。它本质上是NIO的封装和增强&#xff0c;主要针对TCP/IP协议下高性能网络通信场景。 本设计通过Netty的高性能网络通信能力&#xff0c;结合充电桩行业特性&#xff0c;实…...

操作系统学习笔记第1章 操作系统概述(灰灰题库

1.单选题 用户发起系统服务请求时&#xff0c;处理器处于______。 A. 用户态 B. 核心态 C. 阻塞态 D. 挂起态 第 1 题 答案&#xff1a;A 解析&#xff1a;用户态下&#xff0c;用户程序只能执行非特权指令 。当用户发起系统服务请求&#xff08;通常通过系统调用&#xff09;时…...

后端开发实习生-抖音生活服务

职位描述 ByteIntern&#xff1a;面向2026届毕业生&#xff08;2025年9月-2026年8月期间毕业&#xff09;&#xff0c;为符合岗位要求的同学提供转正机会。 团队介绍&#xff1a;生活服务业务依托于抖音、抖音极速版等平台&#xff0c;致力于促进用户与本地服务的连接。过去一…...

机器学习算法-sklearn源起

scikit-learn&#xff08;简称 sklearn&#xff09;是 Python 中最流行的开源机器学习库之一&#xff0c;基于 NumPy、SciPy 和 Matplotlib 构建。它提供了丰富的机器学习算法和工具&#xff0c;适用于数据挖掘和数据分析任务。以下是其核心特点的简介&#xff1a; 1、sklearn主…...

Keepalived 在不同场景下的高可用方案设计与最佳实践

一、Keepalived 典型应用场景深度解析 1. Web 服务器集群&#xff1a;统一入口与故障容错 1.1 场景需求 核心目标&#xff1a;为多台 Web 服务器提供统一 VIP 入口&#xff0c;隐藏后端节点细节&#xff0c;实现故障透明切换。 挑战&#xff1a; 确保用户请求在主节点故障时…...

注册并创建一个微信小程序

目录 &#xff08;一&#xff09;前往微信公众平台&#xff0c;并注册一个微信小程序账号 &#xff08;二&#xff09;配置微信小程序 &#xff08;三&#xff09;创建微信小程序项目 1.流程 1.1获取小程序ID 1.2下载微信开发者工具 1.3安装微信开发者工具 2.创建项目…...

CentOS 10:启动telnet服务

参考&#xff0c; 鳥哥私房菜 - 第七章、網路安全與主機基本防護&#xff1a;限制埠口, 網路升級與 SELinux 7.3.3 埠口与服务的启动/关闭及开机时状态设定 我们知道系统的 Telnet 服务通常是以 super daemon 来控管的&#xff0c;请您启动您系统的 telnet 试看看。 1 要启动 …...

计算机网络——每一层的用到的设备及其作用

计算机网络基础 OSI参考模型TCP/IP协议族集线器&#xff08;Hub&#xff09;交换机&#xff08;Switch&#xff09;路由器&#xff08;Router&#xff09;功能特点无线路由器&#xff08;家庭宽带&#xff09;光猫功能 网关&#xff08;Gateway&#xff09;功能应用场景特点 IP…...

OpenLayers 加载鹰眼控件

注&#xff1a;当前使用的是 ol 5.3.0 版本&#xff0c;天地图使用的key请到天地图官网申请&#xff0c;并替换为自己的key 地图控件是一些用来与地图进行简单交互的工具&#xff0c;地图库预先封装好&#xff0c;可以供开发者直接使用。OpenLayers具有大部分常用的控件&#x…...