1.queryForObject

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

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

2.queryForList

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

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

3.queryForMap

  1. /**
  2. * Executes a mapped SQL SELECT statement that returns data to populate
  3. * a number of result objects that will be keyed into a Map.
  4. * <p/>
  5. * The parameter object is generally used to supply the input
  6. * data for the WHERE clause parameter(s) of the SELECT statement.
  7. *
  8. * @param id The name of the statement to execute.
  9. * @param parameterObject The parameter object (e.g. JavaBean, Map, XML etc.).
  10. * @param keyProp The property to be used as the key in the Map.
  11. * @return A Map keyed by keyProp with values being the result object instance.
  12. * @throws java.sql.SQLException If an error occurs.
  13. */
  14. 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. javascript学习初衷

    很久没有过来写东西了,由于要做小网页,介于不懂javascript,一味的去爬其他站点的代码下来,却不能自由组合,控制,达到自己想要的效果, 于是只能沉下心,javascript从头学起,还记得张老师 ...

  2. 去掉chrome记住密码后自动填充表单的黄色背景

    chrome表单自动填充后,input文本框的背景会变成黄色的,通过审查元素可以看到这是由于chrome会默认给自动填充的input表单加上input:-webkit-autofill私有属性,然后对 ...

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

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

  4. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 站点设置

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 站点设置         你应该熟悉(假设还咩有)的SharePo ...

  5. 优酷m3u8视频源地址获取失败

    昨天和今天上午,优酷站点视频全然没有办法播放,可是我是获取的优酷视频的视频原地址,所以app还是能够正常播放而且有下载功能.今天下午開始,优酷视频网页能够訪问了,可是视频原地址却不在了.我全部的app ...

  6. 专注UI——有用技术:模糊搜索

    在如今的项目中.须要做模糊搜索,在曾经技术的基础上非常快得完毕了第一版.大家先看看第一版的效果,我们一会做评论: 0基础: 我们可能部分源代码(附件中会有所有源代码) <span style=& ...

  7. 119 - Greedy Gift Givers

     Greedy Gift Givers  The Problem This problem involves determining, for a group of gift-giving frien ...

  8. GIS Tools for Hadoop 详细介绍

    2013年Esri美国在开发者大会上演示了GIS数据结合Hadoop分析的一个示例,这个示例赢得了听众的阵阵掌声,我们也许对GIS不是很陌生,但是对Hadoop却不是很清楚,其实Esri是用Java开 ...

  9. hdu2845(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2845 题意:给你一个n*m的矩阵,每个位置有一定数量的豆子,如果你去map[x][y]位置上的豆子,则 ...

  10. C++ Primer 学习笔记_79_模板与泛型编程 --模板编译模型

    模板与泛型编程 --模板编译模型 引言: 当编译器看到模板定义的时候,它不马上产生代码.仅仅有在用到模板时,假设调用了函数模板或定义了模板的对象的时候,编译器才产生特定类型的模板实例. 一般而言,当调 ...