news 2026/9/8 17:46:27

JDBCTemplate基本使用

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
JDBCTemplate基本使用

开发步骤

「1」导入spring-jdbc和spring-tx坐标

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.0.0.RELEASE</version> </dependency>

「2」创建数据库和实体对象

package com.Itheima.domain; public class Account { private String name; private Double balance; public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getBalance() { return balance; } public void setBalance(Double balance) { this.balance = balance; } @Override public String toString() { return super.toString(); } }
public void testJdbcTemplate(){ // 创建数据源 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class); int rows = jdbcTemplate.queryForObject("select count(*) from person", Integer.class); int count = jdbcTemplate.update("insert into account values (?,?)", "zhangsan",5000); }

「3」创建JdbcTemplate对象

<!-- 加载外部的properties文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 数据源对象--> <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.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- jdbc模板对象--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean>
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useSSL=false jdbc.username=root jdbc.password=root jdbc.driver=com.mysql.cj.jdbc.Driver

「4」执行数据库操作

//=========1.新增数据========= int insertRow = jdbcTemplate.update("insert into account values (?,?)","zhangsan",5000); System.out.println("新增影响行数:"+insertRow);

@Test public void testDelete(){ jdbcTemplate.update("delete from account where name=?","jerry"); }

@Test public void testUpdate(){ jdbcTemplate.update("update account set balance=? where name=?",10000,"jerry"); }

此处的泛型就是你要封装的实体的泛型就是数据库对应的Java类

@Test public void testQueryAll(){ List<Account> query = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class)); System.out.println("query = " + query); }

查某一条数据

@Test public void testQueryOne(){ Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "jerry"); System.out.println("account = " + account); }

聚合查询用到的 (下面的SQL用于辅助回忆)

查询全班总人数、总分、平均分、最高分、最低分

SELECT COUNT(*) AS 总人数, SUM(score) AS 总分, AVG(score) AS 平均分, MAX(score) AS 最高分, MIN(score) AS 最低分 FROM student;

// 聚合查询 @Test public void testQueryAccount(){ Account account = jdbcTemplate.queryForObject("select count(*) from account", Account.class); System.out.println("account = " + account); }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/8 17:43:13

Pi Agent终端编程代理:从安装到实践的全流程指南

1. Pi Agent到底是个什么东西Pi Agent是一个跑在终端里的极简编程代理&#xff08;coding agent&#xff09;&#xff0c;装好之后你直接在命令行里给它下任务&#xff0c;它就能帮你读代码、改文件、跑命令、执行测试&#xff0c;整个过程不需要离开终端&#xff0c;也不依赖I…

作者头像 李华
网站建设 2026/9/8 17:41:58

第十六讲:安装NFS服务器

大家好&#xff0c;接下来的一段时间我将开始学习野火的Linux系统课程并将学习到的干货逐步更新到我的CSDN博客中。没时间刷课的同学可以把我的博客喂给AI 突击一下。 目录 什么是NFS&#xff1f; 常见配置命令 常见问题 实战&#xff1a; 1.更新apt&#xff1a; 2.下载…

作者头像 李华
网站建设 2026/9/8 17:38:14

Clawdbot深度拆解:AI客服智能对话引擎与多渠道接入实战

2. Clawdbot 核心功能拆解 2.1 智能对话引擎&#xff1a;不止是聊天机器人 很多朋友一听到 AI 客服机器人&#xff0c;第一反应就是"不就是个自动回复吗"。说实话&#xff0c;Clawdbot 的智能对话引擎完全不是传统意义上的 FAQ 应答机&#xff0c;它在设计上做了几个…

作者头像 李华
网站建设 2026/9/8 17:38:02

WandEnhancer 三步解锁 WeMod Pro:本地补丁工具完整上手教程

WandEnhancer 三步解锁 WeMod Pro&#xff1a;本地补丁工具完整上手教程 【免费下载链接】Wand-Enhancer Advanced UX and interoperability extension for Wand (WeMod) app 项目地址: https://gitcode.com/GitHub_Trending/we/Wand-Enhancer 用 WeMod 免费版每天被倒计…

作者头像 李华