ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

mapper.xml select 查询返回map,字段一为key,字段二为value

2021-12-10 15:02:16  阅读:122  来源: 互联网

标签:xml mapper Map map 字段 handler import public


直接resultType 可以为map 但是 是以查询的字段作为key, 值为value,有时候想让值为key-value,所以还是有缺陷

 

编写select 普通查询语句

    <select id="findOrderWaitissue" parameterType="list" resultMap="getAllSetDaysResult">
        SELECT FPQQLSH SP_FPARAMEKEY,GROUP_CONCAT(ddh) SP_FPARAMEVALUE FROM `t_inv_orders_waitissue`
        where FPQQLSH in
         <foreach collection="list" item="item" separator="," open="(" close=")">
             #{item.fpqqlsh}
         </foreach>
          GROUP BY FPQQLSH
    </select>

定义resultMap ,在里面对字段设置key,value

    <resultMap id="getAllSetDaysResult"   type="HashMap">
        <result property="key" column="SP_FPARAMEKEY" />
        <result property="value" column="SP_FPARAMEVALUE" />

    </resultMap>

 

创建二配置个类


package com.cqaisino.newbill.common.handler;

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

import javax.annotation.Resource;

import com.cqaisino.newbill.output.entity.InvInvoiceExcel;
import com.cqaisino.newbill.output.mapper.InvInvoiceMapper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

@Repository public class SessionMapper extends SqlSessionDaoSupport { @Resource public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { super.setSqlSessionFactory(sqlSessionFactory); } /** * @return */ @SuppressWarnings("unchecked") public Map<String,Object> findOrderWaitissue(List<InvInvoiceExcel> invInvoiceExcels){ FblMapResultHandler handler = new FblMapResultHandler(); //namespace : XxxMapper.xml 中配置的地址(XxxMapper.xml的qualified name) //.selectXxxxNum : XxxMapper.xml 中配置的方法名称 //this.getSqlSession().select(namespace+".selectXxxxNum", handler); this.getSqlSession().select(InvInvoiceMapper.class.getName()+".findOrderWaitissue",invInvoiceExcels, handler); Map<String, Object> map = handler.getMappedResults(); return map; } }

 

 

package com.cqaisino.newbill.common.handler;

import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;

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

public class FblMapResultHandler implements 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的值,对应的列
    }
    public Map getMappedResults() {  
        return mappedResults;  
    }  
}

 

然后直接注入mapper  使用

    @Autowired
    private SessionMapper invoiceMapMapper;
        Map<String, Object> orderWaitissue = this.invoiceMapMapper.findOrderWaitissue(invInvoiceExcels);

 

标签:xml,mapper,Map,map,字段,handler,import,public
来源: https://www.cnblogs.com/hylr/p/15670665.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有