天天看點

架構底層綜合+快速開發+代碼重用架構-設計(Model層)

架構底層綜合+快速開發+代碼重用架構-設計(Model層)

架構底層綜合+快速開發+代碼重用架構-設計(Dao層)

架構底層綜合+快速開發+代碼重用架構-設計(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;
         }
}