欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

常用hql语句

程序员文章站 2022-06-17 19:52:48
...
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());		
}

相关标签: hql 日期比较