1.queryForObject

  /**
* Executes a mapped SQL SELECT statement that returns data to populate
* the supplied result object.
* <p/>
* The parameter object is generally used to supply the input
* data for the WHERE clause parameter(s) of the SELECT statement.
*
* @param id The name of the statement to execute.
* @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
* @param resultObject The result object instance that should be populated with result data.
* @return The single result object as supplied by the resultObject parameter, populated with the result set data,
* or null if no result was found
* @throws java.sql.SQLException If more than one result was found, or if any other error occurs.
*/
Object queryForObject(String id, Object parameterObject, Object resultObject) throws SQLException;

当查询对象是一个重量级对象、创建过程比較复杂时或者查询对象没有默认的构造方法时。通过该方法。能够在外部先构建好查询对象。然后传给Ibatis。Ibatis此时不会创建新对象,而是调用传入对象的set方法进行赋值。

2.queryForList

  /**
* Executes a mapped SQL SELECT statement that returns data to populate
* a number of result objects within a certain range.
* <p/>
* This overload assumes no parameter is needed.
*
* @param id The name of the statement to execute.
* @param skip The number of results to ignore.
* @param max The maximum number of results to return.
* @return A List of result objects.
* @throws java.sql.SQLException If an error occurs.
*/
List queryForList(String id, int skip, int max) throws SQLException;

利用这种方法能够实现分页功能,如(skip=0,max=10)返回前10条数据。(skip=10,max=10)返回第10-20条数据,但这种方法的分页效率很低,由于Ibatis是把全部的查询结果查询出来之后才进行筛选操作。数据量小的时候用用还能够,所以这种方法比較鸡肋。

3.queryForMap

/**
* Executes a mapped SQL SELECT statement that returns data to populate
* a number of result objects that will be keyed into a Map.
* <p/>
* The parameter object is generally used to supply the input
* data for the WHERE clause parameter(s) of the SELECT statement.
*
* @param id The name of the statement to execute.
* @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
* @param keyProp The property to be used as the key in the Map.
* @return A Map keyed by keyProp with values being the result object instance.
* @throws java.sql.SQLException If an error occurs.
*/
Map queryForMap(String id, Object parameterObject, String keyProp) throws SQLException;

网上有不少帖子说这种方法仅仅能返回一条记录是不正确的,还有说是把resultClass的全部属性放到一个map中返回来也是不正确的。这种方法是对queryForList的一个补充,大部分情况下我们用的都是queryForList返回对象的列表,但有时候放到Map里用起来可能更方便,假设没有这种方法还得自己进行转换,相同的一个<select ...>配置,不用做不论什么更改即能够用queryForList訪问也能够用queryForMap訪问。

最新文章

  1. (转)java DecimalFormat用法
  2. HTML5学习笔记(持续更新中....)
  3. Centeros7 环境相关问题
  4. ThinkPHP中SQL调试方法
  5. file_operations结构体解析 1
  6. 推荐几款提高.net编程效率的辅助工具
  7. HDU_1245_Saving James Bond_最短路
  8. JUDE-UML工具软件介绍
  9. 机器翻译评测——BLEU改进后的NIST算法
  10. OpenGL ES学习001---绘制三角形
  11. zabbix3.2监控vcenter和exsi信息
  12. Android CollapsingToolbarLayout使用介绍
  13. 微信小程序之wx.showmodal
  14. 深入springboot原理——动手封装一个starter
  15. Java中浮点型数据Float和Double进行精确计算的问题
  16. 第 8 章 容器网络 - 053 - overlay 是如何隔离的?
  17. fastjson的日期格式化
  18. 10 Maven 版本管理
  19. centos 配置yum源
  20. Decorator模式 装饰器模式

热门文章

  1. 九度OJ 1179 阶乘(模拟)
  2. Wayland中的跨进程过程调用浅析
  3. OMR数据查询
  4. Java魔法堂:JVM的运行模式 (转)
  5. SQLite/嵌入式数据库
  6. C# Http以文件的形式上传文件
  7. C++内存管理学习笔记(6)
  8. codeforces 659B Qualifying Contest
  9. VC生成的DLL给QT的EXE调用时lib路径问题小结
  10. php 简易验证码(GD库)