JavaEE 3层架构的DAO,Service,Web简单封装
时间:2011-02-04 博客园 Hacker-TTAO
实现的逻辑大体是
GeneralDAO(Interface). AbstractGeneralDAOImpl(Abstract class), GeneralService(Interface), AbstractGeneralServiceImpl(Abstract class).
具体
IDAO extends GeneralDAO,
IDAOImpl extends AbstractGeneralDAOImpl,
IService extends GeneralService,
IServiceImpl extends AbstractGeneralServiceImpl
实现代码如下.
GeneralDAO<T, ID extends Serializable>
1: import java.io.Serializable;
2: import java.util.Collection;
3: import java.util.List;
4:
5: import PagingVO;
6:
7: /**
8: * General DAO
9: *
10: * @author Hacker-TTAO
11: * @version 0.1.0
12: */
13: public interface GeneralDAO<T, ID extends Serializable> {
14:
15: T findByID(ID id);
16:
17: List<T> findAll();
18:
19: PagingVO getPagingData(int currentPage, int pageSize);
20:
21: T save(T entity);
22:
23: void save(T... entity);
24:
25: T update(T entity);
26:
27: void update(Collection<T> entity);
28:
29: void saveOrUpdate(T entity);
30:
31: void saveOrUpdate(Collection<T> entities);
32:
33: void delete(T entity);
34:
35: void delete(ID id);
36:
37: void delete(Collection<T> entities);
38:
39: void marge(T eneity);
40:
41: void marge(T... eneity);
42:
43: int getCount();
44: }
JavaEE 32??ü11μ?DAO,Service,Web?òμ¥·a×°(2)
ê±??:2011-02-04 2??í?° Hacker-TTAO
AbstractGeneralDAOImpl<T, ID extends Serializable>
1:????import??java.io.Serializable;
2:????import??java.lang.reflect.ParameterizedType;
3:
4:????import??org.apache.commons.logging.Log;
5:????import??org.apache.commons.logging.LogFactory;
6:
7:????/**
8:??????*??Abstract??General??DAO??Implement
9:??????*??
10:??????*??@author??Hacker-TTAO
11:??????*??@version??0.1.0
12:??????*/
13:????public??abstract??class??AbstractGeneralDAOImpl<T,??ID??extends??Serializable>??implements??GeneralDAO<T,??ID>??{
14:
15:????????????private??final??Log??log??=??LogFactory.getLog(this.getClass());
16:
17:????????????protected??final??Class<T>??persistenceClass;
18:
19:????????????protected??final??Class<T>??pkClass;
20:
21:????????????protected??AbstractGeneralDAOImpl()??{
22:????????????????????this.persistenceClass??=??(Class<T>)??((ParameterizedType)??getClass().getGenericSuperclass()).getActualTypeArguments()[0];
23:????????????????????this.pkClass??=??(Class<T>)??((ParameterizedType)??getClass().getGenericSuperclass()).getActualTypeArguments()[1];
24:
25:????????????????????if??(log.isInfoEnabled())??{
26:????????????????????????????log.info("?a??·oDí2?êy??T??:??"??+??getPersistenceClass().getName());
27:????????????????????}
28:????????????}
29:
30:????????????protected??final??Class<T>??getP
|