您的位置:首页 > 其它

GenericDAOImpl

2012-07-16 09:53 190 查看
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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: