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]
相关推荐
大神薯条老师12 分钟前
Python从入门到高手4.3节-掌握跳转控制语句
后端·爬虫·python·深度学习·机器学习·数据分析
秋落风声1 小时前
【数据结构】---图
java·数据结构··graph
2401_857622661 小时前
Spring Boot新闻推荐系统:性能优化策略
java·spring boot·后端
qinzechen1 小时前
分享几个做题网站------学习网------工具网;
java·c语言·c++·python·c#
hakesashou1 小时前
python交互式命令时如何清除
java·前端·python
攒了一袋星辰1 小时前
今日指数项目项目集成RabbitMQ与CaffienCatch
java·分布式·rabbitmq
wrx繁星点点1 小时前
事务的四大特性(ACID)
java·开发语言·数据库
IT学长编程1 小时前
计算机毕业设计 Java酷听音乐系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·音乐系统·计算机毕业设计选题
AskHarries1 小时前
如何优雅的处理NPE问题?
java·spring boot·后端
IT学长编程2 小时前
计算机毕业设计 基于协同过滤算法的个性化音乐推荐系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·毕业论文·协同过滤算法·计算机毕业设计选题·个性化音乐推荐系统