ssh中service層與dao層解耦的問題_第1頁
ssh中service層與dao層解耦的問題_第2頁
ssh中service層與dao層解耦的問題_第3頁
ssh中service層與dao層解耦的問題_第4頁
ssh中service層與dao層解耦的問題_第5頁
已閱讀5頁,還剩27頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、11111當前應(yīng)用dao層使用了hibernate,但是為了使應(yīng)用與hibernate解耦,在dao層使用一個接口,此接口中提供了get、save、update、delete、list、query等的方法,service層使用該接口提供的方法而無需知道dao層使用的具體框架。現(xiàn)在有個問題,如果是較復(fù)雜查詢,hibernate提供了hql或者Restrictions、Criteria之類的類來實現(xiàn)查詢,但是我希望service層或action層不去使用hibernate的東西,如何設(shè)計封閉這個條件比較好?回答針對接口編程,而不是實現(xiàn)就好了。DAO接口存在的目的就是這個,Service層只向DAO

2、依賴(這個是必須的),表現(xiàn)層只向Service層依賴,不能跨層依賴,這樣不符合分層的設(shè)計。因此,只有Service層對DAO的依賴,而且依賴的是接口,Service層自然不必知道DAO的實現(xiàn)到底是用Hibernate還是JPA或JDBC了,這已經(jīng)達到了解耦的目的。myali88 (架構(gòu)師) 2010-05-20 代碼是人寫的,如果程序員非要在service層寫hibernate的東西,你是沒辦法控制的。 分層只是個規(guī)范,程序員只有遵守這個規(guī)范,才解耦了。2022228 (中級程序員) 2010-05-20 在設(shè)計的時候hibernate的hql或者Restrictions、Criteria之類

3、的類就不要出現(xiàn)在方法參數(shù)里,多采用泛型機制。可以看下appfuse的源代碼看它是怎么設(shè)計的。cwx714 (架構(gòu)師) 2010-05-20 我比較同意2022228的說法,我們現(xiàn)在使用的就是appfuse,就是有人非要把相關(guān)的東西寫到service層里,很無奈,還有更可氣的是,hibernate不用,非要放個ibatis進去使。 重要的還是遵守規(guī)范,和代碼的審查。 說句很扯的話,時不時的換下數(shù)據(jù)庫(例如oracle改成mysql),時不時的改下框架(hibernate改ibatis)沒準讓他們吃點苦頭就好了。 以上屬于扯淡的話,別拍我。sheep3600 (高級程序員) 2010-05-20

4、1樓的說的不錯,zhou363667565 (初級程序員) 2010-05-20 myali88說的很正確,針對接口編程。 使用spring將dao implement注入進service里。 如果程序員缺乏分層和面向接口編程的概念,可以讓有經(jīng)驗的帶一帶;如果是有意不遵循規(guī),就讓他滾蛋。summerfeel (初級程序員) 2010-05-21 no dao 一個service 一個adapter 業(yè)務(wù)service extends baseServicerrsy23 (初級程序員) 2010-05-21 多使用泛型 將hibernate的操作全都放到daoImpl里面去,不用扯到servic

5、e層的 比如在dao層返回一個List給service就操作就OK了Java代碼 1. /*  2.  * WjMax Corporation copy right 2010 - 2010  3.  *   4.  * $Header: /cvsroot/idc/wj/java/com/wj/common/utils/WjCriteria.java,v 1.5 2010/04/16 09:14:20

6、0;elan Exp $  5.  * $Id: WjCriteria.java,v 1.5 2010/04/16 09:14:20 elan Exp $  6.  * $Author: elan $  7.  * $Date: 2010/04/16 09:14:20 $  8.  * $Revision: 1.5 $

7、  9.  */  10. package mon.utils;   11.   12. /*  13.  * 標準條件查詢  14.  *   15.  *   16.  * author elan Email:272926206  17.  */  18. public interface WjCrite

8、ria    19.   20.     /*  21.      * 添加條件  22.      *   23.      * param criterion  24.      * return  25.

9、      */  26.     public WjCriteria add(Object criterion);   27.   28.     /*  29.      * 排序  30.      *   31. 

10、0;    * param wjOrder  32.      * return  33.      */  34.     public WjCriteria addOrder(WjOrder wjOrder);   35.   36.    &#

11、160;/*  37.      * 創(chuàng)建一個關(guān)聯(lián)別名  38.      *   39.      * param associationPath  40.      *            關(guān)聯(lián)

12、對象導航  41.      * param alias  42.      *            別名  43.      * return  44.      */  45. 

13、60;   public WjCriteria createAlias(String associationPath, String alias);   46.   47.     /*  48.      * 創(chuàng)建一個關(guān)聯(lián)Criteria  49.      *   50.  

14、    * param associationPath  51.      *            關(guān)聯(lián)對象導航  52.      * return  53.      */  54.  

15、0;  public WjCriteria createCriteria(String associationPath);   55.   56.     /*  57.      * 創(chuàng)建一個關(guān)聯(lián)Criteria  58.      *   59.      *

16、60;param associationPath  60.      *            關(guān)聯(lián)對象導航  61.      * param alias  62.      *       

17、60;    別名  63.      * return  64.      */  65.     public WjCriteria createCriteria(String associationPath, String alias);   66.   67.  

18、;   /*  68.      * 獲取條件  69.      *   70.      * return 標準條件  71.      */  72.     public Object criteria

19、();   73.   /* * WjMax Corporation copy right 2010 - 2010 * * $Header: /cvsroot/idc/wj/java/com/wj/common/utils/WjCriteria.java,v 1.5 2010/04/16 09:14:20 elan Exp $ * $Id: WjCriteria.java,v 1.5 2010/04/16 09:14:20 elan Exp $ * $Author: elan $ * $Date: 2010/04/16 09:14:20 $ * $Rev

20、ision: 1.5 $ */package mon.utils;/* * 標準條件查詢 * * * author elan Email:272926206 */public interface WjCriteria /* * 添加條件 * * param criterion * return */public WjCriteria add(Object criterion);/* * 排序 * * param wjOrder * return */public WjCriteria addOrder(WjOrder wjOrder);/* * 創(chuàng)建一個關(guān)聯(lián)別名 * * param assoc

21、iationPath * 關(guān)聯(lián)對象導航 * param alias * 別名 * return */public WjCriteria createAlias(String associationPath, String alias);/* * 創(chuàng)建一個關(guān)聯(lián)Criteria * * param associationPath * 關(guān)聯(lián)對象導航 * return */public WjCriteria createCriteria(String associationPath);/* * 創(chuàng)建一個關(guān)聯(lián)Criteria * * param associationPath * 關(guān)聯(lián)對象導航 * pa

22、ram alias * 別名 * return */public WjCriteria createCriteria(String associationPath, String alias);/* * 獲取條件 * * return 標準條件 */public Object criteria();實現(xiàn)類: Java代碼 1. /*  2.  * WjMax Corporation copy right 2010 - 2010  3.  *   4.  *

23、 $Header: /cvsroot/idc/wj/java/com/wj/common/utils/hbm/WjCriteriaImpl.java,v 1.7 2010/05/17 01:42:14 elan Exp $  5.  * $Id: WjCriteriaImpl.java,v 1.7 2010/05/17 01:42:14 elan Exp $  6.  * $Author

24、: elan $  7.  * $Date: 2010/05/17 01:42:14 $  8.  * $Revision: 1.7 $  9.  */  10. package mon.utils.hbm;   11.   12. import org.hibernate.criterion.Criterion;   13. import

25、0;org.hibernate.criterion.DetachedCriteria;   14.   15. import mon.utils.WjCriteria;   16. import mon.utils.WjOrder;   17.   18. /*  19.  * 基于Hibernate的CriteriaImpl的實現(xiàn)。  20.  *   21.  * author

26、60;elan Email:272926206  22.  *   23.  */  24. public class WjCriteriaImpl implements WjCriteria    25.     private DetachedCriteria    detachedCriteria    =

27、60;null;   26.   27.     public WjCriteriaImpl(Class<?> entityClass)    28.         detachedCriteria = DetachedCriteria.forClass(entityClass);   29.    &#

28、160;   30.   31.     public WjCriteriaImpl(Class<?> entityClass, String alias)    32.         detachedCriteria = DetachedCriteria.forClass(entityClass, alias); 

29、0; 33.        34.   35.     public WjCriteria add(Object criterion)    36.         detachedCriteria.add(Criterion) criterion);   37.    

30、;     return this;   38.        39.   40.     public WjCriteria addOrder(WjOrder wjOrder)    41.         detachedCriteria.addOrder

31、(OrderConverter.convert(wjOrder);   42.         return this;   43.        44.   45.     public WjCriteria createAlias(String associationPath, String a

32、lias)    46.         detachedCriteria.createAlias(associationPath, alias);   47.         return this;   48.        49.   50.  

33、0;  public WjCriteria createCriteria(String associationPath)    51.         detachedCriteria.createCriteria(associationPath);   52.         return this;   

34、53.        54.   55.     public WjCriteria createCriteria(String associationPath, String alias)    56.         detachedCriteria.createCriteria(associationPath,&

35、#160;alias);   57.         return this;   58.        59.   60.     public Object criteria()    61.         ret

36、urn detachedCriteria;   62.        63.   /* * WjMax Corporation copy right 2010 - 2010 * * $Header: /cvsroot/idc/wj/java/com/wj/common/utils/hbm/WjCriteriaImpl.java,v 1.7 2010/05/17 01:42:14 elan Exp $ * $Id: WjCriteriaImpl.java,v 1.7 2010/05/17

37、 01:42:14 elan Exp $ * $Author: elan $ * $Date: 2010/05/17 01:42:14 $ * $Revision: 1.7 $ */package mon.utils.hbm;import org.hibernate.criterion.Criterion;import org.hibernate.criterion.DetachedCriteria;import mon.utils.WjCriteria;import mon.utils.WjOrder;/* * 基于Hibernate的CriteriaImpl的實現(xiàn)。 * * author

38、elan Email:272926206 * */public class WjCriteriaImpl implements WjCriteria private DetachedCriteriadetachedCriteria= null;public WjCriteriaImpl(Class<?> entityClass) detachedCriteria = DetachedCriteria.forClass(entityClass);public WjCriteriaImpl(Class<?> entityClass, String alias) detach

39、edCriteria = DetachedCriteria.forClass(entityClass, alias);public WjCriteria add(Object criterion) detachedCriteria.add(Criterion) criterion);return this;public WjCriteria addOrder(WjOrder wjOrder) detachedCriteria.addOrder(OrderConverter.convert(wjOrder);return this;public WjCriteria createAlias(St

40、ring associationPath, String alias) detachedCriteria.createAlias(associationPath, alias);return this;public WjCriteria createCriteria(String associationPath) detachedCriteria.createCriteria(associationPath);return this;public WjCriteria createCriteria(String associationPath, String alias) detachedCr

41、iteria.createCriteria(associationPath, alias);return this;public Object criteria() return detachedCriteria;排序: Java代碼 1. /*  2.  * WjMax Corporation copy right 2010 - 2010  3.  *   4.  * $Header: /cvsroot/idc/wj/java/com

42、/wj/common/utils/WjOrder.java,v 1.4 2010/04/16 09:14:20 elan Exp $  5.  * $Id: WjOrder.java,v 1.4 2010/04/16 09:14:20 elan Exp $  6.  * $Author: elan $  7.  * $Date: 2010/04/1

43、6 09:14:20 $  8.  * $Revision: 1.4 $  9.  */  10. package mon.utils;   11.   12. /*  13.  * 排序  14.  *   15.  * author elan Email:272926206  16.  *  &

44、#160;17.  */  18. public class WjOrder    19.     public static WjOrder asc(String propertyName)    20.         WjOrder order = new WjOrder(); &#

45、160; 21.         pertyName = propertyName;   22.         order.ascending = true;   23.   24.         return ord

46、er;   25.        26.     public static WjOrder des(String propertyName)    27.         WjOrder order = new WjOrder();   28.   

47、;      pertyName = propertyName;   29.         return order;   30.        31.   32.     private String propertyName&

48、#160;= null;   33.   34.     private boolean ascending = false;   35.   36.     protected WjOrder()    37.         / can't

49、0;instance of out   38.        39.   40.     public String getPropertyName()    41.         return propertyName;   42.    

50、60;   43.   44.     public boolean isAscending()    45.         return ascending;   46.        47.   48.     public 

51、;String toString()    49.         return propertyName + ' ' + (ascending ? "asc" : "desc");   50.        51.   /

52、* * WjMax Corporation copy right 2010 - 2010 * * $Header: /cvsroot/idc/wj/java/com/wj/common/utils/WjOrder.java,v 1.4 2010/04/16 09:14:20 elan Exp $ * $Id: WjOrder.java,v 1.4 2010/04/16 09:14:20 elan Exp $ * $Author: elan $ * $Date: 2010/04/16 09:14:20 $ * $Revision: 1.4 $ */package mon.utils;/* * 排

53、序 * * author elan Email:272926206 * */public class WjOrder public static WjOrder asc(String propertyName) WjOrder order = new WjOrder();pertyName = propertyName;order.ascending = true;return order;public static WjOrder des(String propertyName) WjOrder order = new WjOrder();pertyNam

54、e = propertyName;return order;private String propertyName = null;private boolean ascending = false;protected WjOrder() / can't instance of outpublic String getPropertyName() return propertyName;public boolean isAscending() return ascending;public String toString() return propertyName + ' 

55、9; + (ascending ? "asc" : "desc");表達式工具類 Java代碼 1. /*  2.  * WjMax Corporation copy right 2010 - 2010  3.  *   4.  * $Header: /cvsroot/idc/wj/java/com/wj/common/utils/WjExpression.java,v 1.5 

56、;2010/04/16 09:14:20 elan Exp $  5.  * $Id: WjExpression.java,v 1.5 2010/04/16 09:14:20 elan Exp $  6.  * $Author: elan $  7.  * $Date: 2010/04/16 09:14:20 $  8.  *&

57、#160;$Revision: 1.5 $  9.  */  10. package mon.utils;   11.   12. import java.util.Collection;   13. import java.util.Map;   14.   15. /*  16.  * 條件工具類  17.  *   18. 

58、0;* author elan Email:272926206  19.  *   20.  */  21. public interface WjExpression    22.     /*  23.      * Apply an "equals" constraint 

59、to each property in the key set of a <tt>Map</tt>  24.      *   25.      * param propertyNameValues  26.      *    

60、60;       a map from property names to values  27.      * return Object  28.      */  29.     public Object allEq(Map<?,&#

61、160;?> propertyNameValues);   30.   31.     /*  32.      * Return the conjuction of two expressions  33.      *   34.      *

62、60;param lhs  35.      * param rhs  36.      * return Object  37.      */  38.     public Object and(Object lhs, Object rhs); 

63、0; 39.   40.     /*  41.      * Apply a "between" constraint to the named property  42.      *   43.      * param&#

64、160;propertyName  44.      * param lo  45.      *            value  46.      * param hi  47.      *&

65、#160;           value  48.      * return Object  49.      */  50.     public Object between(String propertyName, Object 

66、lo, Object hi);   51.   52.     /*  53.      * Group expressions together in a single conjunction (A and B and C.)  54.      *

67、0;  55.      * return Conjunction  56.      */  57.     public WjJunction conjunction();   58.   59.     /*  60.      *&

68、#160;Group expressions together in a single disjunction (A or B or C.)  61.      *   62.      * return Conjunction  63.      */  6

69、4.     public WjJunction disjunction();   65.   66.     /*  67.      *   68.      *   69.      * param propertyName 

70、0;70.      * param value  71.      * return Object  72.      */  73.     public Object eq(String propertyName, Object value);   74

71、.   75.     /*  76.      * Apply an "equal" constraint to two properties  77.      */  78.     public Object eqProperty(Strin

72、g propertyName, String otherPropertyName);   79.   80.     /*  81.      * Apply a "greater than or equal" constraint to the named property  82. &

73、#160;    *   83.      * param propertyName  84.      * param value  85.      * return Object  86.      */  87. 

74、0;   public Object ge(String propertyName, Object value);   88.   89.     /*  90.      * Apply a "greater than or equal" constraint to 

75、two properties  91.      */  92.     public Object geProperty(String propertyName, String otherPropertyName);   93.   94.     /*  95.      *

76、 Apply a "greater than" constraint to the named property  96.      *   97.      * param propertyName  98.      * param value  

77、;99.      * return Object  100.      */  101.     public Object gt(String propertyName, Object value);   102.   103.     /*  104.  

78、;    * Apply a "greater than" constraint to two properties  105.      */  106.     public Object gtProperty(String propertyName, String otherProp

79、ertyName);   107.   108.     /*  109.      * Apply an "equal" constraint to the identifier property  110.      *   111.    &#

80、160; * param value  112.      * return  113.      */  114.     public Object idEq(Object value);   115.   116.     /*  117.  

81、    * A case-insensitive "like", similar to Postgres <tt>ilike</tt> operator  118.      *   119.      * param propertyName  120.  

82、60;   * param value  121.      * return Object  122.      */  123.     public Object ilike(String propertyName, Object value);   124.  

83、 125.     /*  126.      * A case-insensitive "like", similar to Postgres <tt>ilike</tt> operator  127.      *   128.     &#

84、160;* param propertyName  129.      * param value  130.      * return Object  131.      */  132.     public Object ilike(String propert

85、yName, String value, WjMatchMode wjMatchMode);   133.   134.     /*  135.      * Apply an "in" constraint to the named property  136.    

86、60; *   137.      * param propertyName  138.      * param values  139.      * return Object  140.      */  141.    

87、 public Object in(String propertyName, Collection<?> values);   142.   143.     /*  144.      * Apply an "in" constraint to the named property &

88、#160;145.      *   146.      * param propertyName  147.      * param values  148.      * return Object  149.      */

89、0; 150.     public Object in(String propertyName, Object values);   151.   152.     /*  153.      * Constrain a collection valued property to be&

90、#160;empty  154.      */  155.     public Object isEmpty(String propertyName);   156.   157.     /*  158.      * Constrain a collection 

91、;valued property to be non-empty  159.      */  160.     public Object isNotEmpty(String propertyName);   161.   162.     /*  163.      

92、;* Apply an "is not null" constraint to the named property  164.      *   165.      * return Object  166.      */  167.  

93、;   public Object isNotNull(String propertyName);   168.   169.     /*  170.      * Apply an "is null" constraint to the named property  171

94、.      *   172.      * return Object  173.      */  174.     public Object isNull(String propertyName);   175.   176.    

95、; /*  177.      * Apply a "less than or equal" constraint to the named property  178.      *   179.      * param propertyName

96、  180.      * param value  181.      * return Object  182.      */  183.     public Object le(String propertyName, Object value);  

97、; 184.   185.     /*  186.      * Apply a "less than or equal" constraint to two properties  187.      */  188.     

98、;public Object leProperty(String propertyName, String otherPropertyName);   189.   190.     /*  191.      * Apply a "like" constraint to the named property

99、  192.      *   193.      * param propertyName  194.      * param value  195.      * return Object  196.      */

100、60; 197.     public Object like(String propertyName, Object value);   198.   199.     /*  200.      * Apply a "like" constraint to the 

101、named property  201.      *   202.      * param propertyName  203.      * param value  204.      * return Object  205.   

102、0;  */  206.     public Object like(String propertyName, String value, WjMatchMode wjMatchMode);   207.   208.     /*  209.      * Apply a &

103、quot;less than" constraint to the named property  210.      *   211.      * param propertyName  212.      * param value  213.    &

104、#160; * return Object  214.      */  215.     public Object lt(String propertyName, Object value);   216.   217.     /*  218.      

105、;* Apply a "less than" constraint to two properties  219.      */  220.     public Object ltProperty(String propertyName, String otherPropertyName);   221.   222.  &#

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論