Spring中JdbcTemplate的常用操作

本文记录Java学习过程中遇到的Spring中JdbcTemplate的常用操作~

插入操作

jdbcTemplate.update("insert into account values (null, ?, ?)", "oliver", 18);

修改操作

jdbcTemplate.update("update account set name=?,age=? where id = ?", "oliver", 18, 1);

删除操作

jdbcTemplate.update("delete from account where id = ?", 1);

示例代码(RowMapper):

class MyRowMapper implements RowMapper<Account> {
	@Override
	public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
		Account account = new Account();
		account.setId(rs.getInt(id));
		account.setName(rs.getString("name"));
		account.setAge(rs.getInt("age"));
		return account;
	}
}

查询单条记录

Account account  = jdbcTemplate.queryForObject("select * from account where id = ?", new MyRowMapper(), 1);

查询所有记录

List<Account> list = jdbcTemplate.query("select * from account", new MyRowMapper(););

标签: none