I would like to iterate through a list, get the object's name and match that name with the record in my database. Then fetch that record.
I tried to use the foreach tag in Mybatis but I only get an empty list in return. Please help me out and point out if I have made any mistakes.
For reference, I have attached my Service implementation, DAO, mapper & the list class I'm using.
Service implementation
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");
DAO
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);
}
Mapper
<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);
}
}
I found a solution.
I am now getting the names from the List<PaymentCommon.CampaignsRSp> and making a simple List of those names. Then, I'm passing that list to the mapper, making it simple to iterate the list. Plus, I was missing the @Param annotation in my DAO, so I added that.
This approach gets me the data I want from my DB.