日期:2014-05-16 浏览次数:20347 次
package com.org.momo.bean; public class PageInfo { private Integer pageRows ; //每页行数 private Integer currentPage ; //当前页数 private Integer pageTotal ; //页面总数 public Integer getPageRows() { return pageRows; } public void setPageRows(Integer pageRows) { this.pageRows = pageRows; } public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public Integer getPageTotal() { return pageTotal; } public void setPageTotal(Integer pageTotal) { this.pageTotal = pageTotal; } }
package com.org.momo.dao; import java.util.List; import com.org.momo.bean.PageInfo; import com.org.momo.bean.Team; public interface TeamDao { public void insert(Team team) throws Exception ; public void deleteById(Integer id) throws Exception ; public Team findById(Integer id) throws Exception ; public List<Team> findAll() throws Exception ; public void update(Team team) throws Exception ; public List<Team> findAllPage(PageInfo pageInfo) throws Exception ; }
package com.org.momo.dao.hibernateTemplate; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.org.momo.bean.PageInfo; import com.org.momo.bean.Team; import com.org.momo.dao.TeamDao; import com.org.momo.util.HibernateUtil; /** * 此方法为Hibernate自身的类实现底层数据访问 * 另外一种实现方式就是:spring对Hibernate的支持HibernateTemplate(见下一个 * 类 TeamDaoHibernateTemplate.java * ) * */ public class TeamDaoHibernate implements TeamDao { private SessionFactory sessionFactory; public TeamDaoHibernate() { sessionFactory = HibernateUtil.getSessionFactory(); } public void insert(Team team) throws Exception { Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); session.save(team); transaction.commit(); session.close() ; } public void deleteById(Integer id) throws Exception { Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); String hql = "delete from Team where id=?"; Query query = session.createQuery(hql); query.setInteger(0, id); query.executeUpdate(); transaction.commit(); session.close(); } public void update(Team team) throws Exception { Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); session.merge(team); transaction.commit(); session.close(); } public List<Team> findAll() throws Exception { List<Team> teams = null; Session session = sessionFactory.openSession(); Query query = session.createQuery("from Team"); teams = query.list(); session.close(); return teams; } public Team findById(Integer id) throws Exception { Te