Liferay学习历程二:搭建二次开发环境(第二部分)

HibernateServiceFactory.java

/**
 * 
 
*/

package  cn.pisoft.portlet.hibernate.factory;

import  cn.pisoft.portlet.hibernate.HibernateService;



/**
 * <p>Title:</p>
 * 
 * <p>Description:</p>
 * 
 * <p>Copyright: Copyright (c) 2007</p>
 * 
 * <p>Company: </p>
 * 
 * <p>Author: 一个人的日子</p>
 * 
 * <p>Email: zhjb2000@hotmail.com</p>
 * 
 * <p>Version: 1.0</p>
 * 
 * <p>Create 2007-7-29</p>
 * 
 
*/


public   class  HibernateServiceFactory  {
    
public static HibernateService getService() {
        
return _getFactory()._service;
    }

        

    
public static HibernateService getTxImpl() {
        
if (_txImpl == null{
            _txImpl 
= (HibernateService)com.liferay.portal.kernel.bean.BeanLocatorUtil.locate(_TX_IMPL);
        }


        
return _txImpl;
    }


    
public void setService(HibernateService service) {
        _service 
= service;
    }


    
private static HibernateServiceFactory _getFactory() {
        
if (_factory == null{
            _factory 
= (HibernateServiceFactory)com.liferay.portal.kernel.bean.BeanLocatorUtil.locate(_FACTORY);
        }


        
return _factory;
    }


    
private static final String _FACTORY = HibernateServiceFactory.class.getName();
    
private static final String _TX_IMPL = HibernateService.class.getName() +".transaction";
    
private static HibernateServiceFactory _factory;
    
private static HibernateService _txImpl;
    
private  HibernateService _service;
}


HibernateServiceUtil.java

package  cn.pisoft.portlet.hibernate.util;

import  java.io.Serializable;
import  java.util.List;

import  cn.pisoft.portlet.common.Pager;
import  cn.pisoft.portlet.hibernate.factory.HibernateServiceFactory;




/**
 * <p>Title:</p>
 * 
 * <p>Description:</p>
 * 
 * <p>Copyright: Copyright (c) 2007</p>
 * 
 * <p>Company: </p>
 * 
 * <p>Author: 一个人的日子</p>
 * 
 * <p>Email: zhjb2000@hotmail.com</p>
 * 
 * <p>Version: 1.0</p>
 * 
 * <p>Create 2007-7-29</p>
 * 
 
*/


public   class  HibernateServiceUtil  {
        
    
public static void save(Object po) throws Exception {
        HibernateServiceFactory.getService().save(po);
    }

    
    
public static List query(String hql)throws Exception {
        
return HibernateServiceFactory.getService().query(hql);
    }

    
    
public static Object get(Class clas ,Serializable id)throws Exception{
        
return HibernateServiceFactory.getService().get(clas, id);
    }

    
    
public static void update(Object po) throws Exception {
        HibernateServiceFactory.getService().update(po);
    }

    
    
public static void delete(Object po) throws Exception {
        HibernateServiceFactory.getService().delete(po);
    }

    
    
public static Pager queryByPage(String hql,int totalElements,int pageNumber,int pageSize) throws Exception {
        
return HibernateServiceFactory.getService().queryByPage(hql,totalElements,pageNumber,pageSize);
    }

    
    
public static Pager queryByPage(String hql,int pageNumber,int pageSize) throws Exception {
        
return HibernateServiceFactory.getService().queryByPage(hql,pageNumber,pageSize);
    }

    
    
public static void executeSql(String sql)throws Exception {
        HibernateServiceFactory.getService().executeSql(sql);
    }

    
    
public static int getCountByExecuteSql(String sql)throws Exception{
        
return HibernateServiceFactory.getService().getCountByExecuteSql(sql);
    }

    
    
public static List getListByExecuteSql(String sql,String columnName)throws Exception{
        
return HibernateServiceFactory.getService().getListByExecuteSql(sql,columnName);
    }

    
public static List getListByExecuteSql(String sql,int fetchNubmer)throws Exception{
        
return HibernateServiceFactory.getService().getListByExecuteSql(sql,fetchNubmer);
    }

    
    
public static List getTopN(String hql, int n) throws Exception {
        
return HibernateServiceFactory.getService().getTopN(hql, n);
    }

}

 上面的类和接口是实现Hibernate数据库访问层CURD的封装,至于他们之间的关系要结合Spring配置文件来看,我封装的访问入口类的是HibernateServiceUtil.java这个类,从这里入手再结合Spring文件就能很清楚地看出这几个类之间地关系.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值