Generic DAO 是一个 Java 的轻量级 ORM 框架,允许开发者为持久化对象编写 DAO 类。
示例代码:
public class BlogLogicImpl implements BlogLogic { private BlogWowDaoImpl dao; public void setDao(BlogWowDaoImpl dao) { this.dao = dao; } public BlogWowBlog getBlogByLocationAndUser(String locationId, String userId) { List<BlogWowBlog> l = dao.findBySearch(BlogWowBlog.class, new Search("location", locationId) ); if (l.size() <= 0) { // no blog found, create a new one if (canWriteBlog(null, locationId, userId)) { BlogWowBlog blog = new BlogWowBlog(userId, locationId, "Initial title", new Date()); dao.save(blog); return blog; } return null; } else if (l.size() >= 1) { // found existing blog return (BlogWowBlog) l.get(0); } } ... }