首页 > 编程知识 正文

mysql子查询字段作为条件,mysql查询字符串

时间:2023-05-06 09:16:52 阅读:188605 作者:2247

 

今天测试碰到个有趣的问题

很简单一个请求

http://127.0.0.1:8080/driverinfomanagecs/selectDriverCarInfolistByPartyId?partyid=565613848

contrller里面也很简单 就是一个mybatis的级联查询,查出的数据json返回

@RequestMapping("/selectDriverCarInfolistByPartyId") public ResponseVO<List<CarDriverInfo>> selectDriverCarInfolistByPartyId(String partyid) { if (StringUtils.isBlank(partyid)) { return ResponseVO.error("partyid不能为空"); } Pattern pattern = Pattern.compile(PatternUtil.NUMBER); if(!pattern.matcher(partyid).matches()) { return ResponseVO.error("partyid格式不正确"); } Integer partyId = Integer.parseInt(partyid); int count = driverInfoManageService.selectDriverCarInfoCountByPartyId(partyId); if(count < 1) { return ResponseVO.error("没有查到该车队信息"); } List<CarDriverInfo> info = driverInfoManageService.selectDriverCarInfolistByPartyId(partyId); if (info == null) { return ResponseVO.error("没有查到该车队信息"); } else { return ResponseVO.successWithData("查询成功", info, count); } }

 

用Postman测试了一下好的,然后我再参数partyid后面又加了个1 变成?partyid=5656138481

再去测试发现报错了 

报了个

Exception in thread "main" java.lang.NumberFormatException: For input string: "5656138481"

一看是数字啊 应该对的 再一次 好像超出int的范围了 然后观察数据库 发现

`partyId` int(19) NOT NULL

int(19) 的话 我用Long接收就好了 

 

把controller改成

@RequestMapping("/selectDriverCarInfolistByPartyId") public ResponseVO<List<CarDriverInfo>> selectDriverCarInfolistByPartyId(String partyid) { if (StringUtils.isBlank(partyid)) { return ResponseVO.error("partyid不能为空"); } Pattern pattern = Pattern.compile(PatternUtil.NUMBER); if(!pattern.matcher(partyid).matches()) { return ResponseVO.error("partyid格式不正确"); } Long partyId = Long.parseLong(partyid); int count = driverInfoManageService.selectDriverCarInfoCountByPartyId(partyId); if(count < 1) { return ResponseVO.error("没有查到该车队信息"); } List<CarDriverInfo> info = driverInfoManageService.selectDriverCarInfolistByPartyId(partyId); if (info == null) { return ResponseVO.error("没有查到该车队信息"); } else { return ResponseVO.successWithData("查询成功", info, count); } }

ok 正常 参数加长也不报错了

 

后来一想 为什么不直接改成用String接收呢

就把controller改成

@RequestMapping("/selectDriverCarInfolistByPartyId") public ResponseVO<List<CarDriverInfo>> selectDriverCarInfolistByPartyId(String partyid) { if (StringUtils.isBlank(partyid)) { return ResponseVO.error("partyid不能为空"); } int count = driverInfoManageService.selectDriverCarInfoCountByPartyId(partyid); if(count < 1) { return ResponseVO.error("没有查到该车队信息"); } List<CarDriverInfo> info = driverInfoManageService.selectDriverCarInfolistByPartyId(partyid); if (info == null) { return ResponseVO.error("没有查到该车队信息"); } else { return ResponseVO.successWithData("查询成功", info, count); } }

 

然后有趣的事情就发生了

 我把参数改成?partyid=565613848aaaaa 一样能正常查到565613848的数据 改成?partyid=aaaaa565613848 就只能查到partyid=0的数据 一想 不会是mysql把我进行了什么运算吧

直接在数据库试了试

结果如下

字母在后:

字母在前:

 

 

个人理解 mysql会从左到右开始读取 一旦遇到非数字则视作后面的所有字符值为0 无论后面是否有数字
(565613848+0【aaabbb】)=565613848

(0【aaabbb565613848】)=0

保存一下 以防不测……

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。