There are two methods to implement fuzzy query in mysql: LIKENOTLIKE and REGEXPNOTREGEXP. next I will introduce their usage, I hope this tutorial will help you. In mysql, like and regexp are implemented for fuzzy queries.
------------------------
Many people know the usage of like. The most common case is select * from a where name like '% a % ';
'%' Indicates any character, which is like '*' in a regular expression. it has several usage methods: 'A % ',' % a % ', '% A', which respectively indicates the start, existence, and end of the start.
You can also use the '_' character, which represents any character. The effect is similar to '.' in a regular expression '.'.
Like is a comparison of all strings in this field, and the efficiency is not high.
------------------------
Regexp is the regular expression matching operator of mysql.
The regexp usage is as follows: select * from a where name regexp 'A'. you can refer to the regular expression for its syntax. like '*', '[a-z]', 'a | B ', 'CC $', '^ B {2} $ '.
------------------------
Regexp is not directly used in hibernate, but it can be implemented by adding a custom function.
public class MySQL5DialectRegexp extends org.hibernate.dialect.MySQLInnoDBDialect{ public MySQL5DialectRegexp() { super(); registerFunction( "regexp", new SQLFunctionTemplate(Hibernate.BOOLEAN, "?1 REGEXP ?2") ); }}
Here, org. hibernate. dialect. MySQLInnoDBDialect is the value of hibernate. dialect in hibernate configuration information. you can use the path of MySQL5DialectRegexp instead.
HQL statements can be written:
From A where regexp (a, 'a | B | c') = 1;