簡體   English   中英

如何使用mybatis返回填充有多行的HashMap?

[英]How do I return a HashMap populated with multiple rows using mybatis?

我有一個包含三列的INSTR_ROUTING數據庫表: INSTR_ROUTING表

我的Java代碼是:

    public Map<String, String> getInstrumentRouting() {
    return getSqlSession().selectMap(NAMESPACE_PFX + "getInstrumentRouting", "INSTR_INSTANCE_NM");

和mybatis我嘗試了多種方法,最近兩種是:

    <select id="getInstrumentRouting" resultType="java.util.HashMap">
    select  INSTR_ROUTING_ID    as irId,
            INSTR_INSTANCE_NM   as instrumentName,      
            LAB_SYSTEM_NM       as destinationName  
    from INSTR_ROUTING
    WHERE INSTR_ROUTING_ID = #{irId, jdbcType=VARCHAR}
</select>

和:

    <resultMap id="instrumentRoutingMap" type="com.labcorp.adapter.di.common.InstrumentRoute">
    <result column="INSTR_INSTANCE_NM" property="instrumentName"
        jdbcType="VARCHAR" />
    <result column="LAB_SYSTEM_NM" property="queueInstanceName"
        jdbcType="VARCHAR" />
</resultMap>

<select id="getInstrumentRouting" resultMap="instrumentRoutingMap">
    select  INSTR_ROUTING_ID    as irId,
            INSTR_INSTANCE_NM   as instrumentName,      
            LAB_SYSTEM_NM       as destinationName  
    from INSTR_ROUTING
    WHERE INSTR_ROUTING_ID = #{irId, jdbcType=VARCHAR}
</select>

我所能得到的只是一個空的Map“ {}”或一個異常,例如說它找不到類型“ java.util.HashMap”。

誰能讓我挺直? 這應該很容易...

我得到了一個簡化的版本。 我將其更改為對於給定的INSTR_INSTANCE_NM僅獲得一個LAB_SYSTEM_NM(而不是加載整個表)。

<?xml version="1.0" encoding="UTF-8"?>

<select id="getDiInstanceName" resultType="String">
    select LAB_SYSTEM_NM as destinationName 
    from INSTR_ROUTING
    WHERE INSTR_INSTANCE_NM = #{instrumentName, jdbcType=VARCHAR}
</select>

    public String getDiInstanceNameFor(String instrumentName) {
    return getSqlSession().selectOne(NAMESPACE_PFX + "getDiInstanceName", instrumentName);
}

希望對別人有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM