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);
}