Hibernate开发底层公共接口

最近阅读到一个案例,觉得封装的特别好!

package com.jxhkst.school.dao;

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

import com.jxhkst.school.util.PageInfo;

/**
 *      公共的接口
 * @author 浪丶荡
 *
 */
public interface ICommonDao<T> {
    /**
     *      公共查询方法
     * @param entity
     */
    public void save(T entity);
    /**
     *      公共更新方法
     * @param entity
     */
    void update(T entity);
    /**
     *      公共根据编号查找对象方法<br>
     *      绝在不管什么类型的编号都能复用(String/Integer)
     * @param entity
     */
    T findObjectByID(Serializable id);
    /**
     *      编号定点删除
     * @param ids
     */
    void deleteObjectByIDs(Serializable ... ids);
    /**
     *      批量删除
     * @param entities
     */
    void deleteObjectByCollection(Collection<T> entities);
}

以上接口的实现(基于Hibernate):

package com.jxhkst.school.daoimpl;

import java.io.Serializable;
import java.util.Collection;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.jxhkst.school.dao.ICommonDao;
import com.jxhkst.school.util.GenericSuperClass;
/**
 *      
 * @author 浪丶荡
 *
 * @param <T>
 */
public class CommonDaoImpl<T> extends HibernateDaoSupport implements ICommonDao<T> {

    //范型转换
        @SuppressWarnings("unchecked")
        private Class entity = (Class)GenericSuperClass.getClass(this.getClass());
    //需要操作数据库,要用到Hibernate模板,需要继承HibernateDaoSupport
    public void save(T entity) {
        //使用Hibernate模板保存数据
        this.getHibernateTemplate().save(entity);
    }
    //注入sessionFactory
    @Resource(name="sessionFactory")
    public final void setSessionFactoryDi(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }
    @Override
    public void update(T entity) {
        this.getHibernateTemplate().update(entity);

    }
    @SuppressWarnings("unchecked")
    @Override
    public T findObjectByID(Serializable id) {
        return (T)this.getHibernateTemplate().get(entity, id);
    }
    @Override
    public void deleteObjectByIDs(Serializable... ids) {
        for(int i=0;ids!=null && i<ids.length;i++){
            Serializable id = ids[i];
            Object object = (Object)this.getHibernateTemplate().get(entity, id);
            this.getHibernateTemplate().delete(object);
        }

    }
    @Override
    public void deleteObjectByCollection(Collection<T> entities) {
        this.getHibernateTemplate().deleteAll(entities);

    }

}

泛型转换工具

public static Class getClass(Class tClass) {
        ParameterizedType pt = (ParameterizedType) tClass.getGenericSuperclass();
        Class entity = (Class)pt.getActualTypeArguments()[0];
        return entity;
    }

底层接口开发好了后,其他的业务接口只需要继承公共接口就可以。

而业务接口的实现类需要实现业务接口并继续公共接口的实现类

@Repository(ISchoolTestBeanDao.SERVICE_NAME)
public class SchoolTestBeanDaoImpl extends CommonDaoImpl<SchoolTestBean> implements ISchoolTestBeanDao {

}

这里面的泛型和序列化对象用的我水土都不服就服它!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值