一、自定义返回拦截器
package com.yaoex.crm.service.util; import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import java.util.HashMap;
import java.util.Map; /**
* @Title: MapResultHandler
* @Package: com.yaoex.crm.service.util
* @description:
**/ @Component
public class MapResultHandler implements ResultHandler {
private static final Logger log = LoggerFactory.getLogger(MapResultHandler.class); private static SqlSessionTemplate sqlSessionTemplate; @Autowired
public void setSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
MapResultHandler.sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
} @SuppressWarnings("rawtypes")
private final Map mappedResults = new HashMap(); @SuppressWarnings("unchecked")
@Override
public void handleResult(ResultContext context) {
@SuppressWarnings("rawtypes")
Map map = (Map) context.getResultObject();
if(map.get("key") != null && map.get("value") != null ){
// xml配置里面的property的值,对应的列
mappedResults.put(map.get("key"), map.get("value"));
}
} @SuppressWarnings("rawtypes")
public Map getMappedResults() {
return mappedResults;
} public static Map getMapResults(String statementId,Map<String,Object> param){
MapResultHandler handler = new MapResultHandler();
sqlSessionTemplate.select(statementId,param,handler);
return handler.getMappedResults();
}
} 二、mybatis配置
<resultMap id="levelCountMap" type="java.util.Map" >
<result column="cust_level" property="key" jdbcType="INTEGER" />
<result column="count" property="value" jdbcType="INTEGER" />
</resultMap>
<select id="selectCountByLevel" resultMap="levelCountMap">
SELECT l.cust_level,COUNT(l.cust_level) as count from t_crm_customer_level l
GROUP BY l.cust_level
</select>
三、调用方式
Map<Integer,Long> results = MapResultHandler.getMapResults("com.yaoex.crm.dao.customerdata.CrmCustomerLevelDao.selectCountByLevel", null);

自定义Mybatis返回类型及注意事项的更多相关文章

  1. MyBatis 返回类型resultType为map时的null值不返回问题

    问题一:    查询结果集中 某字段 的值为null,在map中不包含该字段的key-value对 解决:在mybatis.xml中添加setting参数 <!-- 在null时也调用 sett ...

  2. mybatis 返回类型为 java.lang.String 接收为null的情景

    <select id="selectOnly" parameterType="java.util.Map" resultType="java.l ...

  3. Mybatis下配置调用Oracle自定义函数返回的游标结果集

    在ibatis和Mybatis对存储过程和函数函数的调用的配置Xml是不一样的,以下是针对Mybatis 3.2的环境进行操作的. 第一步配置Mapper的xml内容 <mapper names ...

  4. 云笔记项目-MyBatis返回自增类型&堆栈对象补充理解

    在云笔记项目中,讲到了MySql的自增,MyBatis查询到自增类型数据后可以设置返回到参数属性,其中学习了MySql的自增写法,堆栈对象等知识. MySql数据类型自增 建立一张Person表,其中 ...

  5. MyBatis使用自定义TypeHandler转换类型的实现方法

    From: http://www.manongjc.com/article/15577.html 这篇文章主要介绍了MyBatis使用自定义TypeHandler转换类型的实现方法,本文介绍使用Typ ...

  6. 使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,报异常的解决方法

    使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,会报异常. 例如: <select id="getPersonRecordId" parameterTy ...

  7. MyBatis使用自定义TypeHandler转换类型

    MyBatis虽然有很好的SQL执行性能,但毕竟不是完整的ORM框架,不同的数据库之间SQL执行还是有差异. 笔者最近在升级 Oracle 驱动至 ojdbc 7 ,就发现了处理DATE类型存在问题. ...

  8. mybatis中查询结果为空时不同返回类型对应返回值

    今天在别人的代码基础上实现新需求,看到对于mybatis查询结果的判断不是很正确,如果查询结果为空就会异常,不知道大家有没有这样的疑惑:mybatis中resultType有多种返回类型,对于每种不同 ...

  9. mybatis返回map类型数据空值字段不显示的解决方法

    在日常开发中,查询数据返回类型为map,数据库中有些自动值为null,则返回的结果中没有值为空的字段,则如何显示值为空的字段呢? Spring boot + MyBatis返回map中null值默认不 ...

随机推荐

  1. compute the su procedure time with python

    #!/usr/bin/python2.6 import re,datetime file_name='sim.log' file=open(file_name,'r') acnum=[];time_r ...

  2. select version();desc mysql.user;

    D:\wamp64\wamp\bin\mysql\mysql5.6.17\bin>mysql -hgoDev -uroot -ppasswordWarning: Using a password ...

  3. ubuntu下配置rails环境遇到的错误

    1.Could not find gem 'sqlite3 (>= 0)' in any of the gem sources listed in your Gemfile 解决:sudo ge ...

  4. SVN版本控制中.a无法提交问题

    1.首先xcode是默认忽略.a文件的.改变方法如下: 1⃣️. 打开终端,  在命令行中输入: vi ~/.subversion/config  来打开配置文件.2⃣️. 然后, 在[miscell ...

  5. [noip2014day1-T2]联合权值

    无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 Wi,每条边的长度均为 1.图上两点(u, v)的距离定义为 u 点到 v 点的最短距离.对于图 G ...

  6. android系统启动框架、Activity界面显示过程详解

    一.Android系统框架 android的系统架构和其操作系统一样,采用了分层的架构.从架构图看,android分为四个层,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心 ...

  7. Block和inline元素对比

    所有的HTML元素都属于block和inline之一.block元素的特点是:总是在新行上开始:高度,行高以及顶和底边距都可控制:宽度缺省是它的容器的100%,除非设定一个宽度<div>, ...

  8. x264 --fullhelp

    >x264 --fullhelp x264 core: Syntax: x264 [options] -o outfile infile Infile can be raw (in which ...

  9. spark uniq 本质上就是单词计数

    粗体部分示例: # dns_domain_info_list_rdd ==> [(src_ip, domain, domain_ip, timestamp, metadataid), ....] ...

  10. Power Strings--KMP

    https://cn.vjudge.net/problem/POJ-2406 上面是比赛链接. 题目意思很明确,问最多是多少个子串连接而成的? 这个需要用到KMP,很好的理解KMP的Next数组.Ne ...