📄 basedaoimpl.java
字号:
package com.lovo.zengxy.base.hibernate;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.lovo.zengxy.base.dao.BaseDAO;
import com.lovo.zengxy.util.BaseDAOException;
public class BaseDAOImpl extends HibernateDaoSupport implements BaseDAO {
public List cutPage(final int firstResult, final int maxResults, final String hql,
final Object[] objs) throws BaseDAOException {
return (List) this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if(objs!=null){
for(int i=0;i<objs.length;i++){
query.setParameter(i, objs[i]);
};
}
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
return query.list();
}
});
}
public void delete(long id, Class clazz) throws BaseDAOException {
}
public int deleteAll(final Class clazz) throws BaseDAOException {
return (Integer)this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery("delete from"+clazz.getName());
return query.executeUpdate();
}
});
}
public int deleteByCondition(final String hql, final Object[] args)
throws BaseDAOException {
return (Integer)this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
for(int i=0;i<args.length;i++){
query.setParameter(i, args[i]);
};
return query.executeUpdate();
}
});
}
public List findAll(final Class clazz) throws BaseDAOException {
return (List)this.getHibernateTemplate().find("from"+clazz.getName());
}
public List findByCondition(final String hql, final Object[] args) throws BaseDAOException{
return (List)this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
for(int i=0;i<args.length;i++){
query.setParameter(i, args[i]);
};
return query.executeUpdate();
}
});
}
public List findByCriteria(Class clazz, Criterion... criterion)
throws BaseDAOException {
// TODO 自动生成方法存根
return null;
}
public Object get(long id, Class clazz) throws BaseDAOException {
return this.getHibernateTemplate().get(clazz, id);
}
public Object getByCondition(final String hql, final Object[] args)
throws BaseDAOException {
List list = this.getHibernateTemplate().find(hql, args);
if(list!=null){
return list.get(0);
}
return null;
}
public Object load(long id, Class clazz) throws BaseDAOException {
return this.getHibernateTemplate().load(clazz, id);
}
public void save(Object object) throws BaseDAOException {
this.getHibernateTemplate().save(object);
}
public void update(Object object) throws BaseDAOException {
this.getHibernateTemplate().update(object);
}
public int updateByCondition(final String hql, final Object[] args)
throws BaseDAOException {
return (Integer)this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
for(int i=0;i<args.length;i++){
query.setParameter(i, args[i]);
};
return query.executeUpdate();
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -