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 ...
随机推荐
- Swift - 操作SQLite数据库(引用SQLite3库)
SQLite轻量级数据库在移动应用中使用非常普遍,但是目前的库是C编写的,为了方便使用,对SQLite相关的操作用Swift进行了封装.这个封装代码使用了一个开源项目SQLiteDB,地址是:http ...
- Spring4 MVC 多文件上传(图片并展示)
开始需要在pom.xml加入几个jar,分别是 <dependency> <groupId>commons-fileupload</groupId> <art ...
- Java魔法堂:JVM的运行模式 (转)
一.前言 JVM有Client和Server两种运行模式.不同的模式对应不同的应用场景,而JVM也会有相应的优化.本文将记录JVM模式的信息,以便日后查阅. 二.介绍 在$JAVA_HOME/jre/ ...
- Android常用控件之RatingBar的使用
RatingBar控件比较常见就是用来做评分控件,先上图看看什么是RatingBar 在布局文件中声明 <?xml version="1.0" encoding=" ...
- [IDEs]Eclipse设置花括号样式
用惯Vistual Studio,在使用Eclipse时发现有很多东西还是挺不习惯,第一个就要解决花括号的样式 步骤: 1.Windows->Preferences->Java->C ...
- Mod in math
An Introduction to Modular Math When we divide two integers we will have an equation that looks like ...
- poj-3791-An Easy Game-记忆化搜索
dp[i][j]:还有i个不同样的位置,还能走j步,一共同拥有多少种走法. 非常明显 dp[i][j]=sigm(dp[i-k][j-1]*c[i][k]*c[n-i][m-k]); 用记忆化搜索记忆 ...
- UVa-Palindromes
题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- R12 付款过程请求-功能和技术信息 (文档 ID 1537521.1)
In this Document Abstract History Details _afrLoop=2234450430619177&id=1537521.1&dis ...
- I2C操作笔记——以 AT24C04为例
1.前言 对于大多数project师而言,I2C永远是一个头疼的问题.相比UART和SPI而言,I2C的时序要复杂一些,I2C组合变化也丰富一些.在这里以AT24C04为例说明I2C使用过程中 ...