mysql数据库int字段String条件查询

 

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

很简单一个请求

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

保存一下 以防不测……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值