MyBatis provides great support with plenty of options for mapping the query results to JavaBeans. But sometimes, we may come across scenarios where we need to process the SQL query results by ourselves for special purposes. MyBatis provides ResultHandler plugin that enables the processing of the ResultSet in whatever way we like.

Suppose that we want to get the student details in a HashMap where stud_id is used as a key and name is used as a value.

For sqlSession.select() methods, we can pass an implementation of ResultHandler that will be invoked for each record in the ResultSet.

public interface ResultHandler {
void handleResult(ResultContext context);
}

Now let us see how we can use ResultHandler to process the ResultSet and return customized results.

public Map<Integer, String> getStudentIdNameMap() {
final Map<Integer, String> map = new HashMap<Integer, String>();
SqlSession sqlSession = MyBatisUtil.openSession();
try {
sqlSession.select(
"com.mybatis3.mappers.StudentMapper.findAllStudents",
new ResultHandler() {
@Override
public void handleResult(ResultContext context) {
Student student = (Student) context.getResultObject();
map.put(student.getStudId(), student.getName());
}
}
);
} finally {
sqlSession.close();
}
return map;
}

In the preceding code, we are providing an inline implementation of ResultHandler. Inside the handleResult() method, we are getting the current result object using context.getResultObject() that is a Student object because we configured resultMap="StudentResult" for the findAllStudents mapped statement. As the handleResult() method will be called for every row returned by the query, we are extracting the studId and name values from the Student object and populating the map.

MyBatis(3.2.3) - Custom ResultSet processing using ResultSetHandler的更多相关文章

  1. Table of Contents - MyBatis

    Getting Started with MyBatis Hello World Integration with Spring Bootstrapping MyBatis Configuring M ...

  2. 框架源码系列十二:Mybatis源码之手写Mybatis

    一.需求分析 1.Mybatis是什么? 一个半自动化的orm框架(Object Relation Mapping). 2.Mybatis完成什么工作? 在面向对象编程中,我们操作的都是对象,Myba ...

  3. MyBatis Generator使用com.mysql.cj.jdbc.Driver遇到的问题

    MyBatis Generator使用com.mysql.cj.jdbc.Driver Mybatis Generator 1.3.5 新建了一个decision库,并创建了一张user表 impor ...

  4. MyBatis Generator 详解 专题

    idea中有plugin可提高效率: http://www.henryxi.com/use-idea-mybatis-plugin-generate-mapper-files eg: <?xml ...

  5. Mybaits 源码解析 (八)----- 全网最详细,没有之一:结果集 ResultSet 自动映射成实体类对象(上篇)

    上一篇文章我们已经将SQL发送到了数据库,并返回了ResultSet,接下来就是将结果集 ResultSet 自动映射成实体类对象.这样使用者就无需再手动操作结果集,并将数据填充到实体类对象中.这可大 ...

  6. Mybatis源码详解系列(三)--从Mapper接口开始看Mybatis的执行逻辑

    简介 Mybatis 是一个持久层框架,它对 JDBC 进行了高级封装,使我们的代码中不会出现任何的 JDBC 代码,另外,它还通过 xml 或注解的方式将 sql 从 DAO/Repository ...

  7. 《Mybatis 手撸专栏》第7章:SQL执行器的定义和实现

    作者:小傅哥 博客:https://bugstack.cn - <手写Mybatis系列> 一.前言 为什么,要读框架源码? 因为手里的业务工程代码太拉胯了!通常作为业务研发,所开发出来的 ...

  8. 《Mybatis 手撸专栏》第9章:细化XML语句构建器,完善静态SQL解析

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 你只是在解释过程,而他是在阐述高度! 如果不是长时间的沉淀.积累和储备,我一定也没有 ...

  9. 《Mybatis 手撸专栏》第10章:使用策略模式,调用参数处理器

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 你这代码写的,咋这么轴呢! 说到轴,让我想起初中上学时老师说的话:"你那脑 ...

随机推荐

  1. MSXML读取XML中文

    // QueryNodes.cpp : Defines the entry point for the console application. // #include <stdio.h> ...

  2. iPhone 3GS/4 / 4s/5

    越努力.越幸福.----willingseal. 像素与分辨率有什么区别与联系 ????点击打开链接 像素和分辨率是成正比的,像素越大,分辨率也越高 像素 简单的说,我们通常所说的像素,就是CCD/C ...

  3. Spring Data JPA Tutorial Part Nine: Conclusions(未翻译)

    This is the ninth and the last part of my Spring Data JPA tutorial. Now it is time to take a look of ...

  4. spring对数据库特殊字段的支持

    1.CLOB <property name="tomdTemplateContent" type="org.springframework.orm.hibernat ...

  5. Lua学习笔记(五):面向对象的实现

    Lua本身是没有class之类的关键字的,但是我们可以巧妙利用function也是值和table的特性来实现面向对象的特性. 通过复制表的实现 Lua中的类也是一个table对象,下面我们看看一个简单 ...

  6. 转载“启动\关闭Oracle数据库的多种方法”--来自百度#Oracle

    启动\关闭Oracle数据库的多种方法 启动和关闭oracle有很多种方法. 这里只给出3种方法: l         Sql*plus l         OEM控制台 l         Wind ...

  7. Oracle数据库的版本变迁功能对比

    Oracle数据库自发布至今,也经历了一个从不稳定到稳定,从功能简单至强大的过程.从第二版开始,Oracle的每一次版本变迁,都具有里程碑意义. 1979年的夏季,RSI(Oracle公司的前身,Re ...

  8. Android ViewPager使用具体解释

    这是谷歌官方给我们提供的一个兼容低版本号安卓设备的软件包,里面包囊了仅仅有在安卓3.0以上能够使用的api.而viewpager就是当中之中的一个利用它,我们能够做非常多事情,从最简单的导航,到页面菜 ...

  9. iOS 2D绘图详解(Quartz 2D)之路径(stroke,fill,clip,subpath,blend)

    Stroke-描边 影响描边的因素 线的宽度-CGContextSetLineWidth 交叉线的处理方式-CGContextSetLineJoin 线顶端的处理方式-CGContextSetLine ...

  10. MySQL ddl丢表

      MySQL ddl丢表: MySQL server层为了和innodb层保持数据一致性,在写binlog和redo log时,引入了两阶段提交,但不同的变更产生的日志并非都使用这种策略. 下面就来 ...