框架底层综合+快速开发+代码重用框架-设计(Service层)
框架底层综合+快速开发+代码重用框架-设计(Action层)
我们现在几乎都是基于SSH(SSJ)Web开发的,这样就需要我们设计一个快速的,代码可复用性强的高质量的代码,在这里介绍一种思路,和个人拙劣的实现!将会在此写一系列的相关文章。欢迎大家交流。
说明:
1、我们采用的Annotation的方式来配置需要的配置
2、持久层使用JPA的Hibernate实现。
3、控制层使用Struts2
4、搜索使用Compass
一、Model层设计
这一层是我们需要弄清楚业务逻辑层的主要属性和对象之间的映射关,个人的设计思路是,设计一个抽象类(abstract Model)让所有的实体对象都去继承(extends)这个抽象层。这个对象需要实现Serializable, Cloneable 这两个接口,因为我们要自爱queryAll()的时候使用到object.clone();这个方法。为了比较对象大小,我们还需要实现以下介个方法:
public boolean equals(Object obj);
int hashCode();
String toString();
Model clone();
这个几个方法。
还有几个公共的属性:
1.id//数据库和对象的唯一标识。
2.modelAlias//别名查询
3.des//描述
4.porxy//代理标志
5.version//版本
等几个属性。代码参见如下:
package com.jxs.sys.core.base.model;
import java.io.Serializable;
import java.util.List;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.OrderBy;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.xml.bind.annotation.XmlAttribute;
import org.compass.annotations.SearchableId;
/**
*
* @TO Model 基础实体
* @des
* @author BestUpon
* @date Aug 15, 2010 11:54:32 AM
* @version since 1.0
*/
@MappedSuperclass
public abstract class Model implements Serializable, Cloneable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@SearchableId
@OrderBy("id")
private Integer id;
/**
* 手否使用反射代理
*/
@Transient
private boolean proxy = false;
/**
* 对象的版本号,修改之后自增一;i++
*/
@Version
private Integer version;
/**
* 对象的别名,以便需要特殊的需要查询有中文问题的时候,使用他的别名查询可以准确的查询到结果
*/
private String modelAlias;
private String des;
/**
* 搜索字符串集合
* @return
*/
public List<String> getSearchProperties() {
return null;
}
public Model clone() throws CloneNotSupportedException {
return ((Model) super.clone());
}
@XmlAttribute
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public boolean equals(Object obj) {
if (obj == null)
return false;
if (this == obj)
return true;
if (!(obj instanceof Model))
return false;
Model model = (Model) obj;
return (model.getId() == getId());
}
public int hashCode() {
if (this.id == null)
this.id = Integer.valueOf(-1);
return new Integer(this.id.intValue() + 1000).hashCode();
}
public String toString() {
return getMetaData() + getId();
}
public abstract String getMetaData();
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public String getModelAlias() {
return modelAlias;
}
public void setModelAlias(String modelAlias) {
this.modelAlias = modelAlias;
}
public boolean isProxy() {
return proxy;
}
public void setProxy(boolean isProxy) {
this.proxy = isProxy;
}
}
getMetaData()是在Struts2中调用查看其对象,和toString()的时候调用比较方便。
实例代码:其他实体继承会有如下的代码:
package com.jxs.sys.copyweb.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.compass.annotations.Searchable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.jxs.sys.core.base.model.Model;
@Entity
@Component("aboutus")
@Scope("prototype")
@Table(name = "jxs_sys_Aboutus")
@Searchable(alias = "Aboutus")
public class Aboutus extends Model {
private static final long serialVersionUID = 7819540493825377389L;
@Column(name = "_title_")
private String title;
@Column(name = "_content_")
private String content;
@Column(name = "_textDate_")
private Date textDate;
@Column(name = "_show_")
private boolean show = true;// 是否显示
@Override
public String getMetaData() {
return "关于我们";
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getTextDate() {
return textDate;
}
public void setTextDate(Date textDate) {
this.textDate = textDate;
}
public boolean isShow() {
return show;
}
public void setShow(boolean show) {
this.show = show;
}
}