您的当前位置:首页正文

mybatis使用用Provider去实现SQL拼接 错误记录

2024-11-29 来源:个人技术集锦

昨天使用Provider进行拼接sql,出现了一个Exception比较懵圈,先将我写的代码与错误cope出来。

    @UpdateProvider( type = SalaryProvider.class,method = "calTempTotal")
    void updateTempTotal(List<SalaryItemEntity> salaryItems);
public class SalaryProvider {

    /**计算合计*/
    public String calTempTotal(List<SalaryItemEntity> salaryItems){
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE t_salary set total = ( 0 ");
        for (SalaryItemEntity salaryItem : salaryItems){
            if (salaryItem.getType() == 1){
                sql.append(" + "+salaryItem.getValue());
            }else if(salaryItem.getType() == -1){
                sql.append(" - "+salaryItem.getValue());
            }
        }

        sql.append(" ) ");
        System.out.println(sql.toString());
        return sql.toString();
    }
}

最后拼接的字段可以像这样的:UPDATE t_salary set total = ( 0 -wage1 + wage2 + wage10);

出现的错误与这个拼接的代码没有关系,错误是mapper与Provider参数不对应错误

Error invoking SqlProvider method (com.xichuan.mapper.SalaryProvider.calTempTotal).  Cause: org.apache.ibatis.binding.BindingException: Parameter 'arg0' not found. Available parameters are [collection, list]

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error invoking SqlProvider method (com.xichuan.mapper.SalaryProvider.calTempTotal).  Cause: org.apache.ibatis.binding.BindingException: Parameter 'arg0' not found. Available parameters are [collection, list]
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
	at com.sun.proxy.$Proxy97.update(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:294)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
	at com.sun.proxy.$Proxy121.updateTempTotal(Unknown Source)
	at com.xichuan.service.impl.SalaryServiceImpl.uploadSalary(SalaryServiceImpl.java:397)
	at com.xichuan.controller.SalaryController.uploadSalary(SalaryController.java:97)
	at com.xichuan.controller.SalaryController$$FastClassBySpringCGLIB$$dcbf4d5a.invoke(<generated&
显示全文