java查询条件 eq()内怎么写条件?
时间: 2024-01-21 12:16:14 浏览: 29
在 Java 中,使用 JPA 或者 Hibernate 等 ORM 框架进行数据库查询时,可以使用 `eq()` 方法指定查询条件。`eq()` 方法用于指定等于条件,其语法如下:
```
eq(String propertyName, Object value)
```
其中,`propertyName` 表示要查询的属性名,`value` 表示要查询的属性值。
举个例子,假设有一个 `User` 实体类,其属性包括 `id`、`name` 和 `age`,我们可以使用 JPA 查询框架进行查询。如果要查询 `name` 为 "Tom" 的用户,可以使用以下代码:
```
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<User> criteriaQuery = criteriaBuilder.createQuery(User.class);
Root<User> root = criteriaQuery.from(User.class);
Predicate namePredicate = criteriaBuilder.equal(root.get("name"), "Tom");
criteriaQuery.where(namePredicate);
List<User> users = entityManager.createQuery(criteriaQuery).getResultList();
```
其中,`equal()` 方法就是用来指定等于条件的,`root.get("name")` 表示获取 `User` 实体类中的 `name` 属性,`"Tom"` 表示要查询的属性值。以上代码的执行结果就是查询出 `name` 为 "Tom" 的所有用户。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)