如何使用 mybatis return 填充多行的 HashMap?
How do I return a HashMap populated with multiple rows using mybatis?
我有一个 INSTR_ROUTING 数据库 table 三列:
我的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>
我所能得到的只是一个空映射“{}”或一个异常,例如说找不到类型 "java.util.HashMap".
任何人都可以纠正我吗?这应该很容易...
我得到了一个简化版本。我将其更改为仅获取给定 INSTR_INSTANCE_NM 的 LAB_SYSTEM_NM(而不是加载整个 table)。
<?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);
}
希望对其他人有所帮助。
我有一个 INSTR_ROUTING 数据库 table 三列:
我的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>
我所能得到的只是一个空映射“{}”或一个异常,例如说找不到类型 "java.util.HashMap".
任何人都可以纠正我吗?这应该很容易...
我得到了一个简化版本。我将其更改为仅获取给定 INSTR_INSTANCE_NM 的 LAB_SYSTEM_NM(而不是加载整个 table)。
<?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);
}
希望对其他人有所帮助。