mybatis的in的sql语句报参数错误

2021-01-18  本文已影响0人  尘埃里的玄
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'selecYear1' not found. Available parameters are [selectYear1, selectYear2, selectYear3, param3, param1, param2]
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:78)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
    at com.sun.proxy.$Proxy151.selectList(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:223)
    at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForMany(MybatisMapperMethod.java:158)
    at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:76)
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:61)
    at com.sun.proxy.$Proxy159.loadRecentYearElectricity(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy160.loadRecentYearElectricity(Unknown Source)
    at com.sf.energy.nplatform.report.Service.ServicesImpl.SynthesisEnergyEfficiencyReportService.getDepartmentInfo(SynthesisEnergyEfficiencyReportService.java:87)
    at com.sf.energy.nplatform.report.POJO.ComprehensiveEnergyConsumptionBean.loadMainPartmentEnergyInfo(ComprehensiveEnergyConsumptionBean.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.bstek.ureport.definition.dataset.BeanDatasetDefinition.buildDataset(BeanDatasetDefinition.java:40)
    at com.bstek.ureport.definition.datasource.SpringBeanDatasourceDefinition.getDatasets(SpringBeanDatasourceDefinition.java:42)
    at com.bstek.ureport.build.ReportBuilder.buildDatasets(ReportBuilder.java:150)
    at com.bstek.ureport.build.ReportBuilder.buildReport(ReportBuilder.java:75)
    at com.bstek.ureport.console.html.HtmlPreviewServletAction.loadReport(HtmlPreviewServletAction.java:279)
    at com.bstek.ureport.console.html.HtmlPreviewServletAction.execute(HtmlPreviewServletAction.java:78)
    at com.bstek.ureport.console.UReportServlet.service(UReportServlet.java:81)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.ibatis.binding.BindingException: Parameter 'selecYear1' not found. Available parameters are [selectYear1, selectYear2, selectYear3, param3, param1, param2]
    at org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:212)
    at org.apache.ibatis.reflection.wrapper.MapWrapper.get(MapWrapper.java:45)
    at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122)
    at com.baomidou.mybatisplus.core.executor.AbstractBaseExecutor.createCacheKey(AbstractBaseExecutor.java:88)
    at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:135)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426)
    ... 59 more

其实这是作者本人犯得迷糊。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sf.energy.nplatform.report.mapper.ILoadComprehensiveEnergyConsumption">
    <select id="loadRecentYearElectricity" resultType="com.sf.energy.nplatform.report.vo.ElectricityStatementByRecentYear">
        select Partment,SelectYear,SUM(ZGross) AS meterAmount FROM t_montham WHERE SelectYear in (#{selecYear1},#{selectYear2},#{selectYear3}) AND Partment IN (SELECT Partment_ID FROM partment WHERE IsEnReport = 1) GROUP BY Partment,SelectYear
    </select>
    <select id="loadRecentYearWater" resultType="com.sf.energy.nplatform.report.vo.WaterStatementByRecentYear">
        select Partment,SelectYear,SUM(ZGross) AS waterAmount FROM t_monthwater WHERE SelectYear in (#{selecYear1},#{selectYear2},#{selectYear3}) AND Partment IN (SELECT Partment_ID FROM partment WHERE IsEnReport = 1) GROUP BY Partment,SelectYear
    </select>
    <select id="loadRecentYearGas" resultType="com.sf.energy.nplatform.report.vo.GasStatementByRecentYear">
        select Partment,SelectYear,SUM(ZGross) AS gasAmount FROM t_monthgas WHERE SelectYear in (#{selecYear1},#{selectYear2},#{selectYear3}) AND Partment IN (SELECT Partment_ID FROM partment WHERE IsEnReport = 1) GROUP BY Partment,SelectYear
    </select>
</mapper>
image.png

sql是可以直接这么写的,但是mybatis是不能这么写的。

    <select id="loadRecentYearGas" resultType="com.sf.energy.nplatform.report.vo.GasStatementByRecentYear">
        select Partment,SelectYear,SUM(ZGross) AS gasAmount FROM t_monthgas WHERE SelectYear in <foreach
            collection="selectYears" item="selectYear" index="index" open="(" close=")" separator=",">#{selectYear}</foreach> AND Partment IN (SELECT Partment_ID FROM partment WHERE IsEnReport = 1) GROUP BY Partment,SelectYear
    </select>

应该用foreach语法
参考博客:https://blog.csdn.net/u011781521/article/details/79669180

上一篇下一篇

猜你喜欢

热点阅读