代码报错:nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiating class PaperQuestionDetail with invalid types (PaperQuestion) or values (x). Cause: java.lang.IllegalArgumentException: argument type mismatch
这里报错的两个实体,PaperQuestionDetail继承了实体PaperQuestion,实体代码部分示例如下:
@Data
@ApiModel(value = "试卷题目", description = "试卷题目")
public class PaperQuestion {
@ApiModelProperty("主键")
private Long id;
@ApiModelProperty("试卷ID")
private Long paperId;
@ApiModelProperty("题目ID")
private Long quId;
@ApiModelProperty("题目标题")
private Integer title;
}
@Data
@ApiModel(value = "试卷题目详情类", description = "试卷题目详情类")
public class PaperQuestionDetail extends PaperQuestion {
@ApiModelProperty(value = "题目内容")
private String content;
@ApiModelProperty(value = "题目图片")
private String image;
public PaperQuestionDetail(PaperQuestion paperQuestion) {
this.setId(paperQuestion.getId());
this.setPaperId(paperQuestion.getPaperId());
this.setQuId(paperQuestion.getQuId());
this.setTitle(paperQuestion.getTitle());
}
在MyBatis读取数据时,实体映射和查询部分代码如下:
<resultMap id="BaseResultMap" type="PaperQuestion">
<id column="id" property="id"/>
<result column="paper_id" property="paperId"/>
<result column="qu_id" property="quId"/>
<result column="title" property="title"/>
</resultMap>
<resultMap id="ListResultMap"
type="PaperQuestionDetail"
extends="BaseResultMap">
<result column="content" property="content"/>
<result column="image" property="image"/>
</resultMap>
<select id="selectList" resultMap="ListResultMap">
SELECT * FROM table_xxx
</select>
因为未定义无参数构造函数,导致了调用selectList的时候出现了如题报错,解决这个问题,要么去除实体里自定义的构造函数 public PaperQuestionDetail(PaperQuestion paperQuestion),将其功能平移到impl层需要将PaperQuestion实体转换为PaperQuestionDetail的地方即可。
或者在PaperQuestionDetail实体定义中添加无参构造函数:
public PaperQuestionDetail(){
super();
}