IBaseDao
package com.glht.sim.dao;
import java.util.List;
public interface IBaseDao {
T get(long id);
void create(T t);
void delete(T t);
void update(T t);
int getTotalCount();
ListgetPage(int startIndex,int count);
List 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 extends HibernateDaoSupport implements IBaseDao {
protected Class entityClass;
protected String className;
public BaseDao(){
entityClass=(Class)((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 ListgetPage(int startIndex,int count) {
return (List)this.getHibernateTemplate().executeFind(
new PageHibernateCallback(
"from "+className+" as c order by c.id desc",startIndex,count));
}
public List getAll(){
return (List)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 get(long id);
void create(T obj);
void delete(T obj);
void update(T obj);
int getTotalCount();
List getPage(int startIndex,int count);
List 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 implements IBaseService {
protected IBaseDao 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 getPage(int startIndex,int count){
return dao.getPage(startIndex,count);
}
public List getAll(){
return dao.getAll();
}
public void setDao(IBaseDao dao) {
this.dao = dao;
}
}