gpt4 book ai didi

postgresql JSON查询语句like及其spring boot jpa的使用

转载 作者:知者 更新时间:2024-03-13 01:55:20 26 4
gpt4 key购买 nike

在开发中经常遇到数据库存储的json数据,需要查询出来,比如:

select * from event  where resource_type='COMPUTE' and event_type='START' and deleted=FALSE  
      and resource_data ->>'eniIds' like   '%ocid1.vnic.oc1.ap-tokyo-1.abxhiljruybcqzennifrmchug63gyfpki4ano6ylo3hmse2atlkl2rjx635a%'

但是同样的代码放在spring boot JPA中就会报错。

@Query(
		value = "select * from event  where resource_type='COMPUTE' and event_type='START' and deleted=FALSE " +
			" and resource_data ->>'eniIds' like %'+:eniId+'%",
		nativeQuery = true
	)
	fun findUserIdAndTenantIdByEniId(@Param("eniId") eniId:String):List<Event>

正确的SQL写法:

select * from event  where resource_type='COMPUTE' and event_type='START' and deleted=FALSE  
      and resource_data ->>'eniIds' like concat('%','ocid1.vnic.oc1.ap-tokyo-1.abxhiljruybcqzennifrmchug63gyfpki4ano6ylo3hmse2atlkl2rjx635a','%')

正确的JAVA JPA写法:

@Query(
		value = "select * from event  where resource_type='COMPUTE' and event_type='START' and deleted=FALSE " +
			" and resource_data ->>'eniIds' like concat('%',:eniId,'%')",
		nativeQuery = true
	)
	fun findUserIdAndTenantIdByEniId(@Param("eniId") eniId:String):List<Event>

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com