The PosgreSQL database has a column with jsonb, I want to get the contents of this column not as a string, but as an object (convert json to an object on the fly, so that I do not run through the list with answers and do not create objects using Jackson) using myBatis. Is it possible?

I looked for the answer in the documentation, I did not find anything suitable. Thank you in advance.

    2 answers 2

    You can write your TypeHandler or use ready ones - mybatis-jackson , mybatis-gso n.

    If the set of fields in the jsonb object is known in advance, then using SQL you can pull out these fields from json and convert them into regular columns. And then map them to the Java object using standard MyBatis tools.

    For example, there is a data (jsonb) column in the table:

    {"id": "100", "tax": "4.5", "price": "10", "code": "YXCV"} 

    SQL query to represent jsonb fields as normal columns:

     SELECT name, description, data ->> 'id' AS id, data ->> 'tax' AS tax, data ->> 'price' AS price, data ->> 'code' AS code, FROM some_table; 

    The answer to this question here (there is another solution).