//保存
template.execute("insert into account(name,money) values('lisi',1500)");
//更新
template.update("update account set name = ?,money = ? where id = ?","lisan",1300f,9);
//删除
template.update("delete from account where id = ?",9);
//查询所有 使用query方法 new BeanPropertyRowMapper<Account>(Account.class) 返回值为一个列表
List<Account> accounts = template.query("select * from account where money > ?", new BeanPropertyRowMapper<Account>(Account.class), 1000f);
for (Account account : accounts) {
System.out.println(account);
}
//查询一个对象 使用queryFroObject方法,new BeanPropertyRowMapper<Account>(Account.class) 返回值为一个对象
Account account = template.queryForObject("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),1);
System.out.println(account);
//聚合函数 queryForObject方法,Integer.class 返回值为一个Integer类型的数字
Integer count = template.queryForObject("select count(*) from account where money > ?", Integer.class, 1000f);
System.out.println(count);
spring中JdbcTemplate的使用
最新推荐文章于 2024-06-27 08:00:00 发布