我有一个这样的类(class)。
@Entity
@Table(name = "permissions")
public class Permission {
public Permission(){}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@JsonIgnore
@Version
private int version;
@JsonIgnore
@Convert(converter = JpaConverterJson.class)
@Column(name = "sub_category", columnDefinition = "json")
private List<Integer> subCategory = new ArrayList<>();
.
.
.
<Setter Getter>
....
我将整数列表存储为 JSON 类型
到数据库,如下所示。
[349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365]
作为 JSON,我是使用MYSQL。
我需要找到此列表中包含的所有权限。
例如:
我有 10 行permission
,如何找到某行中已经存在的350
。
如果我在 mysql 中进行查询,也许是这样的。
从权限中选择 *,其中 JSON_SEARCH(sub_category,'$[*]') = 350
entitManager.createQuery("从权限中选择g")
最佳答案
我找到了这个问题的解决方案。
我使用这样的查询。
SELECT DISTINCT permissions.*
FROM permissions, JSON_TABLE(sub_category, "$[*]" COLUMNS(nr INT PATH '$')) as ids where ids.nr = 349;
此查询将帮助您查找 349
在 JSON
中的位置,其值为 [123,349,350,452,344,67,676,2,4,666,7334,4353453,65,56,745 ]
NOTE: This query just work on Mysql Version 8.x
关于java - 如何查找 JSON 中包含的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56593445/