GenericDAOImpl

public class GenericDAOImpl extends HibernateDaoSupport implements GenericDAO {


private Log log = LogFactory.getLog(getClass());


public GenericDAOImpl() {
}

@Override
public <T extends Serializable, ID extends Serializable> T findById(Class<T> entityClass, ID id)
throws DaoException {
try {
return this.getHibernateTemplate().get(entityClass, id);
} catch (Exception e) {
throw new DaoException(e);
}
//return this.getHibernateTemplate().get(entityClass, id);
}


@Override
public <T extends Serializable> void save(T entity) throws DaoException {
try {
this.getHibernateTemplate().save(entity);
} catch (Exception e) {
throw new DaoException(e);
}
}

@Override
public <T extends Serializable> T update(T entity) throws DaoException {
try {


return this.getHibernateTemplate().merge(entity);
} catch (Exception e) {
throw new DaoException(e);
}
}


@Override
public <T extends Serializable> void delete(T entity) throws DaoException {
try {
this.getHibernateTemplate().delete(entity);
} catch (Exception e) {
throw new DaoException(e);
}
}

@Override
@SuppressWarnings("unchecked")
public <T extends Serializable> List<T> findAll(Class<T> entityClass) throws DaoException {
List<T> list = null;
try {
list = this.getHibernateTemplate().find("from " + entityClass.getName());
} catch (Exception e) {
throw new DaoException(e);
}
return list;
}


@Override
public List<Object[]> findByNativeSql(final String sql) throws DaoException {
List<Object[]> list = null;
try {
HibernateTemplate template = this.getHibernateTemplate();
list = template.execute(new HibernateCallback<List<Object[]>>() {
@SuppressWarnings("unchecked")
@Override
public List<Object[]> doInHibernate(Session session) throws HibernateException, SQLException {
// TODO Auto-generated method stub
return session.createSQLQuery(sql).list();


}
});
} catch (Exception e) {
throw new DaoException(e);
}
return list;
}


@Override
public <T extends Serializable> void deleteAll(List<T> entitys) throws DaoException {
try {
this.getHibernateTemplate().deleteAll(entitys);
} catch (Exception e) {
throw new DaoException(e);
}
}

@Override
@SuppressWarnings("unchecked")
public <T extends Serializable> List<T> findByCriteria(DetachedCriteria criteria) throws DaoException {
try {
return this.getHibernateTemplate().findByCriteria(criteria);
} catch (Exception e) {
throw new DaoException(e);
}
}


@Override
public <T extends Serializable> long getCount(Class<T> entiClass) throws DaoException {
int count = 0;
try {
count = DataAccessUtils.intResult(this.getHibernateTemplate().find(
"select count(*) from " + entiClass.getName()));
log.debug("count====" + count);
} catch (Exception e) {
throw new DaoException(e);
}
return count;


}

@Override
public <T extends Serializable> long getAvg(Class<T> entiClass, String fieldName) throws DaoException {
try {
return (Long) (this.getHibernateTemplate()
.find("select avg(" + fieldName + ") from " + entiClass.getName()).get(0));
} catch (Exception e) {
throw new DaoException(e);
}
}

@Override
public <T extends Serializable> long getSum(Class<T> entiClass, String fieldName) throws DaoException {
try {
return (Long) (this.getHibernateTemplate()
.find("select sum(" + fieldName + ") from " + entiClass.getName()).get(0));
} catch (Exception e) {
throw new DaoException(e);
}


}


@Override
public <T extends Serializable> int saveAll(List<T> entities) throws DaoException {
try {
if (entities == null || entities.isEmpty()) {
return 0;
}
for (T item : entities) {
this.getHibernateTemplate().save(item);
}
} catch (Exception e) {
throw new DaoException(e);
}
return entities.size();
}


@Override
public <T extends Serializable> void saveOrUpdateAll(List<T> entities) throws DaoException {
// TODO Auto-generated method stub
try {
this.getHibernateTemplate().saveOrUpdateAll(entities);
} catch (Exception e) {
throw new DaoException(e);
}
}


@Override
public <T extends Serializable> void saveOrUpdate(T entity) throws DaoException {
// TODO Auto-generated method stub
try {
this.getHibernateTemplate().saveOrUpdate(entity);
} catch (Exception e) {
throw new DaoException(e);
}
}


@SuppressWarnings("unchecked")
@Override
public <T extends Serializable> List<T> findByHql(String hql, Object... values) throws DaoException {
// TODO Auto-generated method stub
try {
return this.getHibernateTemplate().find(hql, values);
} catch (Exception e) {
throw new DaoException(e);
}
}


@SuppressWarnings("unchecked")
@Override
public <T extends Serializable> List<T> findByExample(T entity) throws DaoException {
// TODO Auto-generated method stub
try {
return this.getHibernateTemplate().findByExample(entity);
} catch (Exception e) {
throw new DaoException(e);
}
}


@SuppressWarnings("unchecked")
@Override
public <T extends Serializable> List<T> paginate(DetachedCriteria criteria, int pageNo, int pageSize)
throws DaoException {
try {
return this.getHibernateTemplate().findByCriteria(criteria, (pageNo - 1) * pageSize, pageSize);
} catch (Exception e) {
throw new DaoException(e);
}
}


@Override
public int executeUpdate(final String sql) throws DaoException {
int count = 0;
try {
HibernateTemplate template = this.getHibernateTemplate();
count = template.execute(new HibernateCallback<Integer>() {


@Override
public Integer doInHibernate(Session session) throws HibernateException, SQLException {
// TODO Auto-generated method stub
return session.createSQLQuery(sql).executeUpdate();
}
});
} catch (Exception e) {
throw new DaoException(e);
}
return count;
}


@Override
public MPAContext getSqlCount(MPAContext ctx) throws DaoException {
final String sql = ctx.getValue(String.class, "querySql");
List<Integer> list = null;
try {
HibernateTemplate template = this.getHibernateTemplate();
list = template.execute(new HibernateCallback<List<Integer>>() {
@SuppressWarnings("unchecked")
@Override
public List<Integer> doInHibernate(Session session) throws HibernateException, SQLException {
// TODO Auto-generated method stub
return session.createSQLQuery(sql).list();
}
});
if (list == null || list.isEmpty()){
ctx.putValue("Count", 0);
return ctx;
}
} catch (Exception e) {
throw new DaoException(e);
}

ctx.putValue("Count",list.get(0));
return ctx;
}


@SuppressWarnings("unchecked")
@Override
public <T extends Serializable> List<T> paginateByQuery(final String hql, final int pageNo, final int pageSize)
throws DaoException {
List<T> list = null;
try {
HibernateTemplate template = this.getHibernateTemplate();
list = template.execute(new HibernateCallback<List<T>>() {
@Override
public List<T> doInHibernate(Session session) throws HibernateException, SQLException {
// TODO Auto-generated method stub
Query query = session.createQuery(hql);
query.setFirstResult((pageNo - 1) * pageSize);
query.setMaxResults(pageSize);
return session.createQuery(hql).list();
}
});
} catch (Exception e) {
throw new DaoException(e);
}


return list;
}


public <T extends Serializable> List<T> paginateByQuery(final String hql, final int pageNo) throws DaoException {
List<T> list = null;
list = this.paginateByQuery(hql, pageNo, BaseConstants.Per_Page_Size);


return list;
}


public List find(String hql) throws DaoException {
try {
return this.getHibernateTemplate().find(hql);
} catch (Exception e) {
throw new DaoException(e);
}
}

public List createSqlQuery(final String sql) throws DaoException {
List list = null;
try {
HibernateTemplate template = this.getHibernateTemplate();
list = template.execute(new HibernateCallback<List>() {
@Override
public List doInHibernate(Session session) throws HibernateException, SQLException {
// TODO Auto-generated method stub
return session.createSQLQuery(sql).list();
}
});
} catch (Exception e) {
throw new DaoException(e);
}
return list;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值