码迷,mamicode.com
首页 > 其他好文 > 详细

HQL和Criteria

时间:2015-06-16 12:25:27      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

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();
	}

  

HQL和Criteria

标签:

原文地址:http://www.cnblogs.com/wujinsen/p/4580145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!