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中插入几条数据。

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

2、编写查询方法

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

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

3、实现查询方法

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

java 复制代码
// 通过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,用于测试条件查询。

java 复制代码
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,用于查询所有用户账户信息。

java 复制代码
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类,控制台会输出结果。

java 复制代码
Account [id=1,username=zhangsan,balance=100.0]
Account [id=3,username=lisi,balance=500.0]
Account [id=4,username=wangwu,balance=300.0]
相关推荐
我真的是大笨蛋7 小时前
K8S-Pod(下)
java·笔记·云原生·容器·kubernetes
碳水加碳水7 小时前
Java代码审计实战:XML外部实体注入(XXE)深度解析
java·安全·web安全·代码审计
努力也学不会java8 小时前
【设计模式】 原型模式
java·设计模式·原型模式
方渐鸿9 小时前
【2024】k8s集群 图文详细 部署安装使用(两万字)
java·运维·容器·kubernetes·k8s·运维开发·持续部署
学亮编程手记9 小时前
K8S v1.33 版本主要新特性介绍
java·容器·kubernetes
Haven-10 小时前
Java-面试八股文-JVM篇
java·jvm·面试
我真的是大笨蛋10 小时前
JVM调优总结
java·jvm·数据库·redis·缓存·性能优化·系统架构
wjs04010 小时前
Git常用的命令
java·git·gitlab
superlls11 小时前
(算法 哈希表)【LeetCode 349】两个数组的交集 思路笔记自留
java·数据结构·算法
田里的水稻11 小时前
C++_队列编码实例,从末端添加对象,同时把头部的对象剔除掉,中的队列长度为设置长度NUM_OBJ
java·c++·算法