構建dubbo分散式平臺-maven構建ant-framework核心程式碼Base封裝

IT小兵發表於2018-08-28

因為涉及到springmvc、mybatis的整合,為了使專案編碼更簡潔易用,這邊將基礎的BASE進行封裝,其中包括:BaseBean、BaseDao、BaseService、CRUD的基礎封裝、分頁元件的封裝、mybatis的mapper的基礎封裝,各種資料來源支援的封裝等。

1. BaseEntity基礎封裝,程式碼如下:

/**
 * Entity基礎封裝
 */
public abstract class BaseEntity<T> implements Serializable {
private static final long serialVersionUID = 1234567890987654321L;
/**
* 實體編號(唯一標識)
*/
protected String id;
 
/**
* 當前實體分頁物件
*/
protected Page<T> page;
/**
* 是否插入新紀錄
*/
protected boolean isNewRecord = false;
public BaseEntity() {
}
public BaseEntity(String id) {
this();
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
/**
* 資料插入之前
*/
public abstract void preInsert();
 
/**
* 更新資料之前
*/
public abstract void preUpdate();
 
       /**
* 是否是新記錄(預設:false)
        */
public boolean getIsNewRecord() {
        return isNewRecord || StringUtils.isBlank(getId());
    }
/**
* 是否是新記錄(預設:false)
*/
public void setIsNewRecord(boolean isNewRecord) {
this.isNewRecord = isNewRecord;
}
/**
* 全域性變數物件
*/
@JsonIgnore
public Global getGlobal() {
return Global.getInstance();
}
@Override
public boolean equals(Object obj) {
if (null == obj) {
   return false;
}
if (this == obj) {
   return true;
}
if (!getClass().equals(obj.getClass())) {
   return false;
}
BaseEntity<?> that = (BaseEntity<?>) obj;
return null == this.getId() ? false : this.getId().equals(that.getId());
} 
}


複製程式碼


2. BaseDao的基礎封裝(這個很簡單,因為使用的是mybatis整合方案,只需要保留介面即可),程式碼如下:

public interface BaseDao {}複製程式碼


3. CrudDao的基礎封裝

/**
 * DAO基礎封裝
 */
public interface CrudDao<T> extends BaseDao {
 
 
/**
* 獲取單條資料
* @param id
* @return
*/
public T get(String id);
 
/**
* 獲取單條資料
* @param entity
* @return
*/
public T get(T entity);
 
/**
* 查詢資料列表,如果需要分頁,請設定分頁物件,如:entity.setPage(new Page<T>());
* @param entity
* @return
*/
public List<T> findList(T entity);
 
/**
* 查詢所有資料列表
* @param entity
* @return
*/
public List<T> findAllList(T entity);
 
/**
* 查詢所有資料列表
* @see public List<T> findAllList(T entity)
* @return
*/
@Deprecated
public List<T> findAllList();
 
/**
* 插入資料
* @param entity
* @return
*/
public int insert(T entity);
 
/**
* 更新資料
* @param entity
* @return
*/
public int update(T entity);
 
/**
* 刪除資料
* @param id
* @see public int delete(T entity)
* @return
*/
@Deprecated
public int delete(String id);
 
/**
* 刪除資料
* @param entity
* @return
*/
public int delete(T entity);
 
}


複製程式碼


4. BaseService的基礎封裝(裡面封裝了基礎的CRUD操作,包括基礎get,find,insert,update等)

/**
 * BaseService基礎封裝
 */
@Transactional(readOnly = true)
public abstract class CrudService<D extends CrudDao<T>, T extends DataEntity<T>> extends BaseService {
 
/**
* 持久層dao
*/
@Autowired
protected D dao;
 
/**
* 獲取單條資料
* @param id
* @return
*/
public T get(String id) {
return dao.get(id);
}
 
/**
* 獲取單條資料
* @param entity
* @return
*/
public T get(T entity) {
return dao.get(entity);
}
 
/**
* 查詢列表資料
* @param entity
* @return
*/
public List<T> findList(T entity) {
return dao.findList(entity);
}
 
/**
* 查詢分頁資料
* @param page 分頁物件
* @param entity
* @return
*/
public Page<T> findPage(Page<T> page, T entity) {
entity.setPage(page);
page.setList(dao.findList(entity));
return page;
}
 
 
/**
* 儲存資料(插入或更新)
* @param entity
*/
@Transactional(readOnly = false)
public void save(T entity) {
if (entity.getIsNewRecord()){
entity.preInsert();
dao.insert(entity);
}else{
entity.preUpdate();
dao.update(entity);
}
}
 
/**
* 刪除資料
* @param entity
*/
@Transactional(readOnly = false)
public void delete(T entity) {
dao.delete(entity);
}
}
複製程式碼

文章內容不寫太多,希望大家能夠掌握每一個知識點,基礎的CRUD,BASE的封裝差不多都在這裡,後面會繼續補充,具體的業務和實現後面會講解到


相關文章