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]
相关推荐
卷福同学2 小时前
【养虾日记】Openclaw操作浏览器自动化发文
人工智能·后端·算法
江湖十年3 小时前
Go 并发控制:sync.Pool 详解
后端·面试·go
回到原点的码农4 小时前
Spring Data JDBC 详解
java·数据库·spring
gf13211114 小时前
python_查询并删除飞书多维表格中的记录
java·python·飞书
zb200641204 小时前
Spring Boot 实战:轻松实现文件上传与下载功能
java·数据库·spring boot
一勺菠萝丶4 小时前
Flowable + Spring 集成踩坑:流程结束监听器查询历史任务为空 & 获取不到审批意见
java·数据库·spring
jwn9994 小时前
Spring Boot 整合 Keycloak
java·spring boot·后端
宁波阿成4 小时前
OpenClaw 在 Ubuntu 22.04.5 LTS 上的安装与问题处理记录
java·linux·ubuntu·openclaw·龙虾
mldlds4 小时前
SpringBoot详解
java·spring boot·后端
kang_jin4 小时前
Spring Boot 自动配置
java·spring boot·后端