日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

(九) 構(gòu)建dubbo分布式平臺(tái)-maven構(gòu)建ant-framework核心代碼Base封裝

 開始6m01rwbqw6 2017-12-11

上一篇我們介紹《構(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代碼  收藏代碼
  1. /** 
  2.  * Entity基礎(chǔ)封裝 
  3.  */  
  4. public abstract class BaseEntity<T> implements Serializable {  
  5.   
  6.     private static final long serialVersionUID = 1234567890987654321L;  
  7.   
  8.     /** 
  9.      * 實(shí)體編號(hào)(唯一標(biāo)識(shí)) 
  10.      */  
  11.     protected String id;  
  12.       
  13.     /** 
  14.      * 當(dāng)前實(shí)體分頁對(duì)象 
  15.      */  
  16.     protected Page<T> page;  
  17.       
  18.   
  19.     /** 
  20.      * 是否插入新紀(jì)錄 
  21.      */  
  22.     protected boolean isNewRecord = false;  
  23.   
  24.     public BaseEntity() {  
  25.           
  26.     }  
  27.       
  28.     public BaseEntity(String id) {  
  29.         this();  
  30.         this.id = id;  
  31.     }  
  32.   
  33.     public String getId() {  
  34.         return id;  
  35.     }  
  36.   
  37.     public void setId(String id) {  
  38.         this.id = id;  
  39.     }  
  40.   
  41.     /** 
  42.      * 數(shù)據(jù)插入之前 
  43.      */  
  44.     public abstract void preInsert();  
  45.       
  46.     /** 
  47.      * 更新數(shù)據(jù)之前 
  48.      */  
  49.     public abstract void preUpdate();  
  50.       
  51.        /** 
  52.      * 是否是新記錄(默認(rèn):false) 
  53.         */  
  54.     public boolean getIsNewRecord() {  
  55.         return isNewRecord || StringUtils.isBlank(getId());  
  56.     }  
  57.   
  58.     /** 
  59.      * 是否是新記錄(默認(rèn):false) 
  60.      */  
  61.     public void setIsNewRecord(boolean isNewRecord) {  
  62.         this.isNewRecord = isNewRecord;  
  63.     }  
  64.   
  65.     /** 
  66.      * 全局變量對(duì)象 
  67.      */  
  68.     @JsonIgnore  
  69.     public Global getGlobal() {  
  70.         return Global.getInstance();  
  71.     }  
  72.       
  73.     @Override  
  74.     public boolean equals(Object obj) {  
  75.         if (null == obj) {  
  76.             return false;  
  77.         }  
  78.         if (this == obj) {  
  79.             return true;  
  80.         }  
  81.         if (!getClass().equals(obj.getClass())) {  
  82.             return false;  
  83.         }  
  84.         BaseEntity<?> that = (BaseEntity<?>) obj;  
  85.         return null == this.getId() ? false : this.getId().equals(that.getId());  
  86.     }     
  87. }  

 2. BaseDao的基礎(chǔ)封裝(這個(gè)很簡(jiǎn)單,因?yàn)槭褂玫氖莔ybatis集成方案,只需要保留接口即可),代碼如下:

Java代碼  收藏代碼
  1. public interface BaseDao {  
  2. }  

 

3. CrudDao的基礎(chǔ)封裝

Java代碼  收藏代碼
  1. /** 
  2.  * DAO基礎(chǔ)封裝 
  3.  */  
  4. public interface CrudDao<T> extends BaseDao {  
  5.   
  6.     /** 
  7.      * 獲取單條數(shù)據(jù) 
  8.      * @param id 
  9.      * @return 
  10.      */  
  11.     public T get(String id);  
  12.       
  13.     /** 
  14.      * 獲取單條數(shù)據(jù) 
  15.      * @param entity 
  16.      * @return 
  17.      */  
  18.     public T get(T entity);  
  19.       
  20.     /** 
  21.      * 查詢數(shù)據(jù)列表,如果需要分頁,請(qǐng)?jiān)O(shè)置分頁對(duì)象,如:entity.setPage(new Page<T>()); 
  22.      * @param entity 
  23.      * @return 
  24.      */  
  25.     public List<T> findList(T entity);  
  26.       
  27.     /** 
  28.      * 查詢所有數(shù)據(jù)列表 
  29.      * @param entity 
  30.      * @return 
  31.      */  
  32.     public List<T> findAllList(T entity);  
  33.       
  34.     /** 
  35.      * 查詢所有數(shù)據(jù)列表 
  36.      * @see public List<T> findAllList(T entity) 
  37.      * @return 
  38.      */  
  39.     @Deprecated  
  40.     public List<T> findAllList();  
  41.       
  42.     /** 
  43.      * 插入數(shù)據(jù) 
  44.      * @param entity 
  45.      * @return 
  46.      */  
  47.     public int insert(T entity);  
  48.       
  49.     /** 
  50.      * 更新數(shù)據(jù) 
  51.      * @param entity 
  52.      * @return 
  53.      */  
  54.     public int update(T entity);  
  55.       
  56.     /** 
  57.      * 刪除數(shù)據(jù) 
  58.      * @param id 
  59.      * @see public int delete(T entity) 
  60.      * @return 
  61.      */  
  62.     @Deprecated  
  63.     public int delete(String id);  
  64.       
  65.     /** 
  66.      * 刪除數(shù)據(jù) 
  67.      * @param entity 
  68.      * @return 
  69.      */  
  70.     public int delete(T entity);  
  71.       
  72. }  

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

Java代碼  收藏代碼
  1. /** 
  2.  * BaseService基礎(chǔ)封裝 
  3.  */  
  4. @Transactional(readOnly = true)  
  5. public abstract class CrudService<D extends CrudDao<T>, T extends DataEntity<T>> extends BaseService {  
  6.       
  7.     /** 
  8.      * 持久層dao 
  9.      */  
  10.     @Autowired  
  11.     protected D dao;  
  12.       
  13.     /** 
  14.      * 獲取單條數(shù)據(jù) 
  15.      * @param id 
  16.      * @return 
  17.      */  
  18.     public T get(String id) {  
  19.         return dao.get(id);  
  20.     }  
  21.       
  22.     /** 
  23.      * 獲取單條數(shù)據(jù) 
  24.      * @param entity 
  25.      * @return 
  26.      */  
  27.     public T get(T entity) {  
  28.         return dao.get(entity);  
  29.     }  
  30.       
  31.     /** 
  32.      * 查詢列表數(shù)據(jù) 
  33.      * @param entity 
  34.      * @return 
  35.      */  
  36.     public List<T> findList(T entity) {  
  37.         return dao.findList(entity);  
  38.     }  
  39.       
  40.     /** 
  41.      * 查詢分頁數(shù)據(jù) 
  42.      * @param page 分頁對(duì)象 
  43.      * @param entity 
  44.      * @return 
  45.      */  
  46.     public Page<T> findPage(Page<T> page, T entity) {  
  47.         entity.setPage(page);  
  48.         page.setList(dao.findList(entity));  
  49.         return page;  
  50.     }  
  51.   
  52.     /** 
  53.      * 保存數(shù)據(jù)(插入或更新) 
  54.      * @param entity 
  55.      */  
  56.     @Transactional(readOnly = false)  
  57.     public void save(T entity) {  
  58.         if (entity.getIsNewRecord()){  
  59.             entity.preInsert();  
  60.             dao.insert(entity);  
  61.         }else{  
  62.             entity.preUpdate();  
  63.             dao.update(entity);  
  64.         }  
  65.     }  
  66.       
  67.     /** 
  68.      * 刪除數(shù)據(jù) 
  69.      * @param entity 
  70.      */  
  71.     @Transactional(readOnly = false)  
  72.     public void delete(T entity) {  
  73.         dao.delete(entity);  
  74.     }  
  75. }  

 

文章內(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

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多