`

java 树形结构设计(四) Struts + Hibernate

    博客分类:
  • JAVA
阅读更多

step 8: 数据访问类DAO

java 代码
  1. /**  
  2.  * 地理信息           
  3.  */  
  4. package com.fzfx88.base.service;   
  5.   
  6. import java.util.List;   
  7.   
  8. import org.apache.commons.logging.Log;   
  9. import org.hibernate.Criteria;   
  10. import org.hibernate.Hibernate;   
  11. import org.hibernate.HibernateException;   
  12. import org.hibernate.Query;   
  13. import org.hibernate.Session;   
  14. import org.hibernate.Transaction;   
  15. import org.hibernate.criterion.Order;   
  16. import org.hibernate.criterion.Restrictions;   
  17.   
  18. import com.fzfx88.common.UserInfo;   
  19. import com.fzfx88.common.util.DateUtility;   
  20. import com.fzfx88.common.util.LogUtil;   
  21. import com.fzfx88.po.base.DimGeography;   
  22. import com.fzfx88.util.HibernateUtil;   
  23.   
  24. /**  
  25.  * @author huguoqing  
  26.  *   
  27.  */  
  28. public class GeographyService {   
  29.     Log log = LogUtil.getLoger(OrgService.class);   
  30.   
  31.     /**  
  32.      * 取得当前所有的地理信息  
  33.      *   
  34.      * @return  
  35.      */  
  36.     public List queryGeography() {   
  37.         List graphyList = null;   
  38.         Session session = HibernateUtil.currentSession();   
  39.         try {   
  40.             Query query = session   
  41.                     .createQuery("from DimGeography o where o.usageFlag='1' order by o.id");   
  42.             graphyList = query.list();   
  43.         } catch (HibernateException e) {   
  44.             log.error(e.getMessage());   
  45.         } finally {   
  46.   
  47.             HibernateUtil.closeSession();   
  48.         }   
  49.         return graphyList;   
  50.     }   
  51.     public List queryGeoByLevelId(Integer levelId,Integer parentGeo) {   
  52.         List graphyList = null;   
  53.         Session session = HibernateUtil.currentSession();   
  54.         try {   
  55.   
  56.             Criteria crit = session.createCriteria(DimGeography.class);   
  57.             crit.add(Restrictions.eq("geographyLevel",levelId));   
  58.             crit.add(Restrictions.eq("parentGeo",parentGeo));   
  59.             crit.add(Restrictions.eq("usageFlag","1"));   
  60.             graphyList = crit.list();   
  61.         } catch (HibernateException e) {   
  62.             e.printStackTrace();   
  63.             log.error(e.getMessage());   
  64.         } finally {   
  65.   
  66.             HibernateUtil.closeSession();   
  67.         }   
  68.         return graphyList;   
  69.     }   
  70.      public List queryGeoByLevelId(Integer levelId) {   
  71.             List graphyList = null;   
  72.             Session session = HibernateUtil.currentSession();   
  73.             try {   
  74.   
  75.                 Criteria crit = session.createCriteria(DimGeography.class);   
  76.                 crit.add(Restrictions.eq("geographyLevel",levelId));   
  77.                 crit.add(Restrictions.eq("usageFlag","1"));   
  78.                 graphyList = crit.list();   
  79.             } catch (HibernateException e) {   
  80.                 e.printStackTrace();   
  81.                 log.error(e.getMessage());   
  82.             } finally {   
  83.   
  84.                 HibernateUtil.closeSession();   
  85.             }   
  86.             return graphyList;   
  87.         }   
  88.     /**  
  89.      * 根据地理信息的英文名称模糊查询  
  90.      * @param geoName  
  91.      * @return  
  92.      */  
  93. public List queryGeoListByGeoNameEn(String geoName,String geoLevel){   
  94.     List graphyList = null;   
  95.     Session session = HibernateUtil.currentSession();   
  96.     try {   
  97.   
  98.         Criteria crit = session.createCriteria(DimGeography.class);   
  99.         crit.add(Restrictions.eq("usageFlag","1"));   
  100.         crit.add(Restrictions.eq("geographyLevel",Integer.valueOf(geoLevel)));   
  101.         crit.add(Restrictions.like("geographyNameEn","%"+geoName+"%"));   
  102.         graphyList = crit.list();   
  103.     } catch (HibernateException e) {   
  104.         e.printStackTrace();   
  105.         log.error(e.getMessage());   
  106.     } finally {   
  107.   
  108.         HibernateUtil.closeSession();   
  109.     }   
  110.     return graphyList;   
  111. }   
  112.     /**  
  113.      * 根据获得的父id,查村当前id下所有的字节点地理信息  
  114.      *   
  115.      * @param parentGeographyId  
  116.      * @param usageFlag  
  117.      * @return  
  118.      */  
  119.     public List queryGeographyByParentId(int parentGeographyId, String usageFlag) {   
  120.         List graphyList = null;   
  121.         Session session = HibernateUtil.currentSession();   
  122.         try {   
  123.   
  124.             Criteria crit = session.createCriteria(DimGeography.class);   
  125.             crit.add(Restrictions.eq("parentGeo",   
  126.                     new Integer(parentGeographyId)));   
  127.             crit.add(Restrictions.eq("usageFlag", usageFlag));   
  128.             crit.addOrder(Order.desc("geoTreeCode"));   
  129.             graphyList = crit.list();   
  130.         } catch (HibernateException e) {   
  131.             e.printStackTrace();   
  132.             log.error(e.getMessage());   
  133.         } finally {   
  134.   
  135.             HibernateUtil.closeSession();   
  136.         }   
  137.         return graphyList;   
  138.     }   
  139.   
  140.     /**  
  141.      * 根据当前获得的地理信息id,取得地理信息相关信息  
  142.      *   
  143.      * @param graphyId  
  144.      * @return  
  145.      */  
  146.     public DimGeography queryGeography(Integer graphyId) {   
  147.         DimGeography graphy = new DimGeography();   
  148.         Session session = HibernateUtil.currentSession();   
  149.         try {   
  150.             graphy = (DimGeography) session.load(DimGeography.class, graphyId);   
  151.   
  152.         } catch (HibernateException e) {   
  153.             e.printStackTrace();   
  154.             log.error(e.getMessage());   
  155.         } finally {   
  156.             HibernateUtil.closeSession();   
  157.         }   
  158.         return graphy;   
  159.     }   
  160.        
  161.     /**  
  162.      * 新建 地理信息  
  163.      * @param po  
  164.      */  
  165.     public void createGeography(DimGeography po,UserInfo user) {   
  166.         Session session = HibernateUtil.currentSession();   
  167.         Transaction tx = null;   
  168.         po.setCreateBy(user.getEmployeeName());   
  169.         po.setCreateDate(DateUtility.getCurrentDate());   
  170.         po.setLastupdateBy(user.getEmployeeName());   
  171.         po.setLastupdateDate(DateUtility.getCurrentDate());   
  172.         po.setUsageFlag("1");   
  173.         try {   
  174.             tx = session.beginTransaction();   
  175.             session.save(po);   
  176.             tx.commit();   
  177.             session.flush();   
  178.         } catch (HibernateException e) {   
  179.             log.error(e.getMessage());   
  180.         } finally {   
  181.             HibernateUtil.closeSession();   
  182.         }   
  183.     }   
  184.     /**  
  185.      * 更新地理信息  
  186.      * @param vo  
  187.      */  
  188.     public void updateGeography(DimGeography vo,UserInfo user) {   
  189.         Session session = HibernateUtil.currentSession();   
  190.         Transaction tran = null;   
  191.         try {   
  192.             tran = session.beginTransaction();   
  193.   
  194.             vo.setLastupdateBy(user.getEmployeeName());   
  195.             vo.setLastupdateDate(DateUtility.getCurrentDateTime());   
  196.   
  197.             session.update(vo);   
  198.   
  199.             tran.commit();   
  200.             session.flush();   
  201.         } catch (HibernateException e) {   
  202.             if (tran != null) {   
  203.                 tran.rollback();   
  204.             }   
  205.             log.error(e.getMessage());   
  206.         } finally {   
  207.             HibernateUtil.closeSession();   
  208.         }   
  209.     }   
  210.     public DimGeography retrieveOrgByTreeCode(String geoTreeCode){   
  211.         Session session = HibernateUtil.currentSession();   
  212.         Transaction tx = null;   
  213.         DimGeography geographyPo = null;   
  214.         try {   
  215.             tx = session.beginTransaction();   
  216.             Query query = session.createQuery("from DimGeography c where c.geoTreeCode=:geoTreeCode and c.usageFlag='1'");   
  217.             query.setParameter("geoTreeCode",geoTreeCode);   
  218.             geographyPo=(DimGeography)query.uniqueResult();   
  219.             if (Hibernate.isInitialized(geographyPo)) {   
  220.                 Hibernate.initialize(geographyPo);   
  221.             }   
  222.         } catch (HibernateException e) {   
  223.             if (tx != null) {   
  224.                 tx.rollback();   
  225.             }   
  226.             log.error(e.getMessage());   
  227.         } finally {   
  228.             HibernateUtil.closeSession();   
  229.         }   
  230.         return geographyPo;        
  231.  }   
  232.        
  233.     public DimGeography retrieveOrgByTreeCode(String treeCode,int treeCodeNum){   
  234.         DimGeography geo = null;   
  235.         geo = this.retrieveOrgByTreeCode(treeCode.substring(0,treeCodeNum*3));   
  236.         return geo;        
  237.     }   
  238. }   

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics