小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Spring、Hibernate和范型

 shaobin0604@163.com 2006-09-02
      去年6月份開(kāi)始接觸Java,到8,9月份基本掌握Spring,Hibernate,Struts,然后自己搭建了一個(gè)框架,并成功的應(yīng)用到項(xiàng)目上。應(yīng)該說(shuō),從那個(gè)時(shí)候起,我就開(kāi)始思考這個(gè)問(wèn)題,這個(gè)過(guò)程持續(xù)了很長(zhǎng)的時(shí)間。
大概是今年年后,51之前的這一段時(shí)間,突然想通了怎么在Spring里面實(shí)現(xiàn)范型,大致的實(shí)現(xiàn)如下:
public class UserServiceImp  implements  UserService {
        private RootService<T> rootService;
        public setRootService(RootService<T> rootService)
        {
              this.rootService = rootService;
        }
}
applicationContext.xml文件的相關(guān)配置
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation">
   <value>classpath:hibernate.cfg.xml</value>
  </property>
  <property name="mappingDirectoryLocations">
   <list>
    <value>classpath:/com/upup/bean/</value>
   </list>
  </property>
 </bean>
<bean id="rootService" class="com.upup.dao.RootServiceImp">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>
<bean id="userService" class="com.upup.business.service.UserServiceImp">
  <property name="rootService">
   <ref local="rootService" />
  </property>
 </bean>
 
 
在解決了Spring的范型支持的問(wèn)題后,開(kāi)始考慮Hibernate的范型支持的問(wèn)題,但始終受困于如何在范型類中根據(jù)T得到class,因?yàn)椴荒芎?jiǎn)單的調(diào)用T.getClass來(lái)獲得class,直到昨天看到江南白衣的blog上的那篇文章,終于發(fā)現(xiàn)了解決辦法,即定義一個(gè)范型父類AbstractBaseService,在其中實(shí)現(xiàn)基本的CRUD操作,然后UserService繼承AbstractBaseService,這樣完美的實(shí)現(xiàn)了Spring、Hibernate對(duì)于范型的支持,可以大量簡(jiǎn)化開(kāi)發(fā)工作。當(dāng)然,現(xiàn)在還有個(gè)局限,即只能通過(guò)繼承父類的方法來(lái)實(shí)現(xiàn)在父類中獲得T的class。
相關(guān)實(shí)現(xiàn)的代碼:
UserServiceImp.java
public class UserServiceImp extends AbstractBaseService<User> implements
  UserService {
 public UserServiceImp()
 {
  
  super("User");
 }
@Override
 public User add(User obj) {
  obj = super.add(obj);
  this.getCache().addToCache(obj, this.getGetAllKey(), obj.getId());
  this.getCache().putCountKey(this.getGetAllCountKey());
  return obj;
 }
}
AbstractBaseService.java
public abstract class AbstractBaseService<T>{
 private Class<T> entityClass;
 private Log logger = null;
 private String tableName;
 private RootService<T> rootService;
 private CacheService<T> cache;
 
public void setCache(CacheService<T> cacheService) {
  this.cache = cacheService;
  cache.setEntityClass(entityClass);
  cache.setTableName(tableName);
 }
public AbstractBaseService(String tableName) {
  this.tableName = tableName;
  entityClass = (Class<T>) ((ParameterizedType) getClass()
    .getGenericSuperclass()).getActualTypeArguments()[0];
  logger = LogFactory.getLog(entityClass);
 }
public T findByID(Serializable id) {
  return cache.getObjectFormCacheByID(id.toString());
 }
 
 public T add(T obj)
 {
  try {
   obj = rootService.insertObject(obj);
   
   return obj;
  } catch (DataAccessException ex) {
   logger.error("add(T obj):", ex.getCause());
   return null;
  }
 }
 
 public T save(T obj) {
  try {
   rootService.saveObject(obj);
   return obj;
  } catch (DataAccessException ex) {
   logger.error("save(T obj):" + ex);
   return null;
  }
 }
 
 public void delete(T obj)
 {
  rootService.deleteObject(obj);
 }
}
以上代碼也考慮對(duì)緩存的支持,關(guān)于緩存的實(shí)現(xiàn),留待下次專門討論。

    本站是提供個(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)論公約

    類似文章 更多