ssh中service层与dao层解耦的问题_第1页
ssh中service层与dao层解耦的问题_第2页
ssh中service层与dao层解耦的问题_第3页
ssh中service层与dao层解耦的问题_第4页
ssh中service层与dao层解耦的问题_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、11111当前应用dao层使用了hibernate,但是为了使应用与hibernate解耦,在dao层使用一个接口,此接口中提供了get、save、update、delete、list、query等的方法,service层使用该接口提供的方法而无需知道dao层使用的具体框架。现在有个问题,如果是较复杂查询,hibernate提供了hql或者Restrictions、Criteria之类的类来实现查询,但是我希望service层或action层不去使用hibernate的东西,如何设计封闭这个条件比较好?回答针对接口编程,而不是实现就好了。DAO接口存在的目的就是这个,Service层只向DAO

2、依赖(这个是必须的),表现层只向Service层依赖,不能跨层依赖,这样不符合分层的设计。因此,只有Service层对DAO的依赖,而且依赖的是接口,Service层自然不必知道DAO的实现到底是用Hibernate还是JPA或JDBC了,这已经达到了解耦的目的。myali88 (架构师) 2010-05-20 代码是人写的,如果程序员非要在service层写hibernate的东西,你是没办法控制的。 分层只是个规范,程序员只有遵守这个规范,才解耦了。2022228 (中级程序员) 2010-05-20 在设计的时候hibernate的hql或者Restrictions、Criteria之类

3、的类就不要出现在方法参数里,多采用泛型机制。可以看下appfuse的源代码看它是怎么设计的。cwx714 (架构师) 2010-05-20 我比较同意2022228的说法,我们现在使用的就是appfuse,就是有人非要把相关的东西写到service层里,很无奈,还有更可气的是,hibernate不用,非要放个ibatis进去使。 重要的还是遵守规范,和代码的审查。 说句很扯的话,时不时的换下数据库(例如oracle改成mysql),时不时的改下框架(hibernate改ibatis)没准让他们吃点苦头就好了。 以上属于扯淡的话,别拍我。sheep3600 (高级程序员) 2010-05-20

4、1楼的说的不错,zhou363667565 (初级程序员) 2010-05-20 myali88说的很正确,针对接口编程。 使用spring将dao implement注入进service里。 如果程序员缺乏分层和面向接口编程的概念,可以让有经验的带一带;如果是有意不遵循规,就让他滚蛋。summerfeel (初级程序员) 2010-05-21 no dao 一个service 一个adapter 业务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.      * 创建一个关联别名  38.      *   39.      * param associationPath  40.      *            关联

12、对象导航  41.      * param alias  42.      *            别名  43.      * return  44.      */  45. 

13、60;   public WjCriteria createAlias(String associationPath, String alias);   46.   47.     /*  48.      * 创建一个关联Criteria  49.      *   50.  

14、    * param associationPath  51.      *            关联对象导航  52.      * return  53.      */  54.  

15、0;  public WjCriteria createCriteria(String associationPath);   55.   56.     /*  57.      * 创建一个关联Criteria  58.      *   59.      *

16、60;param associationPath  60.      *            关联对象导航  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);/* * 创建一个关联别名 * * param assoc

21、iationPath * 关联对象导航 * param alias * 别名 * return */public WjCriteria createAlias(String associationPath, String alias);/* * 创建一个关联Criteria * * param associationPath * 关联对象导航 * return */public WjCriteria createCriteria(String associationPath);/* * 创建一个关联Criteria * * param associationPath * 关联对象导航 * pa

22、ram alias * 别名 * return */public WjCriteria createCriteria(String associationPath, String alias);/* * 获取条件 * * return 标准条件 */public Object criteria();实现类: 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的实现。  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的实现。 * * 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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论