接口传多字段参数查询

3 篇文章 0 订阅
2 篇文章 0 订阅

接口传多字段查询方法介绍

1.案例

抽奖明细记录查询,需要查询draw_record表,通过user_id、pool_code关键字段查询,需要传递多个code值(card_1,card_2),获取包含这些code值的记录。
在这里插入图片描述

2.总体思路

前端传递参数poolCode数组保存多个code值,参数传递转化为逗号分割的字符串,传递到接口,接口直接使用该字段通过mysql内置的查询方法,如in pool_code、find_in_set(pool_code, ‘’)查询。

个人觉得接口传递对象然后解析的方式不如直接传递字符串简便,且不容易出现转化方面的错误,有更好的方法欢迎留言。

3.编写方法

var poolCodes = new Array('card_1','card_2');
var jsonStr = poolCodes.toString();
$.ajax({
	url:"xxx",
	type:'get',
	dataType:'json',
	data:{
		userId:"xxx",
		poolCode:jsonStr,
	},
	...
})

方法一:使用in查询

@Query(value = "select * from draw_record where user_id = :userId and pool_code in :poolCode")
public List<Object> findByCodes(@Param("userId") String userId, @Param("poolCode") String poolCode);

方法二:使用find_in_set查询

@Query(value = "select * from draw_record where user_id = :userId and find_in_set(pool_code, :poolCode)")
public List<Object> findByCodes(@Param("userId") String userId, @Param("poolCode") String poolCode);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值