HQL string_to_map()使用
时间: 2024-01-05 12:04:08 浏览: 37
在 HQL 中,我们可以使用 `map()` 函数将一个字符串表示的键值对转换成一个 Map 对象,具体实现可以参考以下代码:
```sql
SELECT map(split(trim(str), ',' , ':')[0], split(trim(str), ',' , ':')[1])
FROM table
```
该查询语句首先使用 `split()` 函数将字符串按照逗号和冒号分割成数组,然后使用 `trim()` 函数去除每个数组元素的空格,最后使用 `map()` 函数将第一个数组元素作为键,第二个数组元素作为值,构造成一个 Map 对象并返回。需要注意的是,`split()` 函数返回的是一个数组,需要使用数组下标 `[0]` 和 `[1]` 获取键和值。
在实际使用中,你需要将 `table` 替换成你的表名,`str` 替换成你的包含键值对的字符串列名,最终查询结果将返回一个 Map 对象。
相关问题
@Override public Map<String, Object> selectOrderDetails(String currPage, String pageLimit, String sortType, String value, List<Map<String, Object>> searchConditions, JSONObject jsonObj) { Long id = jsonObj.getLong("id"); Map<String, Object> data = new HashMap<String, Object>(); String hql = " from MisHROrderDetails order WHERE order.orderPlanId = " + id ; data = commonDao.findByQueryMapResult(hql, null, null, currPage, pageLimit, sortType, searchConditions); return data; }
这段代码是一个Java方法,其中的参数包括当前页码、每页显示数量、排序类型、搜索条件、JSON对象等。该方法通过使用Hibernate Query Language(HQL)查询数据库中的订单详情数据,其中的查询条件是订单计划的ID。最终,该方法返回一个包含查询结果的Map集合。这段代码中的commonDao是一个通用的数据访问对象,用于执行数据库查询操作。
StringBuilder hql=new StringBuilder(); hql.append("select status "); hql.append(" from smsserver_out "); hql.append(" where status!='U' and user_id:=affairIds"); Map<String, Object> param = new HashMap<>();如果affairIds为多个参数的话 该怎么改
如果affairIds为多个参数的话,可以将它们放入一个List中,然后在HQL语句中使用IN关键字来匹配多个参数。下面是代码示例:
```java
List<Integer> affairIds = Arrays.asList(1, 2, 3); // affairIds为多个参数
StringBuilder hql = new StringBuilder();
hql.append("select status ");
hql.append(" from smsserver_out ");
hql.append(" where status!='U' and user_id IN (:affairIds)"); // 使用IN关键字匹配多个参数
Map<String, Object> param = new HashMap<>();
param.put("affairIds", affairIds); // 将参数放入Map中
```
需要注意的是,这里使用IN关键字时,参数名需要加上冒号(:)前缀。同时,将参数放入Map中时,需要使用参数名作为键。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)