mybatis select返回值为map时,选取表字段的两列作为key,value

6 篇文章 0 订阅

mybatis select返回值为map时,选取表字段的两列作为key,value

一:数据格式确定

在做项目时,遇见这样的需求,统计每部部门下的设备数量,因为后台需要对该数据进行二次处理,所以如果dao层返回List格式的数据则后台需要对该数据进行遍历,而如果只返回Map数据的话,则会使得后台代码简洁,并且提高程序效率。即需要返回这样的数据格式:

部门编号数量1数量2数量3
部门A1019
部门B20218

在按照此数据格式进行实施的时,发现Map只有 key、value 只能返回两个字段,所有对sql语句进行处理,将其余字段通过 concat(数量1,’,’,数量2,’,’,数量3) 连接,则现在的数据格式为

部门编号数量
部门A10,1,9
部门B20,2,18

在上表中 部门编号代表Map的 key ,数量代表Map的 value

二:XxxMapper.xml 书写

定义resultMap

  <resultMap id="mapResultMap"   type="HashMap">  
    <result property="key" column="id" jdbcType="VARCHAR" javaType="java.lang.String" />  
    <result property="value" column="value" javaType="java.lang.String" jdbcType="VARCHAR" />  
  </resultMap> 

查询语句

注:该mapper中使用的case when 语法进行统计分析,使用concat拼接结果集字段

<select id="selectXxxxNum" resultMap="mapResultMap">
    select 
    id as id ,
    concat(count(device_id) ,',',
    sum(case when  customer_id is not null and customer_id != '' then 1 else 0 end),',',
    sum(case when customer_id is null or customer_id = '' then 1 else 0 end) ) as value
    from sys_device_info 
    where is_effective = true
    group by id
  </select>

ResultHandler.java 书写

package cn.ja.rome.mybatis;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.ResultContext;

/**
 * 用于转化结果集
 * 
 * @author jiangliuhong
 */
public class ResultHandler implements org.apache.ibatis.session.ResultHandler {

    @SuppressWarnings("rawtypes")
    private final Map mappedResults = new HashMap();

    @SuppressWarnings("unchecked")
    @Override
    public void handleResult(ResultContext context) {
        @SuppressWarnings("rawtypes")
        Map map = (Map) context.getResultObject();
        mappedResults.put(map.get("key"), map.get("value")); // xml配置里面的property的值,对应的列
    }

    @SuppressWarnings("rawtypes")
    public Map getMappedResults() {
        return mappedResults;
    }

}

SessionMapper.java 书写

package cn.ja.rome.dao;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import cn.ja.rome.mybatis.ResultHandler;

/**
 * 用于session查询
 * 
 * @author jiangliuhong
 */
@Repository
public class SessionMapper extends SqlSessionDaoSupport {

    @Resource
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
    }

    /**
     * @return
     */
    @SuppressWarnings("unchecked")
    public Map<String,Object> selectXxxNum(){
        ResultHandler handler = new ResultHandler();
        //namespace : XxxMapper.xml 中配置的地址(XxxMapper.xml的qualified name)
        //.selectXxxxNum : XxxMapper.xml 中配置的方法名称
        //this.getSqlSession().select(namespace+".selectXxxxNum", handler);
        this.getSqlSession().select(XxxMapper.class.getName()+".selectXxxxNum", handler);
        Map<String, Object> map = handler.getMappedResults();  
        return map;
    }
}

运行

配置完成,最后在service中注入SessionMapper

@Autowired
private SessionMapper sessionMapper;
public Map<String,Object> test(){
    return sessionMapper.selectXxxNum();
}
  • 12
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值