gpt4 book ai didi

GWT-Objectify : basic

转载 作者:行者123 更新时间:2023-12-04 06:31:26 25 4
gpt4 key购买 nike

我已经阅读了一些文档,但还无法与数据存储区进行通信……谁能给我一个在 GWT Web 应用程序中使用的 objectify 的示例项目/代码(我使用 eclipse)……只是一个简单的“使用 RPC 执行 put' 和 'get' 操作应该可以……或者,至少告诉我它是如何完成的

最佳答案

了解如何使对象化工作的最简单方法是重复 this article 中描述的所有步骤。来自大卫的钱德勒博客。如果您对 GWT、GAE(Java)、gwt-presenter、gin\guice 等感兴趣,整个博客几乎是必读的。在那里你会找到工作示例,但无论如何在这里我将展示一个稍微高级的示例。

包装内 shared定义您的实体/模型:

import javax.persistence.Embedded;
import javax.persistence.Id;
import com.google.gwt.user.client.rpc.IsSerializable;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Unindexed;

@Entity
public class MyEntry implements IsSerializable {
// Objectify auto-generates Long IDs just like JDO / JPA
@Id private Long id;
@Unindexed private String text = "";
@Embedded private Time start;

// empty constructor for serialization
public MyEntry () {
}
public MyEntry (Time start, String text) {
super();
this.text = tText;
this.start = start;
}
/*constructors,getters,setters...*/
}

时间类(也是 shared 包)只包含一个字段毫秒:
@Entity  
public class Time implements IsSerializable, Comparable<Time> {
protected int msecs = -1;
//rest of code like in MyEntry
}

复制类 ObjectifyDao从上面的链接到您的 server.dao包裹。然后专门为 MyEntry 制作 DAO 类——MyEntryDAO:
package com.myapp.server.dao;

import java.util.logging.Logger;

import com.googlecode.objectify.ObjectifyService;
import com.myapp.shared.MyEntryDao;

public class MyEntryDao extends ObjectifyDao<MyEntry>
{
private static final Logger LOG = Logger.getLogger(MyEntryDao.class.getName());

static
{
ObjectifyService.register(MyEntry.class);
}

public MyEntryDao()
{
super(MyEntry.class);
}

}

最后我们可以向数据库( server 包)发出请求:
public class FinallyDownloadingEntriesServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/plain");
//more code...
resp.setHeader("Content-Disposition", "attachment; filename=\""+"MyFileName"+".txt\";");
try {
MyEntryDao = new MyEntryDao();
/*query to get all MyEntries from datastore sorted by start Time*/
ArrayList<MyEntry> entries = (ArrayList<MyEntry>) dao.ofy().query(MyEntry.class).order("start.msecs").list();

PrintWriter out = resp.getWriter();
int i = 0;
for (MyEntry entry : entries) {
++i;
out.println(i);
out.println(entry.getStart() + entry.getText());
out.println();
}
} finally {
//catching exceptions
}
}

关于GWT-Objectify : basic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5370396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com