zoukankan      html  css  js  c++  java
  • 基于hibernate的BaseDao及其实现类的设计

    以前做设计的时候dao接口和它的实现了,这样子就不必写这么多的重复代码了。但由于对反射没有了解,除非依赖hibernate的其他组件,否则写不出来。不过,有了反射,我们可以通过泛型来实现我们想要做的功能了。

             首先是接口:

    package com.sms.dao.base;
    
    import java.util.List;
    
    public interface BaseDao<T> {
    	
    	public void add(T entity) throws Exception;
    
    	public void delete(T entity) throws Exception;
    
    	public void update(T entity) throws Exception;
    
    	public T findById(Integer id) throws Exception;
    	/*
    	 * 得到从startIndex开始大小为pageSize的列表
    	 */
    	public List<T> getPageList(int startIndex , int pageSize) throws Exception;	
    	/*
    	 * 得到总数
    	 */
    	public long getAmount();
    }
    

    然后是实现类:

    package com.sms.dao.base.impl;
    
    import java.lang.reflect.ParameterizedType;
    import java.util.List;
    
    import javax.annotation.Resource;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    
    import com.sms.dao.base.BaseDao;
    
    
    public class BaseDaoImpl<T> implements BaseDao<T> {
    
    	private Class<T> entityClass;
    	private String hql;
    	@Resource
    	private SessionFactory sessionFactory;
    	
    	
    	public Session getSession(){
    		return sessionFactory.getCurrentSession();
    	}
    	
    	
    	@SuppressWarnings("unchecked")
    	public BaseDaoImpl() {
    		//通过反射获取泛型传过来的类的类对象
    		this.entityClass = (Class<T>) ((ParameterizedType) this.getClass()
    				.getGenericSuperclass()).getActualTypeArguments()[0];
    		this.hql = "from " + this.entityClass.getName();
    	}
    	
    	@Override
    	public void add(Object entity) {
    		this.getSession().save(entity);
    	}
    
    	@Override
    	public void delete(Object entity) {
    		this.getSession().delete(entity);
    	}
    
    	@Override
    	public void update(Object entity) {
    		this.getSession().update(entity);
    	}
    	
    	@Override
    	public T findById(Integer id) {
    		@SuppressWarnings("unchecked")
    		T result = (T) this.getSession().get(entityClass,id);
    		return result;
    	}
    
    
    	@Override
    	public List<T> getPageList(int startIndex, int pageSize) {
    		// TODO Auto-generated method stub
    		@SuppressWarnings("unchecked")
    		List<T> list = this.getSession().createQuery(hql).setFirstResult(startIndex).setMaxResults(pageSize).list();
    		System.out.println(hql);
    		return list;
    	}
    
    
    	@Override
    	public long getAmount() {
    		String sql = "select count(*) from "+ this.entityClass.getName();
    		long count =  (Long) this.getSession().createQuery(sql).uniqueResult() ;
    		return count;
    	}
    	
    }
    

    通用接口完成,我们使用的时候,只要继承BaseDaoImp就可以实现最基本的增删改查了。


    例如学生管理系统中的年级:

    接口是:

    package com.sms.dao;
    
    import com.sms.dao.base.BaseDao;
    import com.sms.entity.GradeEntity;
    
    public interface GradeDao extends BaseDao<GradeEntity>{
    	
    }
    

    实现类:

    package com.sms.dao.impl;

    import org.springframework.stereotype.Component;

    import com.sms.dao.GradeDao;
    import com.sms.dao.base.impl.BaseDaoImpl;
    import com.sms.entity.GradeEntity;
    @Component
    public class GradeDaoImpl extends BaseDaoImpl<GradeEntity> implements GradeDao{
        
    }

    这样子,GradeDaoImpl就可以实现最基础的增删改查的功能了。


    转载地址:http://blog.csdn.net/kklt21cn/article/details/42040597


  • 相关阅读:
    页面检测网络外网连接- 网页基础模块(JavaScript)
    sql server替换字段中的某个字符
    关于Linux下 fork() 函数的总结
    郁闷的一天
    关于 Linux 下的线程函数未定义问题
    英文书籍
    学会阅读外文资料
    内存池设计(五)
    内存池设计(四)
    内存池设计(三)
  • 原文地址:https://www.cnblogs.com/archermeng/p/7537442.html
Copyright © 2011-2022 走看看