`
cookieandsession
  • 浏览: 19448 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

spring中使用JdbcTemplate的三种方式

阅读更多
1、使用Jdbc Template(JdbcTemplate模板类)
2、使用命名参数jdbc模板NamedParameterJdbcTemplate
3、使用基于jdk1.5的简单jdbc模板SimpleJdbcTemplate
public List<Customer> findCustomerByName(String name) {
		String sql = "select id,name,age from customers where name=?";
		return jdbcTemplate.query(sql, new Object[]{name}, new RowMapper(){
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				Customer customer = new Customer();
				customer.setId(rs.getInt("id"));
				customer.setName(rs.getString("name"));
				customer.setAge(rs.getInt("age"));
				return customer;
			}});
	}
public List<Customer> findCustomerByName(String name) {
		String sql = "select id,name,age from customers where name=:name";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		return jdbcTemplate.query(sql, map, new RowMapper(){
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				Customer customer = new Customer();
				customer.setId(rs.getInt("id"));
				customer.setName(rs.getString("name"));
				customer.setAge(rs.getInt("age"));
				return customer;
			}});
	}
String sql = "select id,name,age from customers where name=?";
		return jdbcTemplate.query(sql, new ParameterizedRowMapper<Customer>(){
			public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
				Customer customer = new Customer();
				customer.setId(rs.getInt("id"));
				customer.setName(rs.getString("name"));
				customer.setAge(rs.getInt("age"));
				return customer;
			}}, name);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics