按条件检索hql语句

本文介绍了一种通过Java实现的HQL模糊查询方法,可根据用户名、真实姓名、时间范围等多种条件进行灵活组合查询。该方法适用于Hibernate框架中对数据库进行复杂条件筛选的场景。

按条件模糊检索的hql语句拼装

public List<TuserN1> searchUser(String userName, String userTrueName,
			String time, String type, String jibie, String stat) {
		String hql = "from TuserN1 where 1=1";
		if (null != userName && !"".equals(userName)) {
			hql += " and userName like '%" + userName + "%' ";
		}
		if (null != userTrueName && !"".equals(userTrueName)) {
			hql += " and trueName like '%" + userTrueName + "%' ";
		}
		if (null != time && !"".equals(time)) {
			hql += " and time >=to_date( '"
					+ time
					+ " 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and time <=to_date( '"
					+ time + " 23:59:59', 'yyyy-mm-dd hh24:mi:ss')";
		}
		if (null != type && !"".equals(type)) {
			Long  ss=	Long.decode(type);
			hql += " and userType =" + ss;
		}
		if (null != jibie && !"".equals(jibie)) {
			Long  ji=Long.decode(jibie);
			hql += " and userLevel =" + ji;
		}
		if (null != stat && !"".equals(stat)) {
			Long  sstat=Long.decode(stat);
			hql += " and userState =" + sstat;
		}
		return getSession().createQuery(hql).list();
	}

留下,以备不时之需奋斗


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值