上面的方法将查询结果集放在List对象中,而queryForMap()方法将结果集放在一个Map对象中,这个Map对象用一个传入参数keyProperty作为key值。例如,要读入一批Employee对象,您可以将这些Employee对象放在一个用employeeNumber属性作为key值的Map对象中。Map对象的值可以是整个Employee对象,也可以是Employee对象的另一个属性,属性的名称由第二个参数valueProperty指定。例如,您可能只是需要一个Map对象,用员工号作为key值,员工姓名作为value值。不要把它和用Map作为结果对象的概念混淆。这个方法可以使用Java Bean和Map(或基本类型的包装类,但不可能这样用)作为结果对象。
代码例子
例子1:执行update(insert,update,delete)
sqlMap.startTransaction();
Product product = new Product();
product.setId (1);
product.setDescription (“Shih Tzu”);
int rows = sqlMap.insert (“insertProduct”, product);
sqlMap.commitTransaction();
例子2:查询成对象(select)
sqlMap.startTransaction();
Integer key = new Integer (1);
Product product = (Product)sqlMap.queryForObject (“getProduct”, key);
sqlMap.commitTransaction();
例子3:用预赋值的结果对象查询成对象(select)
sqlMap.startTransaction();
Customer customer = new Customer();
sqlMap.queryForObject(“getCust”, parameterObject, customer);
sqlMap.queryForObject(“getAddr”, parameterObject, customer);
sqlMap.commitTransaction();
例子4:查询成对象List(select)
sqlMap.startTransaction();
List list = sqlMap.queryForList (“getProductList”, null);
sqlMap.commitTransaction();
例子5:自动提交
//当没调用startTransaction的情况下,statements会自动提交。
//没必要commit/rollback。
int rows = sqlMap.insert (“insertProduct”, product);
例子6:用结果集边界查询成对象List(select)
sqlMap.startTransaction();
List list = sqlMap.queryForList (“getProductList”, null, 0, 40);
sqlMap.commitTransaction();
例子7:用RowHandler执行查询(select)
public class MyRowHandler implements RowHandler {
public void handleRow (Object object, List list) throws SQLException {
Product product = (Product) object;
product.setQuantity (10000);
sqlMap.update (“updateProduct”, product);
// Optionally you could add the result object to the list.
// The list is returned from the queryForList() method.
}
}
sqlMap.startTransaction();
RowHandler rowHandler = new MyRowHandler();
List list = sqlMap.queryForList (“getProductList”, null, rowHandler);
sqlMap.commitTransaction();
例子8:查询成Paginated List(select)
PaginatedList list =
sqlMap.queryForPaginatedList (“getProductList”, null, 10);
list.nextPage();
list.previousPage();
例子9:查询成Map(select)
sqlMap.startTransaction();
Map map = sqlMap.queryForMap (“getProductList”, null, “productCode”);
sqlMap.commitTransaction();
Product p = (Product) map.get(“EST-93”);
代码例子
例子1:执行update(insert,update,delete)
sqlMap.startTransaction();
Product product = new Product();
product.setId (1);
product.setDescription (“Shih Tzu”);
int rows = sqlMap.insert (“insertProduct”, product);
sqlMap.commitTransaction();
例子2:查询成对象(select)
sqlMap.startTransaction();
Integer key = new Integer (1);
Product product = (Product)sqlMap.queryForObject (“getProduct”, key);
sqlMap.commitTransaction();
例子3:用预赋值的结果对象查询成对象(select)
sqlMap.startTransaction();
Customer customer = new Customer();
sqlMap.queryForObject(“getCust”, parameterObject, customer);
sqlMap.queryForObject(“getAddr”, parameterObject, customer);
sqlMap.commitTransaction();
例子4:查询成对象List(select)
sqlMap.startTransaction();
List list = sqlMap.queryForList (“getProductList”, null);
sqlMap.commitTransaction();
例子5:自动提交
//当没调用startTransaction的情况下,statements会自动提交。
//没必要commit/rollback。
int rows = sqlMap.insert (“insertProduct”, product);
例子6:用结果集边界查询成对象List(select)
sqlMap.startTransaction();
List list = sqlMap.queryForList (“getProductList”, null, 0, 40);
sqlMap.commitTransaction();
例子7:用RowHandler执行查询(select)
public class MyRowHandler implements RowHandler {
public void handleRow (Object object, List list) throws SQLException {
Product product = (Product) object;
product.setQuantity (10000);
sqlMap.update (“updateProduct”, product);
// Optionally you could add the result object to the list.
// The list is returned from the queryForList() method.
}
}
sqlMap.startTransaction();
RowHandler rowHandler = new MyRowHandler();
List list = sqlMap.queryForList (“getProductList”, null, rowHandler);
sqlMap.commitTransaction();
例子8:查询成Paginated List(select)
PaginatedList list =
sqlMap.queryForPaginatedList (“getProductList”, null, 10);
list.nextPage();
list.previousPage();
例子9:查询成Map(select)
sqlMap.startTransaction();
Map map = sqlMap.queryForMap (“getProductList”, null, “productCode”);
sqlMap.commitTransaction();
Product p = (Product) map.get(“EST-93”);