1. 程式人生 > >mysql資料庫int欄位String條件查詢

mysql資料庫int欄位String條件查詢

今天測試碰到個有趣的問題

很簡單一個請求

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);
        }
    }

然後有趣的事情就發生了

直接在資料庫試了試

結果如下

字母在後:

字母在前:

 

個人理解 mysql會從左到右開始讀取 一旦遇到非數字則視作後面的所有字元值為0 無論後面是否有數字
(565613848+0【aaabbb】)=565613848

(0【aaabbb565613848】)=0

儲存一下 以防不測……