Mybatis原生sql分页查询,报错SQLSyntaxErrorException踩坑

1.问题:


mapper层

public interface ProductMapper {
    public ArrayList<Product> getAllProduct();
    public Integer getProductCount();

    //分页查询
    public ArrayList<Product> findByPager(@Param("page") Integer page, @Param("limit") Integer limit);
}

mapper.xml

<select id="findByPager" resultType="Product">
        select * from mmall.product limit #{page},#{limit}
</select>

service层

public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductCategoryMapper productCategoryMapper;


    @Override
    public DataVO<ProductVO> findData(Integer page, Integer limit) {

        //分页
        page = (page-1)*limit;
        ArrayList<Product> products = productMapper.findByPager(page, limit);


        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");
        dataVO.setCount(productMapper.getProductCount());

//        ArrayList<Product> allProduct = productMapper.getAllProduct();

        ArrayList<ProductVO> productVOS = new ArrayList<>();

        for (Product product : products) {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product,productVO);
            productVO.setCategorylevelone(productCategoryMapper.getCategoryById(product.getCategoryleveloneId()));
            productVO.setCategoryleveltwo(productCategoryMapper.getCategoryById(product.getCategoryleveltwoId()));
            productVO.setCategorylevelthree(productCategoryMapper.getCategoryById(product.getCategorylevelthreeId()));
            productVOS.add(productVO);
        }

        System.out.println(productVOS);

        dataVO.setData(productVOS);

        return dataVO;
    }
}

controller层

public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/list")
    public DataVO list(Integer page,Integer limit){
        return productService.findData(page, limit);
    }
}

2.报错:

输入locahost:8080?&page=0,limt=2

### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: 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 '-2,2' at line 1
### The error may exist in file [C:\Users\15938\Desktop\后端-springboot\target\classes\com\ning\layui\mapper\ProductMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select * from mmall.product limit ?,?
### Cause: java.sql.SQLSyntaxErrorException: 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 '-2,2' at line 1
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: 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 '-2,2' at line 1] with root cause

3.解决:

按照错误提示“ near '-2,2' at line 1”,而代码里是“page = (page-1)*limit” 注意到输入的page至少应该从1开始。

后记:这种错误不值得发博客

Guess you like

Origin blog.csdn.net/li1593891525/article/details/120270084