0
0

More than 3 years have passed since last update.

SpringBoot + Spring JPAでMySQLのJSON型のValue検索

Posted at

概要

JPAを使用してのJSON型カラムのValue値検索について調べたので、共有として。
Specificationにて検索するケース。

結論

MySQLの関数を使用する。

JSON_EXTRACT

SELECT * FROM ATABLE WHERE JSON_EXTRACT("column_name", "$.key") = value;

なので、Spring JPAのSpecificationを使用する場合、

public Specification<T> jsonSearch(String key, String val) {
    return  (root, query, cb) -> {
        return cb.like(cb.function("JSON_EXTRACT", String.class, root.get("names"), cb.literal("$." + key)),
            "%" + val + "%");
    };
}

参考

Custom expression in JPA CriteriaBuilder

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0