请大家讨论一下hibernate这样的写法的优点和不足

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


/**
* @描述: 公共DAO层实现
*/
@SuppressWarnings("unchecked")
public class CommonDaoImpl extends HibernateDaoSupport implements ICommonDao {

public Serializable add(Object object) throws Exception{
return (Serializable) super.getHibernateTemplate().save(object);
}

public void del(Object object) throws Exception{
super.getHibernateTemplate().delete(object);
}

public void update(Object object) throws Exception{
super.getHibernateTemplate().update(object);
}
public Object get(Class clazz, Serializable id) throws Exception{

try{
return super.getHibernateTemplate().get(clazz, id);
}catch (Exception e) {
e.printStackTrace();
}
return null;
}


public List list(final String hql, final int pageNo, final int pageSize) throws Exception{
return super.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult((pageNo - 1) * pageSize);
query.setMaxResults(pageSize);
return query.list();
}
});
}

public int getRowCount(final String hql) throws Exception{
Long i = (Long) super.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
return query.uniqueResult();
}
});
return i.intValue();
}

public List list(String hql) throws Exception{
return super.getHibernateTemplate().find(hql);
}

public void execute(final String hql) throws Exception{
super.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
return query.executeUpdate();
}
});
}

public Object execHql(final String hql) throws Exception{

return (Object)super.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
return query.uniqueResult();
}
});
}

public List sqllist(final String sql, final int pageNo, final int pageSize) throws Exception{
return super.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createSQLQuery(sql);
query.setFirstResult((pageNo - 1) * pageSize);
query.setMaxResults(pageSize);
return query.list();
}
});
}

@Override
public List sqllist(final String sql) throws Exception{
return super.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createSQLQuery(sql);
return query.list();
}
});
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值