七十四、Spring与DAO操作 query()

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 七十四、Spring与DAO操作 query()

JdbcTemplate类中还提供了大量的query()方法来处理各种对数据库表的查询操作。其中,常用的几个query()方法如下表所示:


方法

说明

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)

该方法可以返回多行数据的结果,但必须是返回列表,elementType参数返回的是List元素类型。




对DB的查询操作


       JDBC 模板的查询结果均是以对象的形式返回。根据返回对象类型的不同,可以将查询分为两类:简单对象查询,自定义对象查询。


简单对象查询:查询结果为 String、Integer 等简单对象类型,或该类型做为元素的集合 类型,如 List等。


自定义对象查询:查询结果为自定义类型,如 User 等,或该类型做为元素的集合类型, 如 List等。


前景连接


Spring与DAO操作 execute()

https://blog.csdn.net/m0_54925305/article/details/123149019?spm=1001.2014.3001.5501


Spring与DAO操作 update()

https://blog.csdn.net/m0_54925305/article/details/123169124?spm=1001.2014.3001.5501


       注:以下案例基于前置案例之上进行拓展query操作


案例实施


       1、数据库插入数据

mysql> select * from account;
Empty set (0.00 sec)
mysql> insert into account(username,balance) value("孙悟空",100);
Query OK, 1 row affected (0.04 sec)
mysql> insert into account(username,balance) value("唐僧",1000.00);
Query OK, 1 row affected (0.01 sec)
mysql> insert into account(username,balance) value("猪八戒",2000.00);
Query OK, 1 row affected (0.01 sec)
mysql> insert into account(username,balance) value("沙僧",5000.00);
Query OK, 1 row affected (0.00 sec)
mysql> select * from account;
+----+-----------+---------+
| id | username  | balance |
+----+-----------+---------+
|  1 | 孙悟空    |     100 |
|  2 | 唐僧      |    1000 |
|  3 | 猪八戒    |    2000 |
|  4 | 沙僧      |    5000 |
+----+-----------+---------+
4 rows in set (0.00 sec)

       2、AccountDao中添加查询方法

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

       3、AccountDaoImpl中添加查询方法

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

       4、创建测试类JdbcTemplateTest_delete

package com.Example.jdbc;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JdbcTemplateTest_delete {
  public static void main(String[] args) {
  // 加载配置文件
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  // 获取AccountDao实例
  AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
  // 执行deleteAccount()方法,并获取返回结果
  int num = accountDao.deleteAccount(1);
  if (num > 0) {
    System.out.println("成功删除了" + num + "条数据!");
  } else {
    System.out.println("删除操作执行失败!");
  }
  }
  @Test
  public void findAccountByIdTest() {
  // 加载配置文件
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  // 获取AccountDao实例
  AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
  // 执行findAccountById()方法
  Account account = accountDao.findAccountById(1);
  System.out.println(account);
  }
  @Test
  public void findAllAccountTest() {
  // 加载配置文件
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  // 获取AccountDao实例
  AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
  // 执行findAllAccount()方法,获取Account对象的集合
  List<Account> account = accountDao.findAllAccount();
  // 循环输出集合中的对象
  for (Account act : account) {
    System.out.println(act);
  }
  }
}

       5、查找单个数据,执行findAccountByIdTest方法


4.png


       6、查找全部数据,执行findAllAccountTest方法


5.png


JDBC 模板对象多例


       JdbcTemplate 对象是多例的,即系统会为每一个使用模板对象的线程(方法)创建一个 JdbcTemplate 实例,并且在该线程(方法)结束时,自动释放 JdbcTemplate 实例。所以在每次使用 JdbcTemplate 对象时,都需要通过 getJdbcTemplate()方法来获取。


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3月前
|
NoSQL Java Redis
Redis进阶-Jedis以及Spring Boot操作 Redis 5.x Cluster
Redis进阶-Jedis以及Spring Boot操作 Redis 5.x Cluster
53 0
|
4月前
|
XML Java 数据库
【Spring】通过JdbcTemplate实现CRUD操作
【Spring】通过JdbcTemplate实现CRUD操作
32 0
|
1月前
ssm(Spring+Spring mvc+mybatis)Dao层实现类——DeptDaoImpl
ssm(Spring+Spring mvc+mybatis)Dao层实现类——DeptDaoImpl
12 0
|
1月前
ssm(Spring+Spring mvc+mybatis)Dao接口——IDeptDao
ssm(Spring+Spring mvc+mybatis)Dao接口——IDeptDao
8 0
|
6月前
|
存储 运维 监控
如何在 Spring Boot 中设计和实现业务操作日志功能?
如何在 Spring Boot 中设计和实现业务操作日志功能?
996 4
|
4月前
|
Java 数据库连接 数据库
Spring Boot整合MyBatis操作mysql数据库实战(附源码 超详细)
Spring Boot整合MyBatis操作mysql数据库实战(附源码 超详细)
159 0
|
4月前
|
Java 关系型数据库 MySQL
Spring Boot使用JdbcTemplate操作mysql数据库实战(附源码 超详细)
Spring Boot使用JdbcTemplate操作mysql数据库实战(附源码 超详细)
40 0
|
4月前
|
Java 测试技术 API
读书笔记-Spring中更好的Java泛型操作API-ResolvableType
读书笔记-Spring中更好的Java泛型操作API-ResolvableType
31 0
|
6月前
|
XML Java 数据库连接
使用Spring JDBC中的JdbcTemplate对数据进行增删改查操作教程~
使用Spring JDBC中的JdbcTemplate对数据进行增删改查操作教程~
|
7月前
|
存储 NoSQL Java
MongoDB 入门教程系列之二:使用 Spring Boot 操作 MongoDB
MongoDB 入门教程系列之二:使用 Spring Boot 操作 MongoDB
99 0