2021-09-10 JdbcTemplate类中query()方法的使用
JdbcTemplate类中还提供了大量的query()方法来处理各种对数据库表的查询操作。JdbcTemplate中常用的query()方法方法说明List query(String sql,RowMapper rowMapper)执行String类型参数提供的SQL语句,并通过RowMapper返回一个List类型的结果List query(String sql,PreparedStateme
·
JdbcTemplate类中还提供了大量的query()方法来处理各种对数据库表的查询操作。
JdbcTemplate中常用的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元素类型 |
第一步:向user01表中插入几条数据
第二步:在AccountDao接口中,增加两个方法
package com.itheima.jdbc;
import java.util.List;
public interface AccountDao {
//查询:通过id
public Account findAccountById(int id);
//查询:查询所有账户
public List<Account> findAllAcount();
//添加
public int addAccount(Account account);
//更新
public int updateAccount(Account account);
//删除
public int deleteAccount(int id);
}
第三步:在AccountDao接口的实现类AccountDaoImpl中,实现接口中的方法
如果出现:RowMapper错误,查看是否导错包
正确包:import java.util.List;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
//通过id查询账户数据信息
@Override
public Account findAccountById(int id) {
//定义SQL语句
String sql = "select * from user01 where id=?";
//创建一个新的BeanPropertyRowMapper对象
RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class);
//将id绑定到SQL语句中,并通过RowMapper返回一个Object类型的单行记录
return this.jdbcTemplate.queryForObject(sql, rowMapper,id);
}
//查询所有账户信息
@Override
public List<Account> findAllAcount() {
//定义SQL语句
String sql = "select * from user01";
//创建一个新的BeanPropertyRowMapper对象
RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class);
//执行静态的SQL查询,并通过RowMapper返回结果
return this.jdbcTemplate.query(sql, rowMapper);
}
第四步:创建测试类,在测试类JdbcTemplateTest中,添加定义的两个测试方法
(1)、通过id查询信息
//通过id查询账户数据信息
@Test
public static void main05Test(String[] args){
//加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取AccountDao实例
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
//执行updateAccount()方法,并获取返回结果
Account num = accountDao.findAccountById(4);
System.out.println(num);
System.out.println(num.toString());
System.out.println(num.getUsername());
}
输出结果:
(2)、查询所有信息
//查询所有账户信息
@Test
public static void main06Test(String[] args){
//加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取AccountDao实例
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
//执行findAllAcount()方法,获取Account对象的集合
List<Account> num = accountDao.findAllAcount();
//循环输出集合中的对象
for(Account act : num) {
System.out.println(act);
}
System.out.println(num);
System.out.println(num.toString());
}
输出结果:
更多推荐
已为社区贡献1条内容
所有评论(0)