按条件模糊检索的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();
}留下,以备不时之需


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

被折叠的 条评论
为什么被折叠?



