Examples of what to look for like queries in HQL and more.

Source: Internet
Author: User
Tags stringbuffer
Public list getorgan (string organcode, string organname)  {        String hsql;       List list;          if  (Organcode != null && organcode.length ()  >  0)  {        hsql =  "from ab31 where  bae002 = ? and aab061 like ? ";         list = gethibernatetemplate (). Find (hsql, new  object[] { organcode,  '% '  + organName +  '% '  });         }        else {         hsql =  "FROM AB31 WHERE AAB061 LIKE ?";         list = gethibernatetemplate (). Find (Hsql,  '% '  + organName +  '% ');        }          return list;  }  
Public List Getorgan (string organcode, String organname) {
    string hsql;
    List List;

    if (Organcode! = null && organcode.length () > 0) {
     hsql = "from Ab31 where bae002 =? And aab061 like? ";
     List = Gethibernatetemplate (). Find (Hsql, new object[] {organcode, '% ' + organname + '% '});
     else {
     Hsql = "from Ab31 where aab061 like?";
     List = Gethibernatetemplate (). Find (Hsql, '% ' + organname + '% ');
     }

    return list;
}
I in the hql like query time according to SQL syntax with double quotation marks combination "%" + organname + "'%" can not find any results, so it must be noted here only single quotes, such as: '% ' + organname + '% ', multiple "placeholder" with new object[] {Organcode, organname} to populate
Introduction to Like syntax:
% denotes any number of characters
_ Denotes any one character
[ABCDEFG] denotes any one of the characters inside, just take one
[^c] means that the characters inside do not contain C
[A-z] denotes any of the bands, such as C or D, etc.
[^a-z] denotes any one of the non-interval

Escape '/' escape character, which means/after characters are not special characters.

Like '% product/% template% ' escape '/' can detect product% template special characters.

A detailed example is as follows: [Java] View Plain copy print? Public page getuserbycondition (user user, int pageno, int pagesize,  boolean element)  {   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;STRINGBUFFER&NBSP;HQL  = new stringbuffer ();           hql.append (" select user from user user where 1=1  ");            if (! Stringutils.isnullorempty (User.getexpert_name ())  && ! " -1 ". Equals (User.getexpert_name ())) {                hql.append (" realName like :realName ");            }           if (! Stringutils.isnullorempty (User.getexpert_level ())  && ! " -1 ". Equals (User.getexpert_level ())) {               hql.append ("&NBSP;EXPERTRANK&NBSP;=&NBSP;: expertrank  ");           }            if (! Stringutils.isnullorempty (User.getexpert_level ())  && ! " -1 ". Equals (User.getexpert_level ())) {                hql.append (" expertRank = :expertRank ");            }                       map<string,object> param = new hashmap<string , object> ();           param.put ("realName",  '% '  + user.getexpert_name ()  +  '% ');            param.put("Expertrank",  user.getexpert_level ()  );            Return super.pagedquery (Hql.tostring (),  pageno, pagesize, param);        }  

Public Page getuserbycondition (user user, int pageno, int pageSize, Boolean element) {
		StringBuffer hql = new Stringbu Ffer ();
		Hql.append ("Select User from user user where 1=1");
		if (! Stringutils.isnullorempty (User.getexpert_name ()) &&! " -1 ". Equals (User.getexpert_name ())) {
			hql.append (" Realname like:realname ");
		}
		if (! Stringutils.isnullorempty (User.getexpert_level ()) &&! " -1 ". Equals (User.getexpert_level ())) {
			Hql.append (" Expertrank =: Expertrank ");
		}
		if (! Stringutils.isnullorempty (User.getexpert_level ()) &&! " -1 ". Equals (User.getexpert_level ())) {
			Hql.append (" Expertrank =: Expertrank ");
		}
		
		map<string,object> param = new hashmap<string, object> ();
		Param.put ("realname", '% ' + user.getexpert_name () + '% ');
		Param.put ("Expertrank", User.getexpert_level ());
		Return Super.pagedquery (Hql.tostring (), PageNo, pageSize, param);
	}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.