9.2JavaEE——JDBCTemplate的常用方法(三)query()方法

2024-06-18 18:12

本文主要是介绍9.2JavaEE——JDBCTemplate的常用方法(三)query()方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

JdbcTemplate类中常用的查询方法 

方法说明
List query(String sql, RowMapper rowMapper)执行String类型参数提供的SQL语句,并通过参数rowMapper返回一个List类型的结果。
List query(String sql, PreparedStatementSetter pss, RowMapper rowMapper)根据String类型参数提供的SQL语句创建PreparedStatement对象,通过参数rowMapper将结果返回到List中。
List query(String sql,Object[] args, RowMapper rowMapper)使用Object[]的值来设置SQL语句中的参数值,rowMapper是个回调方法,直接返回List类型的数据。
queryForObject(String sql,RowMapper rowMapper,Object… args)将args参数绑定到SQL语句中,并通过参数rowMapper返回一个Object类型的单行记录。
queryForList(String sql,Object[] args, class<T> elementType)该方法可以返回多行数据的结果,但必须返回列表,args参数是sql语句中的参数,elementType参数返回的是List数据类型。

        了解了JdbcTemplate类中几个常用的query()方法后,接下来通过一个具体的案例演示query()方法的使用,案例实现步骤如下。

1、插入数据

        向数据表account中插入几条数据。

insert into 'account'('id','username','balance') 
values (1,'zhangsan',100),(3,'lisi',500),(4,'wangwu',300);

2、编写查询方法

        在前面的AccountDao接口中,声明findAccountById()方法,通过id查询单个账户信息;声明findAllAccount()方法,用于查询所有账户信息。

// 通过id查询
public Account findAccountById(int id);
// 查询所有账户
public List<Account> findAllAccount();

3、实现查询方法

        在前面的的AccountDaoImpl类中,实现AccountDao接口中的findAccountById()方法和findAllAccount()方法,并调用query()方法分别进行查询。

// 通过id查询单个账户信息public Account findAccountById(int id) {//定义SQL语句String sql = "select * from account where id = ?";// 创建一个新的BeanPropertyRowMapper对象RowMapper<Account> rowMapper =new BeanPropertyRowMapper<Account>(Account.class);// 将id绑定到SQL语句中,并通过RowMapper返回一个Object类型的单行记录return this.jdbcTemplate.queryForObject(sql, rowMapper, id);}//查询所有账户信息public List<Account> findAllAccount() {// 定义SQL语句String sql = "select * from account";// 创建一个新的BeanPropertyRowMapper对象RowMapper<Account> rowMapper =new BeanPropertyRowMapper<Account>(Account.class);// 执行静态的SQL查询,并通过RowMapper返回结果return this.jdbcTemplate.query(sql, rowMapper);}

4、测试条件查询

        创建测试类FindAccountByIdTest,用于测试条件查询。

public class FindAccountByIdTest {public static void main(String[] args) {// 加载配置文件ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");// 获取AccountDao实例AccountDao accountDao =(AccountDao) applicationContext.getBean("accountDao");Account account = accountDao.findAccountById(1);System.out.println(account);	}
}

5、测试查询所有用户信息

        创建测试类FindAllAccountTest,用于查询所有用户账户信息。

public class FindAllAccountTest {public static void main(String[] args) {// 加载配置文件ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");// 获取AccountDao实例AccountDao accountDao =(AccountDao) applicationContext.getBean("accountDao");List<Account> account = accountDao.findAllAccount(); // 执行方法for (Account act : account) {// 循环输出集合中的对象System.out.println(act); } }
}

6、查看运行结果

        在IDEA中启动FindAllAccountTest类,控制台会输出结果。

Account [id=1,username=zhangsan,balance=100.0]
Account [id=3,username=lisi,balance=500.0]
Account [id=4,username=wangwu,balance=300.0]

这篇关于9.2JavaEE——JDBCTemplate的常用方法(三)query()方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1072735

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick