本人对QBC语法不是很熟悉,不过还是分享一下,有时候QBC的确效率很高,熟练掌握的话应该说是很方便的,先前博客已经讲了QBC的一些用法,今天我们复习一下用QBC进行日期范围的查询。代码如下:
Dao层中的方法:
// HQL语言
public List findByHql(Integer page, Integer row, Date startDate,
Date endDate) {
try {
Query query = session
.createQuery(
"from Student stu where stu.date > :startDate and stu.date <= :endDate")
.setTimestamp("startDate", startDate).setTimestamp(
"endDate", endDate);
query.setFirstResult((page - 1) * row);
query.setMaxResults(row);
return query.list();
} catch (RuntimeException re) {
throw re;
}
}
// QBC语言
public List findByLikeExampleByDate(Student instance, Integer page,
Integer row, Date startDate, Date endDate) {
try {
List results = getSession().createCriteria("com.tjcyjd.Student")
.setFirstResult((page - 1) * row).setMaxResults(row).add(
Expression.between("date", startDate,
endDate)).addOrder(Order.asc("StudentId"))
.add(
Example.create(instance).enableLike(
MatchMode.ANYWHERE)).list();
return results;
} catch (RuntimeException re) {
throw re;
}
}