一种非常蹩脚的万用泛型service实现
写完我自己已经吐了
@Servicepublic class PubManager<T, PK extends Serializable>{private SessionFactory sessionFactory;@Autowiredprivate PubManagerImpl<T, PK> mgr;@Autowired public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory; }public PubManagerImpl<T, PK> getI(Class<T> c){if(null == mgr.getDAO()){mgr.setDao(new HibernateDao<T, PK>(sessionFactory ,c));}return mgr;}}
@Service@Transactionalpublic class PubManagerImpl<T, PK extends Serializable> extends AbsManager<T, PK> implements absMgr<T, PK> {private HibernateDao<T, PK> dao;@Overridepublic HibernateDao<T, PK> getDAO() {return dao;}public void setDao(HibernateDao<T, PK> dao) {this.dao = dao;}}Controller@RequestMapping("/xxx/xxxx")public class XxxxXxxxXxxController {@Autowired private CodeManager codeManager;@Autowired private PubManager<XxxxxXxxxXxxx, String> pubManager;
@RequestMapping("allXxx")public @ResponseBody Page<XxxxxXxxxXxxx> logs(){return pubManager.getI(XxxxxXxxxXxxx.class).searchByPage();}