创建表

1
2
3
4
5
6
7
CREATE TABLE test
(
id integer NOT NULL
CONSTRAINT test_pk
PRIMARY KEY,
test_data jsonb
);

定义方法

1
2
3
4
5
6
7
8
9
@Modifying
@Query(
value = "INSERT INTO test (id, test_data) VALUES (:id, CAST(:testData AS JSONB))",
nativeQuery = true
)
fun saveTest(
@Param("id") id: Long,
@Param("testData") testData: String
): Int

调用代码

1
2
3
4
5
6
7
val id = 1L
val testData = listOf("1", "2", "3")
val testDataJsonStr = ObjectMapper().writeValueAsString(testData)

transactionTemplate.execute {
repository.saveTest(id, testDataJsonStr)
}