寻找使用java对象的查询语言。找到MQL(骡子查询语言)。 它处于测试阶段,我没有找到太多文档。尝试过where子句和select子句之类的东西。但订单条款不起作用。
List users = new ArrayList();
users.add(new User("Dan", "Dan.Diephouse@gmail.com",2,13000 , address));
address = new Address("International pkway","Atlatna","GA","USA");
users.add(new User("Joe", "Joe.Sales@yahoo.com", 1,14000, address));
users.add(new User("John", "John@yahoo.com", 1,16000, address));
users.add(new User("Scott", "scott@yahoo.com", 1,15000, address));
users.add(new User("Andy", "andy@abc.com", 1,7000, address));
Query query = new QueryBuilder()
.as("p")
.orderby("income")
// .max(3)
.where(eq(property("companyId"), 1))
.select(newObject()
.set("name", "name")
.set("income", "income")
.set("email", "email")).build();
Collection result1 = query.execute(users);
请让我知道,如果任何人有运气玩MQL或建议任何其他好的框架来查询java对象。
Another error – when the result set is not hashmap.
Exception in thread “main” java.lang.ClassCastException: com.mql.test.User cannot be cast to java.util.Map
at com.mulesoft.mql.impl.OrderByComparator.compare(OrderByComparator.java:11)
at java.util.Arrays.mergeSort(Arrays.java:1270)
at java.util.Arrays.sort(Arrays.java:1210)
at java.util.Collections.sort(Collections.java:159)
at com.mulesoft.mql.Query.order(Query.java:214)
at com.mulesoft.mql.Query.execute(Query.java:189)
List persons = getPersons();
Query query = new QueryBuilder()
// .where(and(eq(property(“division”), “Sales”),
// eq(property(“firstName”), “Joe”)))
.orderby(“income”)
.max(3)
.build();
答案 0 :(得分:0)
我查看了mql源代码。我们在OrderByComparator
类中有一个错误(拼写错误)。
Object r1 = MVEL.executeExpression(expression, o1.get(queryBuilder.getAs()), o1);
Object r2 = MVEL.executeExpression(expression, o1.get(queryBuilder.getAs()), **o1)**;
应该是:
Object r2 = MVEL.executeExpression(expression, o1.get(queryBuilder.getAs()), **o2);**
答案 1 :(得分:0)
public int compare(Object o1, Object o2) {
Object r1 = null, r2 = null;
if(o1 instanceof Map && o2 instanceof Map) {
Map<String,Object> o1Map = (Map<String,Object>) o1;
Map<String,Object> o2Map = (Map<String,Object>) o2;
r1 = MVEL.executeExpression(expression,o1Map.get(queryBuilder.getAs()), o1Map);
r2 = MVEL.executeExpression(expression, o2Map.get(queryBuilder.getAs()), o2Map);
}else {
r1 = MVEL.executeExpression(expression, o1);
r2 = MVEL.executeExpression(expression, o2);
}
if (r1 instanceof Comparable && r2 instanceof Comparable) {
return ((Comparable)r1).compareTo(r2);
}
if (r1 == null && r2 == null) {
return 0;
}
if (r1 == null) {
return -1;
}
if (r2 == null) {
return 1;
}
return r1.toString().compareTo(r2.toString());
}