Hibernate Regexp MySQL [英] Hibernate Regexp MySQL

查看:133
本文介绍了Hibernate Regexp MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



问题:

  SELECT * FROM table WHERE regexp column'\d'



解决方案:

转到我的答案。



希望这有助于您。

解决方案

基本上,要在Hibernate中使用MySQL regexp函数,我们需要创建一个SQLFunctionTemplate。

<首先:创建一个名为AppMySQLDialect的类,并从MySQLDialect继承,然后覆盖空的构造函数,最后注册regexp函数:

现在,如何做到这一点: / p>

  public class AppMySQLDialect extends MySQLDialect {
public AppMySQLDialect(){
super();
/ **
*用于评估MySQL中的正则表达式的函数
* /
registerFunction(regexp,new SQLFunctionTemplate(Hibernate.INTEGER,?1 REGEXP?2)) ;


$ / code $ / pre

好​​的,现在让我们按照下面的方式使用它: p>

  FROM实体E WHERE regexp(E.string2evaluate,'\ d')= 1 

创建您的HibernateQuery并执行。

I ask this question to show how MySQL and Hibernate work each other with Regular Expressions.

The problem:

SELECT * FROM table WHERE regexp column '\d'

Solution:

Go to my answer.

Hope this helps.

解决方案

Basically, to use MySQL regexp function in Hibernate we need to create a "SQLFunctionTemplate".

Now, how to do it:

First: Create a class called "AppMySQLDialect" and extends from MySQLDialect then override the empty constructor and finally register the regexp function:

public class AppMySQLDialect extends MySQLDialect {
    public AppMySQLDialect() {
        super();
        /**
         * Function to evaluate regexp in MySQL
         */
        registerFunction("regexp", new SQLFunctionTemplate(Hibernate.INTEGER, "?1 REGEXP ?2"));
    }
}

Ok, now lets use it as follow:

FROM Entity E WHERE regexp(E.string2evaluate, '\d') = 1

Create your HibernateQuery and execute.

这篇关于Hibernate Regexp MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆