Google App Engine List type Query
Google App Engine 很大的一个特点,就是增加了默认对于 ListType的支持。而且,我们可以通过ListType进行查询
,单元测试代码:
@Test
public void testListQuery(){
DatastoreService dataSvc = DatastoreServiceFactory.getDatastoreService();
Entity en = new Entity("TestList");
List<String> listProp = Arrays.asList("t1","t2","t3","t4");
en.setProperty("list", listProp);
dataSvc.put(en);
Entity en2 = new Entity("TestList");
List<String> listProp2 = Arrays.asList("t1","t3","t5","t7");
en2.setProperty("list", listProp2);
dataSvc.put(en2);
//list equals sign items
Query q = new Query("TestList");
q.addFilter("list", FilterOperator.EQUAL, "t1");
q.addFilter("list", FilterOperator.EQUAL, "t3");
List ls = dataSvc.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertEquals(2,ls.size());
//list in list
Query q2 = new Query("TestList");
q2.addFilter("list", FilterOperator.IN, listProp2);
List ls2 = dataSvc.prepare(q2).asList(FetchOptions.Builder.withDefaults());
assertEquals(2,ls2.size());
}
public void testListQuery(){
DatastoreService dataSvc = DatastoreServiceFactory.getDatastoreService();
Entity en = new Entity("TestList");
List<String> listProp = Arrays.asList("t1","t2","t3","t4");
en.setProperty("list", listProp);
dataSvc.put(en);
Entity en2 = new Entity("TestList");
List<String> listProp2 = Arrays.asList("t1","t3","t5","t7");
en2.setProperty("list", listProp2);
dataSvc.put(en2);
//list equals sign items
Query q = new Query("TestList");
q.addFilter("list", FilterOperator.EQUAL, "t1");
q.addFilter("list", FilterOperator.EQUAL, "t3");
List ls = dataSvc.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertEquals(2,ls.size());
//list in list
Query q2 = new Query("TestList");
q2.addFilter("list", FilterOperator.IN, listProp2);
List ls2 = dataSvc.prepare(q2).asList(FetchOptions.Builder.withDefaults());
assertEquals(2,ls2.size());
}
1 . List Equals one : 只需要这个List中的任何一项对应到 one的值,就可以了(List 的item只支持最基本的类型)
2 . List in List : 相当于 只需要值List 和 查询参数List之间,只要有任意一项相等(equals 为true),就认为匹配。所以返回2