10分 |
//按id查找
public List<Comment> findCommentId(int cid) { List<Comment> findId = template.find(“from Comment u where u.cid=”””+cid+””””); if(findId==null||findId.size()<=0){ return null; } return findId; public List<Comment> queryForPage(String hql, int offset, int length) { |
10分 |
|
10分 |
几年前的代码…翻出来给你
package crm.dao; 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; public class QueryDAO extends HibernateDaoSupport{ public List queryByPage( final String hql, final Integer currentPage, final Integer pageSize, final Object ...objects){ //回调 return getHibernateTemplate().executeFind( //匿名类 实现接口 new HibernateCallback(){ public Object doInHibernate(Session session) throws HibernateException, SQLException { Query query=session.createQuery(hql); if(currentPage!=null && pageSize!=null){ query.setFirstResult((currentPage-1) * pageSize); query.setMaxResults(pageSize); } if(objects!=null){ for (int i = 0; i < objects.length; i++) { query.setParameter(i, objects[i]); } } return query.list(); } }); } public List queryByHQL(String hql,Object...objects){ return queryByPage(hql,null,null,objects); } public Object queryByUnique(String hql,Object...objects){ List list= queryByHQL(hql,objects); return list!=null && list.size()>0?list.get(0):null; } } |
10分 |
package com.aohy.dao.impl; import java.io.Serializable; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Repository; import com.aohy.dao.BaseDaoI; @Repository("baseDao") public class BaseDaoImpl<T> implements BaseDaoI<T> { @Resource private SessionFactory sessionFactory; private Session getCurrentSession() { return sessionFactory.openSession(); } @Override public Serializable save(T o) { return this.getCurrentSession().save(o); } @Override public T get(Class<T> c, Serializable id) { return (T) this.getCurrentSession().get(c, id); } @Override public T get(String hql) { Query q = this.getCurrentSession().createQuery(hql); List<T> l = q.list(); if (l != null && l.size() > 0) { return l.get(0); } return null; } @Override public T get(String hql, Map<String, Object> params) { Query q = this.getCurrentSession().createQuery(hql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } List<T> l = q.list(); if (l != null && l.size() > 0) { return l.get(0); } return null; } @Override public void delete(T o) { this.getCurrentSession().delete(o); } @Override public void update(T o) { this.getCurrentSession().update(o); } @Override public void saveOrUpdate(T o) { this.getCurrentSession().saveOrUpdate(o); } @Override public List<T> find(String hql) { Query q = getCurrentSession().createQuery(hql); return q.list(); } @Override public List<T> find(String hql, Map<String, Object> params) { Query q = getCurrentSession().createQuery(hql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } return q.list(); } @Override public List<T> find(String hql, Map<String, Object> params, int page, int rows) { Query q = getCurrentSession().createQuery(hql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list(); } @Override public List<T> find(String hql, int page, int rows) { Query q = getCurrentSession().createQuery(hql); return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list(); } @Override public Long count(String hql) { Query q = getCurrentSession().createQuery(hql); return (Long) q.uniqueResult(); } @Override public Long count(String hql, Map<String, Object> params) { Query q = getCurrentSession().createQuery(hql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } return (Long) q.uniqueResult(); } @Override public int executeHql(String hql) { Query q = getCurrentSession().createQuery(hql); return q.executeUpdate(); } @Override public int executeHql(String hql, Map<String, Object> params) { Query q = getCurrentSession().createQuery(hql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } return q.executeUpdate(); } } |
好复杂! |