/*
构建HQL语句进行查询
*/
1.
Query query = session.createQuery("select s.id, s.name from Student s where s.name like ? ");
//用?占位符
query.setParameter(0, "%1%");
//添加查询条件
List students = query.list();
//返回查询列表
2.
//可以用in()来进行多条件查询,使用setParameterList把所有的查询的条件放入list中
//可以用 :参数 来传递参数
List students = session.createQuery("select s.id, s.name from Student s where s.id in(:myids) ")
.setParameterList(myids, new Object[]{1,2})
.list();
3.
//日期查询语句
List students = session.createQuery("select s.id, s.name from Student s where date_format(s.createTime, '%Y-%m')=? ")
.setParameterList(0, 2009-1)
.list();
4.
//日期段的查询
//首先构建一个日期对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List students = session.createQuery("select s.id, s.name from Student s where s.createTime between ? and ? ")
.setParameter(0, sdf.setparse("2009-1-1 00:00:00"))
.setParameter(1, sdf.setparse("2009-2-1 23:59:59"))
.list();
posted on 2009-03-05 16:20
simplye 阅读(6412)
评论(1) 编辑 收藏