1:数据库中所存的json数组
数据表名为:pay_order,存放这个json数组的字段为:commodityInfo
[{
"id": 2,
"price": 6,
"remaining": 2,
"information": "这是静宁县生产的苹果,质量口感上佳",
"commodityName": "苹果"
}, {
"id": 17,
"price": 180,
"remaining": 2,
"information": "笔记本优选 健康塑体",
"commodityName": "站立办公升降台"
}, {
"id": 30,
"price": 100,
"remaining": 1,
"information": "内外兼修的充电利器,以一抵五,差旅标配",
"commodityName": "智能插线板-Pro版"
}]
2:sql语句查询代码
SELECT
p.id,
JSON_EXTRACT( p.commodityInfo -> '$[0 ]', '$.id' ) AS commodityId,
JSON_EXTRACT( p.commodityInfo -> '$[0]', '$.price' ) AS commodityPrice,
JSON_EXTRACT( p.commodityInfo -> '$[0]', '$.remaining' ) AS commodityRemaining,
REPLACE ( JSON_EXTRACT( p.commodityInfo -> '$[0]', '$.information' ), '"', '' ) AS information,
REPLACE ( JSON_EXTRACT( p.commodityInfo -> '$[0]', '$.commodityName' ), '"', '' ) AS commodityName
FROM
pay_order p
查询结果如下:
这个查询语句只能查询json数组长度为1的,现在我们要想办法把$[0 ]这里的0循环,然后sql用UNION ALL连接符连接起来,进行查询,连接后的sql
SELECT
p.id,
JSON_EXTRACT( p.commodityInfo -> '$[0 ]', '$.id' ) AS commodityId,
JSON_EXTRACT( p.commodityInfo -> '$[0]', '$.price' ) AS commodityPrice,
JSON_EXTRACT( p.commodityInfo -> '$[0]', '$.remaining' ) AS commodityRemaining,
REPLACE ( JSON_EXTRACT( p.commodityInfo -> '$[0]', '$.information' ), '"', '' ) AS information,
REPLACE ( JSON_EXTRACT( p.commodityInfo -> '$[0]', '$.commodityName' ), '"', '' ) AS commodityName
FROM
pay_order p UNION ALL
SELECT
p.id,
JSON_EXTRACT( p.commodityInfo -> '$[1 ]', '$.id' ) AS commodityId,
JSON_EXTRACT( p.commodityInfo -> '$[1]', '$.price' ) AS commodityPrice,
JSON_EXTRACT( p.commodityInfo -> '$[1]', '$.remaining' ) AS commodityRemaining,
REPLACE ( JSON_EXTRACT( p.commodityInfo -> '$[1]', '$.information' ), '"', '' ) AS information,
REPLACE ( JSON_EXTRACT( p.commodityInfo -> '$[1]', '$.commodityName' ), '"', '' ) AS commodityName
FROM
pay_order p
现在要实现这样的,就需要用到mybatis里的mapper.xml里面的foreach循环了
3:利用mybatis的mapper.xml的foreach循环sql进行遍历json数组
具体代码如下
<select id="getCommentPageListCount" parameterType="map" resultType="map">
SELECT
*
FROM
(
<foreach collection="jsonArray" separator=" UNION ALL " index="index" item="json">
SELECT
p.id AS orderId,
JSON_EXTRACT( p.commodityInfo -> '$[${index}]', '$.id' ) AS commodityId,
JSON_EXTRACT( p.commodityInfo -> '$[${index}]', '$.price' ) AS commodityPrice,
JSON_EXTRACT( p.commodityInfo -> '$[${index}]', '$.remaining' ) AS commodityRemaining,
REPLACE ( JSON_EXTRACT( p.commodityInfo -> '$[${index}]', '$.information' ), '"', '' ) AS information,
REPLACE ( JSON_EXTRACT( p.commodityInfo -> '$[${index}]', '$.commodityName' ), '"', '' ) AS commodityName
FROM
pay_order p
</foreach>
) pc
limit #{pageNo},#{pageSize};
</select>
这里面传的jsonArray这个参数,就需要查询出来你所要查询的json数组的最大值,然后定义一个数组传进来即可,主要用到的是index它的下标进行循环。
4:查询json数组的最大长度
sql代码如下:
SELECT
MAX( json_length( p.commodityInfo ) )
FROM
pay_order p
然后查询出来这个长度,定义一个此长度的数组jsonArray,存入map中传入即可,Java代码如下:
int jsonLength = userCommentDao.findMaxJsonLength(userTel);
int jsonArray[]=new int[jsonLength];
Map<String, Object> para=new HashMap<String, Object>();
para.put("jsonArray",jsonArray);
para.put("pageNo",pageNo*pageSize);//这里可以传入你需要的其他参数
para.put("pageSize",pageSize);
总结:
这样就可以用sql进行json数组的解析,只不过这我觉得是偷巧的方法,但是可以实现目的,解析之后就可以进行分页以及联合多表查询。
本人第一次写博客,如有错误之处,敬请指出。
版权声明:本文为XueFW123原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。