java项目常用 BaseDao BaseService

薄伟彦
2023-12-01

IBaseDao

package com.glht.sim.dao;
 
  import java.util.List;
 
 
  public interface IBaseDao<T> {
     T get(long id);
     void create(T t);
     void delete(T t);
     void update(T t);
     int getTotalCount();
     List<T>getPage(int startIndex,int count);
     List<T> getAll();
 }


BasoDao

package com.glht.sim.dao.impl;
 
 import java.lang.reflect.ParameterizedType;
 import java.sql.SQLException;
 import java.util.List;
 
 import org.hibernate.HibernateException;
 import org.hibernate.Session;
 import org.springframework.orm.hibernate3.HibernateCallback;
 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 
 import com.glht.sim.dao.IBaseDao;
 
 public abstract class BaseDao<T> extends HibernateDaoSupport implements IBaseDao<T> {
     protected Class<T> entityClass;
     protected String className;
     public BaseDao(){
         entityClass=(Class<T>)((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
         className=entityClass.getName();
     }
     public T get(long id){
         return (T)this.getHibernateTemplate().get(entityClass, id);
     }
     public void create(T t){
         this.getHibernateTemplate().save(t);
     }
     public void delete(T t){
         this.getHibernateTemplate().delete(t);
     }
     public void update(T t){
         this.getHibernateTemplate().update(t);
     }
     
     public int getTotalCount(){
         
         Object obj=this.getHibernateTemplate().execute(new HibernateCallback(){
             public Object doInHibernate(Session session)
                 throws HibernateException, SQLException{
                 
                 return session.createQuery("select count(id) from "+className).uniqueResult();
             }
         });
         return (int)((Long)obj).longValue();
     }
     public List<T>getPage(int startIndex,int count) {
         
         return (List<T>)this.getHibernateTemplate().executeFind(
                 new PageHibernateCallback(
                         "from "+className+" as c order by c.id desc",startIndex,count));
     }
     
     public List<T> getAll(){
         return (List<T>)this.getHibernateTemplate().find("from "+className+" as c order by c.id desc");
     }
 }


IBaseService

package com.glht.sim.service;
 
 import java.util.List;
 
 
 public interface IBaseService<T> {
     T get(long id);
     void create(T obj);
     
     void delete(T obj);
     void update(T obj);
     int getTotalCount();
     List<T> getPage(int startIndex,int count);
     List<T> getAll();
 }


BaseService

package com.glht.sim.service.impl;
 
 import java.util.List;
 
 import com.glht.sim.dao.IBaseDao;
 import com.glht.sim.service.IBaseService;
 
 public abstract class BaseService<T> implements IBaseService<T> {
     protected IBaseDao<T> dao;
     
     public T get(long id){
         return dao.get(id);
     }
     
     public void create(T obj){
         dao.create(obj);
     }
     
     public void delete(T obj){
         dao.delete(obj);
     }
     public void update(T obj){
         dao.update(obj);
     }
 
     public int getTotalCount(){
         return dao.getTotalCount();
     }
     
     public List<T> getPage(int startIndex,int count){
         return dao.getPage(startIndex,count);
     }
     
     public List<T> getAll(){
         return dao.getAll();
     }
     
     public void setDao(IBaseDao<T> dao) {
         this.dao = dao;
     }
     
 }


 

 类似资料: