1. 程式人生 > >java.lang.IllegalArgumentException: Result Maps collection does not contain value forcom.imooc.bean

java.lang.IllegalArgumentException: Result Maps collection does not contain value forcom.imooc.bean

org.apache.ibatis.exceptions.PersistenceException:

### Error querying database.  Cause: java.lang.IllegalArgumentException: Result Maps collection does not contain value forcom.imooc.bean.CommandContent.content

### The error may exist incom/imooc/config/sqlxml/Command.xml

### The error may involveCommand.commandListQuery

### The error occurred whilehandling results

### SQL: select a.IDC_ID,a.CONAME,a.DESCRIPTION,b.ID,b.CONTENT,b.COMMAND_ID     from COMMAND a left join COMMAND_CONTENTb     on a.ID=b.COMMAND_ID      WHERE a.CONAME=?

### Cause: java.lang.IllegalArgumentException:Result Maps collection does not contain value forcom.imooc.bean.CommandContent.content

   atorg.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)

   atorg.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:122)

   atorg.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:113)

   atcom.imooc.dao.CommandDAO.commandListQuery(CommandDAO.java:26

)

   atcom.imooc.service.QueryService.queryByCommand(QueryService.java:37)

   atcom.imooc.servlet.AutoReplyServlet.doGet(AutoReplyServlet.java:24)

   atcom.imooc.servlet.AutoReplyServlet.doPost(AutoReplyServlet.java:32)

   atjavax.servlet.http.HttpServlet.service(HttpServlet.java:650)

   atjavax.servlet.http.HttpServlet.service(HttpServlet.java:731)

   atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)

   atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)

   atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)

   atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)

   atorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)

   atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)

   atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)

   atorg.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)

   atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)

   atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)

   atorg.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)

   atorg.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)

   atorg.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2516)

   atorg.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2505)

   atjava.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)

   atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)

   atorg.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

   atjava.lang.Thread.run(Thread.java:619)

CommandContent.xml:

<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE mapper

    PUBLIC "-//mybatis.org//DTDMapper 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="CommandContent">

 <resultMap type="com.imooc.bean.CommandContent"id=" CommandContentResult ">

   <id column="ID" jdbcType="INTEGER"property="id"/>

   <result column="CONTENT" jdbcType="VARCHAR"property="content"/>

   <result column="COMMAND_ID" jdbcType="VARCHAR" property="commandId"/>

 </resultMap>

</mapper>

Command.xml:

<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE mapper

    PUBLIC "-//mybatis.org//DTDMapper 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="Command">

 <resultMap type="com.imooc.bean.Command" id="CommandResult">

   <id column="C_ID" jdbcType="INTEGER"property="id"/>

   <result column="CONAME" jdbcType="VARCHAR"property="coname"/>

   <result column="DESCRIPTION" jdbcType="VARCHAR" property="description"/>

   <collection property="contentList"resultMap="CommandContent.Content"/>

   <!-- column對應的是sql語句中查詢的列的列名,有別名就是寫別名 -->

 </resultMap>

 <select id="commandListQuery" parameterType="com.imooc.bean.Command"  resultMap="CommandResult">

   select a.ID C_ID,a.CONAME,a.DESCRIPTION,b.ID,b.CONTENT,b.COMMAND_ID

   from COMMAND a left join COMMAND_CONTENT b

   on a.ID=b.COMMAND_ID

   <where>

     <if test="coname != nulland !&quot;&quot;.equals(coname.trim())">

         anda.CONAME=#{coname}

       </if>

       <if test="description !=null and !&quot;&quot;.equals(description.trim())">

         anda.DESCRIPTION like '%' #{description} '%'

       </if>

   </where>

 </select>

</mapper>

出現上述錯誤主要是因為select標籤內部的resultMap屬性指向的不正確

Command.xml一對多的配置中,resultMap不能隨便配置,要指向正確的多的一方的id,跨檔案設定要加上namespace

所以Command.xml中

<collection property="contentList"resultMap="CommandContent.Content"/>

要改為:

<collection property="contentList"resultMap="CommandContent.CommandContentResult"/>