You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.8 KiB
57 lines
1.8 KiB
package config;
|
|
|
|
import model.AbstractModel;
|
|
import org.hibernate.criterion.DetachedCriteria;
|
|
import org.hibernate.criterion.Projections;
|
|
import org.springframework.dao.DataAccessException;
|
|
import org.springframework.orm.hibernate5.HibernateTemplate;
|
|
|
|
import java.io.Serializable;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Created by reborn on 2017/7/28.
|
|
*/
|
|
|
|
public abstract class BaseService {
|
|
|
|
public abstract HibernateTemplate getHibernateTemplate();
|
|
|
|
public <T extends AbstractModel> Serializable save(T command) throws DataAccessException {
|
|
return getHibernateTemplate().save(command);
|
|
}
|
|
|
|
public <T extends AbstractModel> void update(T command) throws DataAccessException{
|
|
getHibernateTemplate().update(command);
|
|
}
|
|
|
|
public final <T extends AbstractModel> T get(T command) throws DataAccessException {
|
|
return (T) getHibernateTemplate().get((Class<T>)(command.getClass()), command.primaryKey());
|
|
}
|
|
|
|
public <T extends AbstractModel> void delete(T command) throws DataAccessException{
|
|
T model=get(command);
|
|
if(model!=null){
|
|
getHibernateTemplate().delete(model);
|
|
}
|
|
}
|
|
|
|
public final List find(DetachedCriteria criteria, int firstResult,int maxResults) throws DataAccessException{
|
|
return getHibernateTemplate().findByCriteria(criteria,firstResult,maxResults);
|
|
}
|
|
|
|
public final List find(DetachedCriteria criteria) throws DataAccessException{
|
|
return getHibernateTemplate().findByCriteria(criteria);
|
|
}
|
|
|
|
/**
|
|
* 根据查询条件统计记录数
|
|
* @param criteria
|
|
* @return
|
|
*/
|
|
public Long rowCount(DetachedCriteria criteria){
|
|
return getHibernateTemplate().executeWithNativeSession(session -> (Long) criteria.setProjection(Projections.rowCount()).getExecutableCriteria(session).uniqueResult());
|
|
}
|
|
|
|
|
|
}
|
|
|