大家都知道,GAE中是使用JDO来做持久层的。当初我在做自己的清风记账本的时候,去网上找了一些资料,但是国内用的人很少,找到的资料基本上少的可怜,先在将清风记账本这个应用中的JDO一些基本语法公布出来。
每个使用数据存储区的请求都新建一个 PersistenceManager 类的实例。使用 PersistenceManagerFactory 类的实例完成此操作。
PersistenceManagerFactory 实例的初始化需要时间。好在您对于应用程序只需一个实例,而该实例可存储在将由多个请求和多个类使用的静态变量中。简单的做法是为静态实例创建一个单独的包装器类。
一:增加:
/**
* 保存
*
* @param accBean 要保存的实体对象
*/
public void saveAccount(Pay accBean) {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
pm.makePersistent(accBean);
} finally {
pm.close();
}
}
二:查询
public List<Pay> qryAccount(int startNum, int endNum, User user) {
if (startNum > endNum) {
return null;
}
List<Pay> accountList2 = new ArrayList<Pay>();
List<Pay> accountList3 = new ArrayList<Pay>();
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = null;
try {
List<Pay> accountList = null;
pm.setDetachAllOnCommit(true);
//类似于参数
String queryTemplate = "",filter="";
String username = user.getEmail();
//替换的值
queryTemplate = "email == \"%s\" ";
filter = String.format(queryTemplate, username);
query = pm.newQuery(Pay.class,filter);
//排序
query.setOrdering("useDate desc");
accountList = (List<Pay>) query.execute();
// 管理员
if (!user.getEmail().equals("[email protected]")) {
for (Pay tempBean : accountList) {
if (tempBean.getUser().getEmail().equals(user.getEmail())) {
accountList2.add(tempBean);
}
}
} else { // 普通用户
accountList2.addAll(accountList);
}
//
if (accountList2 == null) {
return new ArrayList<Pay>();
}
if (endNum > accountList2.size()) {
endNum = accountList2.size();
}
accountList3.addAll(accountList2.subList(startNum, endNum));
if (accountList3 != null) {
return accountList3;
} else {
return new ArrayList<Pay>();
}
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
query.closeAll();
pm.close();
}
return accountList3;
}
三:统计:
public int qryAccCount(String email, String times) {
//List<Pay> accountList2 = new ArrayList<Pay>();
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = null;
try {
List<Pay> accountList = null;
pm.setDetachAllOnCommit(true);
String queryTemplate = "", filter = "";
//日期参数的传递
//日期参数支持>=,<=的方式
if(times != null && !times.equals("")) {
queryTemplate = "email == \"%s\" && useDate == today";
} else {
queryTemplate = "email == \"%s\" ";
}
filter = String.format(queryTemplate, email);
query = pm.newQuery(Pay.class, filter);
//查询结果排序方式
query.setOrdering("useDate desc");
if(times != null && !times.equals("")) {
//这里类似于声明,声明这个参数类型
query.declareImports("import java.util.Date");
query.declareParameters("Date today");
//格式化参数
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
Date today = null;
try {
today = format1.parse(times);
} catch (ParseException e) {
e.printStackTrace();
}
//执行查询
accountList = (List<Pay>) pm.newQuery(query).execute(today);
} else {
accountList = (List<Pay>) pm.newQuery(query).execute();
}
if (accountList != null)
return accountList.size();
else
return 0;
} finally {
pm.close();
}
}
多个参数的传递:
query.declareImports("import java.util.Date");
//开始日期--这里事先要声明
query.declareParameters("Date today, Date today2");
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
Date today = null;
try {
today = format1.parse(times);
} catch (ParseException e) {
e.printStackTrace();
}
//结束日期
//query.declareParameters("Date today2");
Date today2 = null;
try {
today2 = format1.parse(times2);
} catch (ParseException e) {
e.printStackTrace();
}
//查询结果排序方式
query.setOrdering("useDate desc,id desc");
accountList = (List<Pay>) query.executeWithArray(today, today2);
//这里可以将两个参数的值传入
四:删除
public void delAccount(long accountId) {
if (accountId <= 0) {
return;
}
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
Pay accBean = pm
.getObjectById(Pay.class, accountId);
pm.deletePersistent(accBean);
} finally {
pm.close();
}
}
注意:GAE不支持SUM的统计方法我用如下代码:
query.setResult("sum(price)");
List results = (List)query.execute();
就会直接报出错误信息,而根据JDO的描述,JDO是支持的!
010-11-26 13:06:40 com.google.appengine.tools.development.DevAppServerImpl start
信息: The server is running at http://localhost:8888/
org.datanucleus.store.appengine.query.DatastoreQuery$UnsupportedDatastoreOperatorException: Problem with query <SELECT sum(ID) FROM com.scholers.account.bean.Pay WHERE email == "
[email protected]">: App Engine datastore does not support operator sum.
at org.datanucleus.store.appengine.query.DatastoreQuery.validateResultExpression(DatastoreQuery.java:679)
at org.datanucleus.store.appengine.query.DatastoreQuery.validate(DatastoreQuery.java:618)
at org.datanucleus.store.appengine.query.DatastoreQuery.performExecute(DatastoreQuery.java:215)
at org.datanucleus.store.appengine.query.JDOQLQuery.performExecute(JDOQLQuery.java:89)
at org.datanucleus.store.query.Query.executeQuery(Query.java:1489)
at org.datanucleus.store.query.Query.executeWithArray(Query.java:1371)
at org.datanucleus.store.query.Query.execute(Query.java:1344)
at org.datanucleus.jdo.JDOQuery.execute(JDOQuery.java:221)