上一篇我們介紹《構(gòu)建dubbo分布式平臺(tái)-maven構(gòu)建ant-framework核心代碼annotation》,今天重點(diǎn)講解的是ant-framework核心代碼Base封裝過程。 因?yàn)樯婕暗絪pringmvc、mybatis的集成,為了使項(xiàng)目編碼更簡(jiǎn)潔易用,這邊將基礎(chǔ)的BASE進(jìn)行封裝,其中包括:BaseBean、BaseDao、BaseService、CRUD的基礎(chǔ)封裝、分頁組件的封裝、mybatis的mapper的基礎(chǔ)封裝,各種數(shù)據(jù)源支持的封裝等。 1. BaseEntity基礎(chǔ)封裝,代碼如下: Java代碼  - /**
- * Entity基礎(chǔ)封裝
- */
- public abstract class BaseEntity<T> implements Serializable {
-
- private static final long serialVersionUID = 1234567890987654321L;
-
- /**
- * 實(shí)體編號(hào)(唯一標(biāo)識(shí))
- */
- protected String id;
-
- /**
- * 當(dāng)前實(shí)體分頁對(duì)象
- */
- protected Page<T> page;
-
-
- /**
- * 是否插入新紀(jì)錄
- */
- 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;
- }
-
- /**
- * 數(shù)據(jù)插入之前
- */
- public abstract void preInsert();
-
- /**
- * 更新數(shù)據(jù)之前
- */
- public abstract void preUpdate();
-
- /**
- * 是否是新記錄(默認(rèn):false)
- */
- public boolean getIsNewRecord() {
- return isNewRecord || StringUtils.isBlank(getId());
- }
-
- /**
- * 是否是新記錄(默認(rèn):false)
- */
- public void setIsNewRecord(boolean isNewRecord) {
- this.isNewRecord = isNewRecord;
- }
-
- /**
- * 全局變量對(duì)象
- */
- @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的基礎(chǔ)封裝(這個(gè)很簡(jiǎn)單,因?yàn)槭褂玫氖莔ybatis集成方案,只需要保留接口即可),代碼如下: Java代碼  - public interface BaseDao {
- }
3. CrudDao的基礎(chǔ)封裝
Java代碼  - /**
- * DAO基礎(chǔ)封裝
- */
- public interface CrudDao<T> extends BaseDao {
-
- /**
- * 獲取單條數(shù)據(jù)
- * @param id
- * @return
- */
- public T get(String id);
-
- /**
- * 獲取單條數(shù)據(jù)
- * @param entity
- * @return
- */
- public T get(T entity);
-
- /**
- * 查詢數(shù)據(jù)列表,如果需要分頁,請(qǐng)?jiān)O(shè)置分頁對(duì)象,如:entity.setPage(new Page<T>());
- * @param entity
- * @return
- */
- public List<T> findList(T entity);
-
- /**
- * 查詢所有數(shù)據(jù)列表
- * @param entity
- * @return
- */
- public List<T> findAllList(T entity);
-
- /**
- * 查詢所有數(shù)據(jù)列表
- * @see public List<T> findAllList(T entity)
- * @return
- */
- @Deprecated
- public List<T> findAllList();
-
- /**
- * 插入數(shù)據(jù)
- * @param entity
- * @return
- */
- public int insert(T entity);
-
- /**
- * 更新數(shù)據(jù)
- * @param entity
- * @return
- */
- public int update(T entity);
-
- /**
- * 刪除數(shù)據(jù)
- * @param id
- * @see public int delete(T entity)
- * @return
- */
- @Deprecated
- public int delete(String id);
-
- /**
- * 刪除數(shù)據(jù)
- * @param entity
- * @return
- */
- public int delete(T entity);
-
- }
4. BaseService的基礎(chǔ)封裝(里面封裝了基礎(chǔ)的CRUD操作,包括基礎(chǔ)get,find,insert,update等) Java代碼  - /**
- * BaseService基礎(chǔ)封裝
- */
- @Transactional(readOnly = true)
- public abstract class CrudService<D extends CrudDao<T>, T extends DataEntity<T>> extends BaseService {
-
- /**
- * 持久層dao
- */
- @Autowired
- protected D dao;
-
- /**
- * 獲取單條數(shù)據(jù)
- * @param id
- * @return
- */
- public T get(String id) {
- return dao.get(id);
- }
-
- /**
- * 獲取單條數(shù)據(jù)
- * @param entity
- * @return
- */
- public T get(T entity) {
- return dao.get(entity);
- }
-
- /**
- * 查詢列表數(shù)據(jù)
- * @param entity
- * @return
- */
- public List<T> findList(T entity) {
- return dao.findList(entity);
- }
-
- /**
- * 查詢分頁數(shù)據(jù)
- * @param page 分頁對(duì)象
- * @param entity
- * @return
- */
- public Page<T> findPage(Page<T> page, T entity) {
- entity.setPage(page);
- page.setList(dao.findList(entity));
- return page;
- }
-
- /**
- * 保存數(shù)據(jù)(插入或更新)
- * @param entity
- */
- @Transactional(readOnly = false)
- public void save(T entity) {
- if (entity.getIsNewRecord()){
- entity.preInsert();
- dao.insert(entity);
- }else{
- entity.preUpdate();
- dao.update(entity);
- }
- }
-
- /**
- * 刪除數(shù)據(jù)
- * @param entity
- */
- @Transactional(readOnly = false)
- public void delete(T entity) {
- dao.delete(entity);
- }
- }
文章內(nèi)容不寫太多,希望大家能夠掌握每一個(gè)知識(shí)點(diǎn),基礎(chǔ)的CRUD,BASE的封裝差不多都在這里,后面會(huì)繼續(xù)補(bǔ)充,具體的業(yè)務(wù)和實(shí)現(xiàn)后面會(huì)講解到。 歡迎大家跟我一起學(xué)習(xí)《構(gòu)建dubbo分布式平臺(tái)》,希望大家持續(xù)關(guān)注后面的文章! 愿意了解框架技術(shù)或者源碼的朋友直接求求交流分享技術(shù):2042849237 分布式的一些解決方案,有愿意了解的朋友可以找我們團(tuán)隊(duì)探討 更多詳細(xì)源碼參考來源:http:///technology
|