HQL和Criteria

HQL:

public boolean doCreate(Dept vo) throws Exception {
		return this.sessionFactory.getCurrentSession().save(vo) != null;
	}

	@Override
	public boolean doUpdate(Dept vo) throws Exception {
		String hql = "UPDATE Dept AS d SET d.title=? WHERE d.deptid=?";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, vo.getTitle());
		query.setInteger(1, vo.getDeptid());
		return query.executeUpdate() > 0;
	}

	@Override
	public boolean doRemove(Integer id) throws Exception {
		String hql = "DELETE FROM Dept AS d WHERE d.deptid=?";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, id);
		return query.executeUpdate() > 0;
	}

	@Override
	public Dept findById(Integer id) throws Exception {
		return (Dept) this.sessionFactory.getCurrentSession().get(Dept.class,
				id);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<TCatalogInfo> findAll() throws Exception {
		String hql = "FROM TCatalogInfo AS tc";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}
@SuppressWarnings("unchecked")
	@Override //分页
	public List<TCatalogInfo> findAll(String column, String keyWord,
			Integer currentPage, Integer lineSize) throws Exception {
		String hql = "FROM TCatalogInfo AS tc WHERE tc." + column + " LIKE ?";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, "%" + keyWord + "%");//查询的关键字
		query.setFirstResult((currentPage - 1) * lineSize);//开始页
		query.setMaxResults(lineSize);//最大加载的页数
		return query.list();
	}

	@Override
	public Integer getAllCount(String column, String keyWord) throws Exception {
		String hql = "SELECT COUNT(tc.cid) FROM TCatalogInfo AS tc WHERE tc."
				+ column + " LIKE ?";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, "%" + keyWord + "%");
		return ((Long) query.uniqueResult()).intValue();
	}

  

Criteria:

public List<Admin> findAll() throws Exception {
		Criteria criteria = this.sessionFactory.getCurrentSession()
				.createCriteria(Admin.class);
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Admin> findAll(String column, String keyWord,
			Integer currentPage, Integer lineSize) throws Exception {
		Criteria criteria = this.sessionFactory.getCurrentSession()
				.createCriteria(Admin.class);
		criteria.add(Restrictions.like(column, "%" + keyWord + "%"));
		criteria.setFirstResult((currentPage - 1) * lineSize);
		criteria.setMaxResults(lineSize);
		return criteria.list();
	}

	@Override
	public Integer getAllCount(String column, String keyWord) throws Exception {
		Criteria criteria = this.sessionFactory.getCurrentSession()
				.createCriteria(Admin.class);
		ProjectionList plist = Projections.projectionList();
		plist.add(Projections.rowCount(), "count");
		criteria.setProjection(plist); // 增加统计函数集合
		criteria.add(Restrictions.like(column, "%" + keyWord + "%"));
		return ((Long) criteria.uniqueResult()).intValue();
	}

  

posted @ 2015-06-16 10:43  雨下个不停  阅读(237)  评论(0编辑  收藏  举报