如下:
以User为例
@Repository @Transactional public class ReplyDao extends BaseDao { /** * 分页查询用户 */ public List<User> listUsers(int pageNo, int pageCount) { String hql = "from User"; Query query = getSession().createQuery(hql); if (pageNo > 0 && pageCount > 0) { query.setFirstResult((pageNo - 1) * pageCount); query.setMaxResults(pageCount); } return query.list(); } }
集成的BaseDao
package com.wql.tsp.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @Repository @Transactional public class BaseDao { @Autowired protected SessionFactory sessionFactory; protected Session getSession() { return sessionFactory.getCurrentSession(); } }
时间: 2024-12-05 14:27:23