常用hql语句

1、日期比较示例

String hql="from stu t where t.date >= '2012-05-01 00:00:00' and t.date <= '2012-05-10 23:59:59'";


2、boolean型的比较

String hql="from LinkMan t where t.valid=true";
String hql2="from LinkMan t where t.valid=false";


3、hql的order by中,不能t.organ.code方式来排,只能用t.organ来排序.否则,将导致organ属性为null的记录查不出来

4、一段示例代码

public List<LinkMan> query()
{
UserInfoLogin userInfo = (UserInfoLogin) ServletActionContext.getRequest().getSession().getAttribute("userInfo_login");
String userId = userInfo.getUserId();

String clientname = ServletActionContext.getRequest().getParameter(Constants.SEARCH_ + "clientname");
String name = ServletActionContext.getRequest().getParameter(Constants.SEARCH_ + "name");
String post = ServletActionContext.getRequest().getParameter(Constants.SEARCH_ + "post");
String phone = ServletActionContext.getRequest().getParameter(Constants.SEARCH_ + "phone");
String tel = ServletActionContext.getRequest().getParameter(Constants.SEARCH_ + "tel");
String valid = ServletActionContext.getRequest().getParameter(Constants.SEARCH_ + "valid");
String homeAddress = ServletActionContext.getRequest().getParameter(Constants.SEARCH_ + "homeAddress");
String addTime_begin = ServletActionContext.getRequest().getParameter(Constants.SEARCH_ + "addTime_begin");
String addTime_end = ServletActionContext.getRequest().getParameter(Constants.SEARCH_ + "addTime_end");

StringBuffer hql = new StringBuffer("from LinkMan t where t.client.salesMan.id='" + userId + "'");

if (clientname != null && !"".equals(clientname.trim()))
{
hql.append(" and t.client.name like '%" + clientname + "%'");
}
if (name != null && !"".equals(name.trim()))
{
hql.append(" and t.name like '%" + name + "%'");
}
if (post != null && !"".equals(post.trim()))
{
hql.append(" and t.post like '%" + post + "%'");
}
if (phone != null && !"".equals(phone.trim()))
{
hql.append(" and t.phone like '%" + phone + "%'");
}
if (tel != null && !"".equals(tel.trim()))
{
hql.append(" and t.tel like '%" + tel + "%'");
}
if (homeAddress != null && !"".equals(homeAddress.trim()))
{
hql.append(" and t.homeAddress like '%" + homeAddress + "%'");
}
if (valid != null && !"".equals(valid.trim()))
{
hql.append(" and t.valid=" + valid + "");
}
if (addTime_begin != null && !"".equals(addTime_begin.trim()))
{
hql.append(" and t.addTime >= '" + addTime_begin + "'");
}
if (addTime_end != null && !"".equals(addTime_end.trim()))
{
hql.append(" and t.addTime <= '" + addTime_end + " 23:59:59'");
}
hql.append(" order by t.addTime desc");

return dao.queryByHql_page(hql.toString());
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值