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

直接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);

 

上一篇:4. ARM中断处理实现


下一篇:netty+websocket模式下token身份验证遇到的问题