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 !"".equals(coname.trim())">
anda.CONAME=#{coname}
</if>
<if test="description !=null and !"".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"/>