import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.haosi.base.bean.PageBean;
import com.haosi.base.constant.SystemConstant;
import com.haosi.base.dao.BaseDao;
@SuppressWarnings("unchecked")
@Repository("baseDao")
public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao{
public <T> T get(Class<T> clazz, Integer id) {
return getHibernateTemplate().get(clazz, id);
}
public <T> T load(Class<T> clazz, Integer id) {
return getHibernateTemplate().load(clazz, id);
}
public void save(Object obj) {
if (obj != null)
getHibernateTemplate().save(obj);
}
public void update(Object obj) {
getHibernateTemplate().update(obj);
}
public int update(String hql ,Object...params) {
Query query =createQuery(hql, params);
return query.executeUpdate();
}
public void delete(Object obj){
getHibernateTemplate().delete(obj);
}
public List find(String hql ,Object...params){
return getHibernateTemplate().find(hql,params);
}
public SQLQuery createSQLQuery(String sql,Object...params){
SQLQuery query = getSession().createSQLQuery(sql);
if(params!=null&¶ms.length>0){
for(int i=0 ;i < params.length ;i++ ){
query.setParameter(i, params[i]);
}
}
return query;
}
public Query createQuery(String hql,Object...params){
Query query = getSession().createQuery(hql);
if(params!=null&¶ms.length>0){
for(int i=0 ;i < params.length ;i++ ){
query.setParameter(i, params[i]);
}
}
return query;
}
public List find(String hql ,int start,int count ,Object...params){
return createQuery(hql,params).setFirstResult(start).setMaxResults(count).list();
}
public Object findUnique(String hql,Object...params){
return createQuery(hql,params).uniqueResult();
}
public Long count(String hql,Object...params){
if(hql.startsWith("from")){
hql = "select count(*) " + hql;
}
return (Long) createQuery(hql,params).uniqueResult();
}
public Boolean isExist(String hql ,Object...params){
return count(hql,params) != 0;
}
public int delete(String hql,Object...params){
if(hql.startsWith("from")){
hql = "delete " + hql;
}
return createQuery(hql,params).executeUpdate();
}
public void evict(Object obj){
getSession().evict(obj);
}
@Resource
public void setFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
// public void setTemplate(HibernateTemplate hibernateTemplate){
// super.setHibernateTemplate(hibernateTemplate);
// }
public PageBean forPage(String hql , String pageNo , int pageSize , Object...params){
PageBean page=new PageBean();
String countHql="";
if(hql.contains("order by")){
countHql = "select count(*) " + hql.substring(0,hql.indexOf("order by"));
}else{
countHql = "select count(*) " + hql;
}
Long totalNum = count(countHql , params);
Long totalPage = totalNum % SystemConstant.PAGE_SIZE == 0 ? totalNum / SystemConstant.PAGE_SIZE : totalNum / SystemConstant.PAGE_SIZE + 1;
int curPage=1;
try{
curPage=Integer.parseInt(pageNo);
if(curPage < 1){
curPage = 1;
}else if(curPage > totalPage){
curPage = totalPage.intValue();
}
}catch (Exception e) {
}
page.setData(find(hql,( curPage - 1 ) * SystemConstant.PAGE_SIZE , SystemConstant.PAGE_SIZE , params));
page.setPageNo(curPage);
page.setTotalPage(totalPage.intValue() < 1 ? 1 : totalPage.intValue());
return page;
}
public PageBean forPage(String countHql ,String selectHql , String pageNo , int pageSize , Object...params){
PageBean page=new PageBean();
Long totalNum = count(countHql , params);
Long totalPage = totalNum % SystemConstant.PAGE_SIZE == 0 ? totalNum / SystemConstant.PAGE_SIZE : totalNum / SystemConstant.PAGE_SIZE + 1;
int curPage=1;
try{
curPage=Integer.parseInt(pageNo);
if(curPage < 1){
curPage = 1;
}else if(curPage > totalPage){
curPage = totalPage.intValue();
}
}catch (Exception e) {
}
page.setData(find(selectHql,( curPage - 1 ) * SystemConstant.PAGE_SIZE , SystemConstant.PAGE_SIZE , params));
page.setPageNo(curPage);
page.setTotalPage(totalPage.intValue() < 1 ? 1 : totalPage.intValue());
return page;
}
}
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.haosi.base.bean.PageBean;
import com.haosi.base.constant.SystemConstant;
import com.haosi.base.dao.BaseDao;
@SuppressWarnings("unchecked")
@Repository("baseDao")
public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao{
public <T> T get(Class<T> clazz, Integer id) {
return getHibernateTemplate().get(clazz, id);
}
public <T> T load(Class<T> clazz, Integer id) {
return getHibernateTemplate().load(clazz, id);
}
public void save(Object obj) {
if (obj != null)
getHibernateTemplate().save(obj);
}
public void update(Object obj) {
getHibernateTemplate().update(obj);
}
public int update(String hql ,Object...params) {
Query query =createQuery(hql, params);
return query.executeUpdate();
}
public void delete(Object obj){
getHibernateTemplate().delete(obj);
}
public List find(String hql ,Object...params){
return getHibernateTemplate().find(hql,params);
}
public SQLQuery createSQLQuery(String sql,Object...params){
SQLQuery query = getSession().createSQLQuery(sql);
if(params!=null&¶ms.length>0){
for(int i=0 ;i < params.length ;i++ ){
query.setParameter(i, params[i]);
}
}
return query;
}
public Query createQuery(String hql,Object...params){
Query query = getSession().createQuery(hql);
if(params!=null&¶ms.length>0){
for(int i=0 ;i < params.length ;i++ ){
query.setParameter(i, params[i]);
}
}
return query;
}
public List find(String hql ,int start,int count ,Object...params){
return createQuery(hql,params).setFirstResult(start).setMaxResults(count).list();
}
public Object findUnique(String hql,Object...params){
return createQuery(hql,params).uniqueResult();
}
public Long count(String hql,Object...params){
if(hql.startsWith("from")){
hql = "select count(*) " + hql;
}
return (Long) createQuery(hql,params).uniqueResult();
}
public Boolean isExist(String hql ,Object...params){
return count(hql,params) != 0;
}
public int delete(String hql,Object...params){
if(hql.startsWith("from")){
hql = "delete " + hql;
}
return createQuery(hql,params).executeUpdate();
}
public void evict(Object obj){
getSession().evict(obj);
}
@Resource
public void setFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
// public void setTemplate(HibernateTemplate hibernateTemplate){
// super.setHibernateTemplate(hibernateTemplate);
// }
public PageBean forPage(String hql , String pageNo , int pageSize , Object...params){
PageBean page=new PageBean();
String countHql="";
if(hql.contains("order by")){
countHql = "select count(*) " + hql.substring(0,hql.indexOf("order by"));
}else{
countHql = "select count(*) " + hql;
}
Long totalNum = count(countHql , params);
Long totalPage = totalNum % SystemConstant.PAGE_SIZE == 0 ? totalNum / SystemConstant.PAGE_SIZE : totalNum / SystemConstant.PAGE_SIZE + 1;
int curPage=1;
try{
curPage=Integer.parseInt(pageNo);
if(curPage < 1){
curPage = 1;
}else if(curPage > totalPage){
curPage = totalPage.intValue();
}
}catch (Exception e) {
}
page.setData(find(hql,( curPage - 1 ) * SystemConstant.PAGE_SIZE , SystemConstant.PAGE_SIZE , params));
page.setPageNo(curPage);
page.setTotalPage(totalPage.intValue() < 1 ? 1 : totalPage.intValue());
return page;
}
public PageBean forPage(String countHql ,String selectHql , String pageNo , int pageSize , Object...params){
PageBean page=new PageBean();
Long totalNum = count(countHql , params);
Long totalPage = totalNum % SystemConstant.PAGE_SIZE == 0 ? totalNum / SystemConstant.PAGE_SIZE : totalNum / SystemConstant.PAGE_SIZE + 1;
int curPage=1;
try{
curPage=Integer.parseInt(pageNo);
if(curPage < 1){
curPage = 1;
}else if(curPage > totalPage){
curPage = totalPage.intValue();
}
}catch (Exception e) {
}
page.setData(find(selectHql,( curPage - 1 ) * SystemConstant.PAGE_SIZE , SystemConstant.PAGE_SIZE , params));
page.setPageNo(curPage);
page.setTotalPage(totalPage.intValue() < 1 ? 1 : totalPage.intValue());
return page;
}
}