hibernate criteria 判断varchar字段第3个字符为1
时间: 2024-02-19 22:03:39 浏览: 120
您可以使用 `Restrictions.sqlRestriction` 方法来实现这个需求。具体的代码可以如下:
```java
Criteria criteria = session.createCriteria(Entity.class);
criteria.add(Restrictions.sqlRestriction("substring(fieldName, 3, 1) = ?", "1", StringType.INSTANCE));
List<Entity> result = criteria.list();
```
其中,`fieldName` 是您要判断的 varchar 字段。`substring(fieldName, 3, 1)` 表示取 `fieldName` 字段从第 3 个字符开始的 1 个字符。`Restrictions.sqlRestriction` 方法可以用来添加 SQL 片段条件,第一个参数表示 SQL 片段,第二个参数表示 SQL 片段中的参数,第三个参数表示参数的类型。执行这个查询后,返回的结果集中,`fieldName` 字段的第三个字符一定是 1。
相关问题
hibernate criteria 判断varchar字段第一个字符为1
您可以使用 `Restrictions.like` 方法来实现这个需求。具体的代码可以如下:
```java
Criteria criteria = session.createCriteria(Entity.class);
criteria.add(Restrictions.like("fieldName", "1%"));
List<Entity> result = criteria.list();
```
其中,`fieldName` 是您要判断的 varchar 字段。这里使用了 `like` 操作符来匹配以 1 开头的字符串。`%` 符号表示匹配任意个字符。执行这个查询后,返回的结果集中,`fieldName` 字段的第一个字符一定是 1。
阅读全文
相关推荐
















