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 小时前
服务器部署 gitlab 占用空间太大怎么办,优化思路。
后端
databook2 小时前
Manim实现闪光轨迹特效
后端·python·动效
武子康3 小时前
大数据-98 Spark 从 DStream 到 Structured Streaming:Spark 实时计算的演进
大数据·后端·spark
该用户已不存在3 小时前
6个值得收藏的.NET ORM 框架
前端·后端·.net
文心快码BaiduComate3 小时前
文心快码入选2025服贸会“数智影响力”先锋案例
前端·后端·程序员
neoooo3 小时前
🌐 Cloudflare Tunnel vs ZeroTier:两个世界的内网穿透哲学
后端
卡尔特斯3 小时前
Android Kotlin 项目代理配置【详细步骤(可选)】
android·java·kotlin
白鲸开源3 小时前
Ubuntu 22 下 DolphinScheduler 3.x 伪集群部署实录
java·ubuntu·开源
涡能增压发动积3 小时前
当你不了解“异步”时请慎用“异步”——记一次生产环境故障排查之旅
后端
文心快码BaiduComate3 小时前
用Comate Zulu开发一款微信小程序
前端·后端·微信小程序