mybaits 报错之 mapper中传参limit 取出的数据是?

mapper中传参limit 取出的数据是?:

SSM 架构 通过 mapper 传参 map,写分页语句异常


问题描述

通过 map 传参, limit 分页,显示sql 语句中 数字作为字符串

    @RequestMapping("/workbench/activity/queryActivityConditionForPage.do")
    public @ResponseBody List<Activity> queryActivityConditionForPage(String pageNo,String pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("pageNo",pageNo);       // 这里应该放 startIndex么?
        map.put("pageSize",pageSize);
        List<Activity> activities = activityService.selectActivityByConditionForPage(map);
        return activities;
    }
  <select id="selectActivityByConditionForPage"  resultMap="BaseResultMap" >
    select u.name owner,a.name,a.start_date,a.end_date
    from tbl_activity a
    join tbl_user u
    on a.owner= u.id
    limit #{pageNo},#{pageSize}
  </select>

SQL: select u.name owner,a.name,a.start_date,a.end_date from tbl_activity a join tbl_user u on a.owner= u.id limit ?,?
Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘‘1’,‘10’’ at line 5
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘‘1’,‘10’’ at line 5


原因分析:

可以看到参数传过来了,但是向map中放的是 String 类型的,所以取出来的时候sql语句中 limit ‘1’,‘10’
虽然 map 是 <String,Object> ,但是放入数据的时候,放的是 String ,mapper.xml 拼接 sql 的时候会将其作为字符串


解决方案:

在向 map 中放数据的时候应该放 Integer

    @RequestMapping("/workbench/activity/queryActivityConditionForPage.do")
    public @ResponseBody List<Activity> queryActivityConditionForPage(Integer pageNo,Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("pageNo",pageNo);       // 这里应该放 startIndex么?
        map.put("pageSize",pageSize);
        List<Activity> activities = activityService.selectActivityByConditionForPage(map);
        return activities;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值