我在Play Framework 2.3.6
Java项目中使用MyBatis 3.2.8.我已经挣扎了几天,迭代遍历复杂对象参数中的MyBatis映射器的整数列表.这是我的设置:
我在EventFilter.java中有一个名为EventFilter的类:
public class EventFilter {
private String beginDate;
private String endDate;
private List closestCountry;
private List territorialWaterStatus;
private List vesselCountry;
private String closestCountryInClause;
private String territorialWaterStatusInClause;
private String vesselCountryInClause;
public EventFilter() { }
public EventFilter(JsonNode jsonNode){
this.beginDate = jsonNode.get("beginDate").asText();
this.endDate = jsonNode.get("endDate").asText();
this.closestCountry = JsonHelper.arrayNodeToIntegerList((ArrayNode) jsonNode.get("closestCountry"));
this.territorialWaterStatus = JsonHelper.arrayNodeToIntegerList((ArrayNode) jsonNode.get("territorialWaterStatus"));
this.vesselCountry = JsonHelper.arrayNodeToIntegerList((ArrayNode) jsonNode.get("vesselCountry"));
}
public String getBeginDate() {
return beginDate;
}
public void setBeginDate(String beginDate) {
this.beginDate = beginDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public List getTerritorialWaterStatus() {
if(this.territorialWaterStatus.size() > 0) {
return territorialWaterStatus;
} else {
return null;
}
}
public void setTerritorialWaterStatus(List territorialWaterStatus) {
this.territorialWaterStatus = territorialWaterStatus;
}
public List getClosestCountry() {
if(this.closestCountry.size() > 0) {
return closestCountry;
} else {
return null;
}
}
public void setClosestCountry(List closestCountry) {
this.closestCountry = closestCountry;
}
public List getVesselCountry() {
if(this.vesselCountry.size() > 0) {
return vesselCountry;
} else {
return null;
}
}
public void setVesselCountry(List vesselCountry) {
this.vesselCountry = vesselCountry;
}
}
这在我的mybatis配置文件中被引用为类型别名:
我有一个mapper,它以EventFilter对象作为参数.然后应检查是否设置了beginDate,endDate,nearestCountry,vesselCountry和territorialWaterStatus.如果是,它将它们用于WHERE子句:
SELECT ev.id, to_char(ev.occurred_on, 'YYYY-MM-DD') AS occurred_on_date,
to_char(ev.occurred_on, 'HH24:MI:SS') AS occurred_on_time,
ST_X(ev.location) AS longitude, ST_Y(ev.location) AS latitude,
COALESCE(co01.name, 'Unspecified') AS closest_country,
COALESCE(co02.name, 'Unspecified') AS territorial_water_status,
COALESCE(co03.name, 'Unspecified') AS vessel_flag_country
FROM event AS ev
LEFT JOIN country AS co01
ON co01.cow_id = ev.location_closest_country_id
LEFT JOIN country AS co02
ON co02.cow_id = ev.location_water_status_country_id
LEFT JOIN country AS co03
ON co03.cow_id = ev.vessel_flag_country_id
ev.occurred_on >= #{eventFilter.beginDate}::timestamp AND ev.occurred_on <= #{eventFilter.endDate}::timestamp
AND ev.location_closest_country_id IN
#{id}
AND ev.location_water_status_country_id IN
#{id}
AND ev.vessel_flag_country_id IN
#{id}
ORDER BY ev.occurred_on ASC;
我将映射器链接到一个接口,如下所示:
public List getEventsWithFilter(@Param("eventFilter") EventFilter eventFilter);
我用MybatisMapper辅助类调用它来生成我的会话,如下所示:
public static List getEvents(EventFilter eventFilter) {
MybatisMapper mapper = new MybatisMapper();
SqlSession session = mapper.getSession();
EventMapper eventMapper = session.getMapper(EventMapper.class);
List events;
List li = eventFilter.getClosestCountry();
try {
events = eventMapper.getEventsWithFilter(eventFilter);
} finally {
session.close();
}
return events;
}
问题:
beginDate和endDate自己完全正常工作.但是我对整数列表有以下问题:
>检查列表是否为null的if语句似乎正在获取
忽略,或当它应该是假时评估为true.
>整数列表似乎作为null传递给IN子句.
>如果我注释掉整数列表IN子句,并且只是为了beginDate和
endDate,它完全正常.但是,如果我留下整数
列表IN子句,查询不会失败,但它返回一个空
设置,好像要说“WHERE列IN()”.
这是执行映射器/查询时由Play和Mybatis打印的控制台日志记录,以及EventFilter打印其内容.它们有点冗长所以我把它们放在pastebin中:
这比我想要的要长一点,但提前感谢任何帮助或建议.