構建dubbo分布式平臺-maven構建ant-framework核心代碼Base封裝
阿新 • • 發佈:2018-08-28
isn cor ide crud ase all tcl 基礎 implement 因為涉及到springmvc、mybatis的集成,為了使項目編碼更簡潔易用,這邊將基礎的BASE進行封裝,其中包括:BaseBean、BaseDao、BaseService、CRUD的基礎封裝、分頁組件的封裝、mybatis的mapper的基礎封裝,各種數據源支持的封裝等。
- 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()); }? }
- BaseDao的基礎封裝(這個很簡單,因為使用的是mybatis集成方案,只需要保留接口即可),代碼如下:
public interface BaseDao {
}
- 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); }
- 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的封裝差不多都在這裏,後面會繼續補充,具體的業務和實現後面會講解到。
構建dubbo分布式平臺-maven構建ant-framework核心代碼Base封裝