日期:2014-05-16 浏览次数:20473 次
? Spring-JdbcTemplate 总结
1. 查询单个对象:
a)????? 基本类型
================================================
下面两个方法主要适用于静态sql
◆???? public Object queryForObject(String sql, RowMapper rowMapper)
◆???? public Object queryForObject(String sql, Class requiredType)
注意:requiredType 只能是基本类型的class,只针对基本类型有效
◆???? public Object queryForObject(String sql, Object[] args, Class requiredType)
◆???? public Object queryForObject(String sql, Object[] args, RowMapper rowMapper)
◆???? public Object queryForObject(String sql, Object[] args, int [] argTypes, Class requiredType)
注:其中argTypes 可以使用java.sql.Types常量类中的常量值
◆???? public Object queryForObject(String sql, Object[] args, int [] argTypes, RowMapper rowMapper)
注: rowMapper是一个回调接口
以下主要用于查询数值对象
◆???? public int queryForInt(String sql, Object[] args)
◆???? public long queryForLong(String sql, Object[] args)
==================================================
下面的方法将返回的各个列封装成map对象,key为列名,value为列值.只能一行数据,不能返回多行数据.
◆???? public Map queryForMap(String sql, Object[] args)
b)????? 引用类型
◆???? public Object queryForObject(String sql, RowMapper rowMapper)
◆???? public Object queryForObject(String sql, Object[] args, RowMapper rowMapper)
◆???? public Map queryForMap(String sql, Object[] args)
2. 查询多个对象
a)????? 基本类型:
◆???? public List queryForList(String sql, Object[] args)
注:返回的list的每个元素都是一个map对象,该map对象封装了一行数据且只有一行数据,包括要查询的列字段
b)????? 引用类型: ???
◆???? public List queryForList(String sql, Object[] args)
??????? Iterator iter =? this .jdbcTemplate.queryForList(sql,new Object[]{}).iterator();
??????? List list = new ArrayList();
??????? while (iter.hasNext()){
??????????? User user = new User();
??????????? Map map = (Map) iter.next();
??????????? Set<Entry<String, String>> set = map.entrySet();
??????????? Iterator<Entry<String, String>> iter2 = set.iterator();
??????????? Entry<String, String> entry = iter2.next();
??????????? user.setUsername(entry.getValue());
??????????? entry = iter2.next();
??????????