用easyjweb 生成了一个crud 项目,具体生成过程请参见 http://blog.8358.net/williamraym/entry/%E4%BD%BF%E7%94%A8maven%E5%BF%AB%E9%80%9F%E4%BD%93%E9%AA%8Ceasyjweb_spring2_jpa%E7%9A%84%E6%B7%BB%E5%88%A0%E6%94%B9%E6%9F%A5%E5%BA%94%E7%94%A8 http://blog.8358.net/williamraym/entry/使用Maven快速体验EasyJWeb+Spring2+JPA的添删改查应用
由于上文,建了一个可以持久化的应用,我现在要根据这个基础平台构建一个留言本程序。 上文中帮我生成了一个Account的CRUD示例,太方便了,都导致我不想手工编码了。
便是为了了解其内部构造,我还得来手工写一个Entry的代码,这个Entry为一个留言本的每一条留言
首先建立一个Entry 的Domain对象
com.easyjweb.demo.domain.Entry.java
内容为: package com.easyjweb.demo.domain;
import java.util.Date;
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id;
import com.easyjf.container.annonation.Field; import com.easyjf.container.annonation.FormPO; import com.easyjf.container.annonation.Validator;
@Entity @FormPO(name = "留言", disInject = "id,serialVersionUID,inputTime,ip") public class Entry implements java.io.Serializable {
private static final long serialVersionUID = 3665340848290050762L; @Id @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.TABLE) private Long id;
@Column(length = 100) @Field(name = "您的姓名", validator = @Validator(name = "string", value = "required;min:1;max:100")) private String name;
@Column(length = 255) @Field(name = "留言内容", validator = @Validator(name = "string", value = "required;min:1;max:255")) private String content; private Date inputTime; private String ip; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getInputTime() { return inputTime; } public void setInputTime(Date inputTime) { this.inputTime = inputTime; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } }
建立一个Entry对象的DAO层对象 com.easyjweb.demo.dao.IEntryDAO.java
内容为: package com.easyjweb.demo.dao;
import com.easyjf.core.dao.GenericDAO; import com.easyjweb.demo.domain.Entry;
public interface IEntryDAO extends GenericDAO<Entry> {
} 只写了一个DAO的接口,继承了easyjweb的GenericDAO,这个基类里为我们封装好了常用的DAO操作,省得自已再去写了。
建立一个Entry对象的Service接口 com.easyjweb.demo.service.IentryService.java
此接口里定义几个常要操作的service接口 addEntry(Entry) getEntry(Long) removeEntry(Long) batchRemoveEntries(List<Serializable>) getEntryBy(IqueryObject) updateEntry(Long,Entry)
内容为: package com.easyjweb.demo.service;
import java.io.Serializable; import java.util.List;
import com.easyjf.core.support.query.IQueryObject; import com.easyjf.web.tools.IPageList; import com.easyjweb.demo.domain.Entry;
public interface IEntryService {
public Long addEntry(Entry instance);
public Entry getEntry(Long id);
public boolean removeEntry(Long id);
public boolean batchRemoveEntries(List<Serializable> list);
public IPageList getEntryBy(IQueryObject object);
public boolean updateEntry(Long id, Entry entry); }
这里只有一个接口,下面实现以上接口。 com.easyjweb.demo.service.impl.EntryServiceImpl.java 内容为:
package com.easyjweb.demo.service.impl;
import java.io.Serializable; import java.util.List;
import com.easyjf.core.support.query.IQueryObject; import com.easyjf.web.tools.IPageList; import com.easyjweb.demo.dao.IEntryDAO; import com.easyjweb.demo.domain.Entry; import com.easyjweb.demo.service.IEntryService;
public class EntryServiceImpl implements IEntryService {
// 需要一个EntryDAO对象,我们使用它抽象出来的接口,通过调用它的接口就可以不用管它具体实现是谁了 private IEntryDAO dao;
/* * (non-Javadoc) * * @see com.easyjweb.demo.service.IEntryService#addEntry(com.easyjweb.demo.domain.Entry) */ public Long addEntry(Entry instance) { this.dao.save(instance); if (instance != null && instance.getId() != null) return instance.getId(); return null; }
/* * (non-Javadoc) * * @see com.easyjweb.demo.service.IEntryService#batchRemoveEntries(java.util.List) */ public boolean batchRemoveEntries(List<Serializable> list) { for (Serializable id : list) { this.dao.remove(id); } // 这里实现在实际项目中要作一些修改,直接返回true了 return true; }
/* * (non-Javadoc) * * @see com.easyjweb.demo.service.IEntryService#getEntry(java.lang.Long) */ public Entry getEntry(Long id) { Entry e = this.dao.get(id); if (e != null) ; return e; }
/* * (non-Javadoc) * * @see com.easyjweb.demo.service.IEntryService#getEntryBy(com.easyjf.core.support.query.IQueryObject) */ public IPageList getEntryBy(IQueryObject object) { return null; }
/* * (non-Javadoc) * * @see com.easyjweb.demo.service.IEntryService#removeEntry(java.lang.Long) */ public boolean removeEntry(Long id) { // 删除时先得到这个对象,如果对象不为NULL再作删除 Entry e = this.dao.get(id); if (e != null) { this.dao.remove(id); return true; } return false; }
public boolean updateEntry(Long id, Entry entry) { if (id != null) { entry.setId(id); } else { return false; } this.dao.update(entry); return true; }
public void setDao(IEntryDAO dao) { this.dao = dao; }
}
|