IBaseDao
1 package com.glht.sim.dao; 2 3 import java.util.List; 4 5 6 public interface IBaseDao<T> { 7 T get(long id); 8 void create(T t); 9 void delete(T t);10 void update(T t);11 int getTotalCount();12 List<T>getPage(int startIndex,int count);13 List<T> getAll();14 }
BasoDao
1 package com.glht.sim.dao.impl; 2 3 import java.lang.reflect.ParameterizedType; 4 import java.sql.SQLException; 5 import java.util.List; 6 7 import org.hibernate.HibernateException; 8 import org.hibernate.Session; 9 import org.springframework.orm.hibernate3.HibernateCallback;10 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;11 12 import com.glht.sim.dao.IBaseDao;13 14 public abstract class BaseDao<T> extends HibernateDaoSupport implements IBaseDao<T> {15 protected Class<T> entityClass;16 protected String className;17 public BaseDao(){18 entityClass=(Class<T>)((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];19 className=entityClass.getName();20 }21 public T get(long id){22 return (T)this.getHibernateTemplate().get(entityClass, id);23 }24 public void create(T t){25 this.getHibernateTemplate().save(t);26 }27 public void delete(T t){28 this.getHibernateTemplate().delete(t);29 }30 public void update(T t){31 this.getHibernateTemplate().update(t);32 }33 34 public int getTotalCount(){35 36 Object obj=this.getHibernateTemplate().execute(new HibernateCallback(){37 public Object doInHibernate(Session session)38 throws HibernateException, SQLException{39 40 return session.createQuery("select count(id) from "+className).uniqueResult();41 }42 });43 return (int)((Long)obj).longValue();44 }45 public List<T>getPage(int startIndex,int count) {46 47 return (List<T>)this.getHibernateTemplate().executeFind(48 new PageHibernateCallback(49 "from "+className+" as c order by c.id desc",startIndex,count));50 }51 52 public List<T> getAll(){53 return (List<T>)this.getHibernateTemplate().find("from "+className+" as c order by c.id desc");54 }55 }
IBaseService
1 package com.glht.sim.service; 2 3 import java.util.List; 4 5 6 public interface IBaseService<T> { 7 T get(long id); 8 void create(T obj); 9 10 void delete(T obj);11 void update(T obj);12 int getTotalCount();13 List<T> getPage(int startIndex,int count);14 List<T> getAll();15 }BaseService
1 package com.glht.sim.service.impl; 2 3 import java.util.List; 4 5 import com.glht.sim.dao.IBaseDao; 6 import com.glht.sim.service.IBaseService; 7 8 public abstract class BaseService<T> implements IBaseService<T> { 9 protected IBaseDao<T> dao;10 11 public T get(long id){12 return dao.get(id);13 }14 15 public void create(T obj){16 dao.create(obj);17 }18 19 public void delete(T obj){20 dao.delete(obj);21 }22 public void update(T obj){23 dao.update(obj);24 }25 26 public int getTotalCount(){27 return dao.getTotalCount();28 }29 30 public List<T> getPage(int startIndex,int count){31 return dao.getPage(startIndex,count);32 }33 34 public List<T> getAll(){35 return dao.getAll();36 }37 38 public void setDao(IBaseDao<T> dao) {39 this.dao = dao;40 }41 42 }
来源:https://www.cnblogs.com/qinying/archive/2011/04/24/2026048.html