我想遍历一个列表,获取对象的名称并将该名称与我数据库中的记录匹配。然后获取该记录。
我尝试在 Mybatis 中使用 foreach 标签,但我只得到一个空列表作为回报。请帮助我并指出我是否犯了任何错误。
作为参考,我附上了我的服务实现、DAO、映射器和我正在使用的列表类。
服务实施
String jsonStr = "[{\"name\":\"first \",\"value\":\"1 \"},{\"name\":\"second\",\"value\":\"2\"},{\"name\":\"third\",\"value\":\"3\"},{\"name\":\"fourth\",\"value\":\"4\"}]";
List<PaymentCommon.CampaignsRsp> list = null;
try { // creating a list from the json String
list = mapper.readValue(jsonStr, new TypeReference<List<PaymentCommon.CampaignsRsp>>(){});
} catch (IOException e) {
e.printStackTrace();
}
if(JudgeUtils.isNotEmpty(rsp.getCampaigns())) {
listRsp = cmmClmWhitelistingDao.getCampaigns(list); // calling the method
}
else System.out.println("LIST IS EMPTY");
道
package com.hisun.lemon.cmm.dao;
import com.hisun.lemon.cio.dto.common.PaymentCommon;
import com.hisun.lemon.cmm.entity.CmmClmCampaignDO;
import com.hisun.lemon.framework.dao.BaseDao;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface ICmmClmWhitelistingDao extends BaseDao {
List<CmmClmCampaignDO> getCampaigns(List<PaymentCommon.CampaignsRsp> campaignList);
}
映射器
<select id="getCampaigns" resultMap="BaseResultMap" >
SELECT campaign.campaign_id, campaign.campaign_name,
FROM clm_campaign campaign
WHERE campaign.campaign_name IN
<foreach item="id" collection="list" open="[{" separator="},{" close="}]" >
#{id.name}
</foreach>
</select>
PaymentCommon.class
@Json
public static class CampaignsRsp {
@Item
private String name;
@Item
private String value;
public CampaignsRsp() {
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}