step 8: 数据访问类DAO
java 代码
- /**
- * 地理信息
- */
- package com.fzfx88.base.service;
- import java.util.List;
- import org.apache.commons.logging.Log;
- import org.hibernate.Criteria;
- import org.hibernate.Hibernate;
- import org.hibernate.HibernateException;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import org.hibernate.criterion.Order;
- import org.hibernate.criterion.Restrictions;
- import com.fzfx88.common.UserInfo;
- import com.fzfx88.common.util.DateUtility;
- import com.fzfx88.common.util.LogUtil;
- import com.fzfx88.po.base.DimGeography;
- import com.fzfx88.util.HibernateUtil;
- /**
- * @author huguoqing
- *
- */
- public class GeographyService {
- Log log = LogUtil.getLoger(OrgService.class);
- /**
- * 取得当前所有的地理信息
- *
- * @return
- */
- public List queryGeography() {
- List graphyList = null;
- Session session = HibernateUtil.currentSession();
- try {
- Query query = session
- .createQuery("from DimGeography o where o.usageFlag='1' order by o.id");
- graphyList = query.list();
- } catch (HibernateException e) {
- log.error(e.getMessage());
- } finally {
- HibernateUtil.closeSession();
- }
- return graphyList;
- }
- public List queryGeoByLevelId(Integer levelId,Integer parentGeo) {
- List graphyList = null;
- Session session = HibernateUtil.currentSession();
- try {
- Criteria crit = session.createCriteria(DimGeography.class);
- crit.add(Restrictions.eq("geographyLevel",levelId));
- crit.add(Restrictions.eq("parentGeo",parentGeo));
- crit.add(Restrictions.eq("usageFlag","1"));
- graphyList = crit.list();
- } catch (HibernateException e) {
- e.printStackTrace();
- log.error(e.getMessage());
- } finally {
- HibernateUtil.closeSession();
- }
- return graphyList;
- }
- public List queryGeoByLevelId(Integer levelId) {
- List graphyList = null;
- Session session = HibernateUtil.currentSession();
- try {
- Criteria crit = session.createCriteria(DimGeography.class);
- crit.add(Restrictions.eq("geographyLevel",levelId));
- crit.add(Restrictions.eq("usageFlag","1"));
- graphyList = crit.list();
- } catch (HibernateException e) {
- e.printStackTrace();
- log.error(e.getMessage());
- } finally {
- HibernateUtil.closeSession();
- }
- return graphyList;
- }
- /**
- * 根据地理信息的英文名称模糊查询
- * @param geoName
- * @return
- */
- public List queryGeoListByGeoNameEn(String geoName,String geoLevel){
- List graphyList = null;
- Session session = HibernateUtil.currentSession();
- try {
- Criteria crit = session.createCriteria(DimGeography.class);
- crit.add(Restrictions.eq("usageFlag","1"));
- crit.add(Restrictions.eq("geographyLevel",Integer.valueOf(geoLevel)));
- crit.add(Restrictions.like("geographyNameEn","%"+geoName+"%"));
- graphyList = crit.list();
- } catch (HibernateException e) {
- e.printStackTrace();
- log.error(e.getMessage());
- } finally {
- HibernateUtil.closeSession();
- }
- return graphyList;
- }
- /**
- * 根据获得的父id,查村当前id下所有的字节点地理信息
- *
- * @param parentGeographyId
- * @param usageFlag
- * @return
- */
- public List queryGeographyByParentId(int parentGeographyId, String usageFlag) {
- List graphyList = null;
- Session session = HibernateUtil.currentSession();
- try {
- Criteria crit = session.createCriteria(DimGeography.class);
- crit.add(Restrictions.eq("parentGeo",
- new Integer(parentGeographyId)));
- crit.add(Restrictions.eq("usageFlag", usageFlag));
- crit.addOrder(Order.desc("geoTreeCode"));
- graphyList = crit.list();
- } catch (HibernateException e) {
- e.printStackTrace();
- log.error(e.getMessage());
- } finally {
- HibernateUtil.closeSession();
- }
- return graphyList;
- }
- /**
- * 根据当前获得的地理信息id,取得地理信息相关信息
- *
- * @param graphyId
- * @return
- */
- public DimGeography queryGeography(Integer graphyId) {
- DimGeography graphy = new DimGeography();
- Session session = HibernateUtil.currentSession();
- try {
- graphy = (DimGeography) session.load(DimGeography.class, graphyId);
- } catch (HibernateException e) {
- e.printStackTrace();
- log.error(e.getMessage());
- } finally {
- HibernateUtil.closeSession();
- }
- return graphy;
- }
- /**
- * 新建 地理信息
- * @param po
- */
- public void createGeography(DimGeography po,UserInfo user) {
- Session session = HibernateUtil.currentSession();
- Transaction tx = null;
- po.setCreateBy(user.getEmployeeName());
- po.setCreateDate(DateUtility.getCurrentDate());
- po.setLastupdateBy(user.getEmployeeName());
- po.setLastupdateDate(DateUtility.getCurrentDate());
- po.setUsageFlag("1");
- try {
- tx = session.beginTransaction();
- session.save(po);
- tx.commit();
- session.flush();
- } catch (HibernateException e) {
- log.error(e.getMessage());
- } finally {
- HibernateUtil.closeSession();
- }
- }
- /**
- * 更新地理信息
- * @param vo
- */
- public void updateGeography(DimGeography vo,UserInfo user) {
- Session session = HibernateUtil.currentSession();
- Transaction tran = null;
- try {
- tran = session.beginTransaction();
- vo.setLastupdateBy(user.getEmployeeName());
- vo.setLastupdateDate(DateUtility.getCurrentDateTime());
- session.update(vo);
- tran.commit();
- session.flush();
- } catch (HibernateException e) {
- if (tran != null) {
- tran.rollback();
- }
- log.error(e.getMessage());
- } finally {
- HibernateUtil.closeSession();
- }
- }
- public DimGeography retrieveOrgByTreeCode(String geoTreeCode){
- Session session = HibernateUtil.currentSession();
- Transaction tx = null;
- DimGeography geographyPo = null;
- try {
- tx = session.beginTransaction();
- Query query = session.createQuery("from DimGeography c where c.geoTreeCode=:geoTreeCode and c.usageFlag='1'");
- query.setParameter("geoTreeCode",geoTreeCode);
- geographyPo=(DimGeography)query.uniqueResult();
- if (Hibernate.isInitialized(geographyPo)) {
- Hibernate.initialize(geographyPo);
- }
- } catch (HibernateException e) {
- if (tx != null) {
- tx.rollback();
- }
- log.error(e.getMessage());
- } finally {
- HibernateUtil.closeSession();
- }
- return geographyPo;
- }
- public DimGeography retrieveOrgByTreeCode(String treeCode,int treeCodeNum){
- DimGeography geo = null;
- geo = this.retrieveOrgByTreeCode(treeCode.substring(0,treeCodeNum*3));
- return geo;
- }
- }