我想讓這個複雜的對象與Mybatis。Mybatis收集呼叫程序與輸出參數
public class UserInfo {
public Integer clientId;
public String userName;
public List<Device> deviceList;
}
前兩場返回一個過程:
procedure getUserInfo(pClientId in number, pRes out sys_refcursor);
三場(DEVICELIST)返回另一個步驟:
procedure getDevices(pClientId in number, pRes out sys_refcursor);
我的Java代碼:
SqlSession session = sqlSessionFactory.openSession();
mapper = session.getMapper(UserInfoMapper.class);
Map<String, Object> params = new HashMap<String, Object>();
ResultSet rs = null;
params.put("clientId", clientId);
params.put("result", rs);
mapper.getUserInfo(params);
info = ((ArrayList<UserInfo>)params.get("result")).get(0);
UserInfoMapper:
public interface UserInfoMapper {
void getUserInfo(Map<String, Object> params);
}
如何編寫UserInfoMapper.xml以解決我的問題? 現在看起來是這樣的:
<mapper namespace="UserInfoMapper">
<resultMap id="UserInfoResult" type="UserInfo">
<id property="clientId" column="clientId"/>
<result property="userName" column="userName"/>
<collection property="deviceList" ofType="Device"
column="clientId=clientId" select="getDeviceList"/>
</resultMap>
<resultMap id="DeviceListResult" type="Device">
<result property="deviceName" column="DEVICE_NAME"/>
</resultMap>
<select id="getUserInfo" statementType="CALLABLE" parameterType="java.util.Map">
CALL MESSAGE_SERVER.getUserInfo(
#{clientId, mode=IN},
#{result, jdbcType=CURSOR, javaType=java.sql.ResultSet, mode=OUT, resultMap=UserInfoResult}
)
</select>
<select id="getDeviceList" statementType="CALLABLE" parameterType="java.util.Map" >
CALL MESSAGE_SERVER.getDevices(
#{clientId, mode=IN},
#{result, jdbcType=CURSOR, javaType=java.sql.ResultSet, mode=OUT, resultMap=DeviceListResult}
)
</select>
</mapper>
的問題特別感興趣:如何收集選擇結果得到的最終目標?如何使這個對象鏈接?
現在,結果是前兩個字段成功返回。但deviceList是空的。
忘記聲明ResultSet,這是實現細節,只是'params.put(「result」,null);'和'result'鍵甚至可能不需要存在。使用List而不是ArrayList:變量聲明的類型必須儘可能抽象。 – blackwizard