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訪问。

Ibatis之3个不经常使用的Query方法的更多相关文章

  1. MyBatis笔记----报错:Exception in thread "main" org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)解决方法

    报错 Exception in thread "main" org.apache.ibatis.binding.BindingException: Invalid bound st ...

  2. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 问题解决方法

    在用maven配置mybatis环境时出现此BindingExceptiony异常,发现在classes文件下没有mapper配置文件,应该是maven项目没有扫描到mapper包下的xml文件,在p ...

  3. ibatis中使用List作为传入参数的使用方法及 CDATA使用

    ibatis中list做回参很简单,resultClass设为list中元素类型,dao层调用: (List)getSqlMapClientTemplate().queryForList(" ...

  4. ibatis实战之OR映射

    相对Hibernate等ORM实现而言,ibatis的映射配置更为简洁直接,以下是一个典型的配置文件. <?xml version="1.0" encoding=" ...

  5. ibatis源码学习1_整体设计和核心流程

    背景介绍ibatis实现之前,先来看一段jdbc代码: Class.forName("com.mysql.jdbc.Driver"); String url = "jdb ...

  6. iBatis框架使用 4步曲

    iBatis是一款使用方便的数据訪问工具,也可作为数据持久层的框架.和ORM框架(如Hibernate)将数据库表直接映射为Java对象相比.iBatis是将SQL语句映射为Java对象. 相对于全自 ...

  7. Ibatis之RowHandler

    如果一个场景:账户表中有1千万账户,现在,我们需要这个1千万账户利息结算业务.需求是基于Ibatis框架来实现这个功能. 如果按照一般的编程模式,我们将编写一个sql,然后调用QueryForList ...

  8. ibatis和mybatis中的BatchExecutor

    ibatis中的的处理方法 spring集成了ibatis的批量提交的功能,我们只要调用API就可以了 首先在你的dao中需要继承org.springframework.orm.ibatis.supp ...

  9. springboot项目下的Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

    今天遇到mybatis-puls的报错Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (n ...

随机推荐

  1. OpenJDK1.8.0 源码解析————HashMap的实现(一)

    HashMap是Java Collection Framework 的重要成员之一.HashMap是基于哈希表的 Map 接口的实现,此实现提供所有可选的映射操作,映射是以键值对的形式映射:key-v ...

  2. 为VisualSVN Server增加在线修改用户密码的功能

    原文:为VisualSVN Server增加在线修改用户密码的功能 附件下载:点击下载 VisualSVN Server是一个非常不错的SVN Server程序,方便,直观,用户管理也异常方便. 不过 ...

  3. 2014年Windows平台软件推荐:神器小工具(骨灰级

    原文  http://www.wtoutiao.com/a/120621.html 底层工具 “If you know how to use Process Monitor competently, ...

  4. 300M无线路由器 TL-WR842N - TP-LINK官方网站

    300M无线路由器 TL-WR842N - TP-LINK官方网站 300M无线路由器TL-WR842N 11N无线技术.300Mbps无线速率 2x2MIMO架构.CCA技术,提升无线稳定性.扩大无 ...

  5. python大文件迭代器的流式读取,之前一直使用readlines()对于大文件可以迅速充满内存,之前用法太野蛮暴力,要使用xreadlines或是直接是f,

    #!/usr/bin/env python #encoding=utf-8 import codecs count =0L #for line in file("./search_click ...

  6. Python学习入门基础教程(learning Python)--5.3 Python写文件基础

    前边我们学习了一下Python下如何读取一个文件的基本操作,学会了read和readline两个函数,本节我们学习一下Python下写文件的基本操作方法. 这里仍然是举例来说明如何写文件.例子的功能是 ...

  7. 解压tar.gz文件报错gzip: stdin: not in gzip format解决方法

    解压tar.gz文件报错gzip: stdin: not in gzip format解决方法 在解压tar.gz文件的时候报错 1 2 3 4 5 [Sun@localhost Downloads] ...

  8. C语言里为何会有“2+2=5”的结果

    写这篇原创文章是由于看到了极客中的一篇文章<有趣各种编程语言实现2+2=5>,当中C语言是这样实现的: int main() { char __func_version__[] = &qu ...

  9. cmake编译时遇到的问题解决

    编译cmake首先须要gcc环境,能够运行 gcc --version命令看看. 假设没有,能够使用yum或从cd中进行安装,此处是在虚拟机中从cd中进行安装.将cd链接到虚拟机都会吧,此处略去,.. ...

  10. Indiegogo: An International Crowdfunding Platform to Raise Money

    Indiegogo: An International Crowdfunding Platform to Raise Money The world's funding platform. Fund ...