【问题标题】:How do you perform a bitwise operation using Querydsl in Spring using Hibernate with MySQL如何在 Spring 中使用 Hibernate 和 MySQL 使用 Querydsl 执行按位运算
【发布时间】:2016-08-06 00:24:37
【问题描述】:

我在 mysql 表中有一个 int 列,用于存储与状态相关的位。我有兴趣根据状态字段中是否设置一个或多个位进行查询。

我尝试了以下方法:Expressions.booleanTemplate("(status & {0}) = 0", A_BIT_MASK); 但是,这给了我一个InvalidDataAccessApiUsageException: org.hibernate.QueryException: unexpected char: '&'

我将 Spring Data 与 Hibernate 和 MySQL 一起使用。我正在尝试使用 Querydsl 查询数据库。

【问题讨论】:

    标签: mysql spring hibernate querydsl


    【解决方案1】:

    首先你需要在休眠中创建按位

    public class MySQLDialect extends org.hibernate.dialect.MySQLDialect {
    
        public MySQLDialect() {
            super();
            registerFunction("bitand", new SQLFunctionTemplate(IntegerType.INSTANCE, "(?1 & ?2)"));
        }
    }
    

    然后您可以在 querydsl 或 hibernate 中引用它

    NumberTemplate<Integer> numberTemplate = Expressions.numberTemplate(Integer.class, "function('bitand', {0}, {1})", path, value);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-12
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 2014-09-12
      • 1970-01-01
      • 2018-02-04
      相关资源
      最近更新 更多