Ibatis之3个不经常使用的Query方法
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方法的更多相关文章
- 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 ...
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 问题解决方法
在用maven配置mybatis环境时出现此BindingExceptiony异常,发现在classes文件下没有mapper配置文件,应该是maven项目没有扫描到mapper包下的xml文件,在p ...
- ibatis中使用List作为传入参数的使用方法及 CDATA使用
ibatis中list做回参很简单,resultClass设为list中元素类型,dao层调用: (List)getSqlMapClientTemplate().queryForList(" ...
- ibatis实战之OR映射
相对Hibernate等ORM实现而言,ibatis的映射配置更为简洁直接,以下是一个典型的配置文件. <?xml version="1.0" encoding=" ...
- ibatis源码学习1_整体设计和核心流程
背景介绍ibatis实现之前,先来看一段jdbc代码: Class.forName("com.mysql.jdbc.Driver"); String url = "jdb ...
- iBatis框架使用 4步曲
iBatis是一款使用方便的数据訪问工具,也可作为数据持久层的框架.和ORM框架(如Hibernate)将数据库表直接映射为Java对象相比.iBatis是将SQL语句映射为Java对象. 相对于全自 ...
- Ibatis之RowHandler
如果一个场景:账户表中有1千万账户,现在,我们需要这个1千万账户利息结算业务.需求是基于Ibatis框架来实现这个功能. 如果按照一般的编程模式,我们将编写一个sql,然后调用QueryForList ...
- ibatis和mybatis中的BatchExecutor
ibatis中的的处理方法 spring集成了ibatis的批量提交的功能,我们只要调用API就可以了 首先在你的dao中需要继承org.springframework.orm.ibatis.supp ...
- 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 ...
随机推荐
- ListView数据动态刷新
在Android开发中用到ListView时,经常遇到要更改ListView内容的情形,比如删除或增加ListView中显示的条目,这里给大家提供一下思路:不论ListView要显示的对象是什么(如: ...
- Delphi的指针 good
Pointers are like jumps, leading wildly from one part of the data structure to another. Their introd ...
- ThinkPhp学习03
原文:ThinkPhp学习03 一.ThinkPHP 3 的输出 (重点) a.通过 echo 等PHP原生的输出方式在页面中输出 b.通过display方法输出 想分配变量可以使用as ...
- ThinkPHP框架配置自定义的模板变量(十)
原文:ThinkPHP框架配置自定义的模板变量(十) 模板替换(手册有详细介绍对应的目录) __PUBLIC__:会被替换成当前网站的公共目录 通常是 /Public/ __ROOT__: 会替换成当 ...
- 慎得慌风 656ik67o
http://photo.163.com/q/7634581 http://photo.163.com/q/7634580 http://photo.163.com/q/7634577 http:// ...
- sprintf,多少钱你知道?
选<CSDN 社区电子杂志——C/C++杂志>http://emag.csdn.net 2005 年1 月 总号1 期 - 93 -笔者:steedhorse(晨星)printf 可能是很 ...
- sql连接错误(Microsoft SQL Server,错误:2)
昨天用SQL语句建表的时候写了一段代码,对于代码的逻辑和内容我不太肯定对不正确.反正是毫不犹豫的让它运行了,过程中出现好几个错误,当时没有太在意,想着大不了出错了再重写一个.结果--玩坏了,从昨天到如 ...
- UNIX高级环境编程1
UNIX高级环境编程1 故宫角楼是很多摄影爱好者常去的地方,夕阳余辉下的故宫角楼平静而安详. 首先,了解一下进程的基本概念,进程在内存中布局和内容. 此外,还需要知道运行时是如何为动态数据结构(如链表 ...
- 使用 sphinx 制作简洁而又美观的文档
使用 sphinx 制作简洁而又美观的文档 使用 sphinx 制作简洁而又美观的文档
- Makefile分析基础
http://blog.csdn.net/ghostyu/article/details/6866863 在我前一篇写的[ linux驱动学习(一)Makefile基础]中,Makefile写的中规中 ...