今天遇到了一个奇怪的错误,报错如下图所示:
org.springframework.dao.DataIntegrityViolationException: Error attempting to get column 'question_id' from result set. Cause: java.sql.SQLDataException: Unsupported conversion from LONG to java.sql.Timestamp
; Unsupported conversion from LONG to java.sql.Timestamp; nested exception is java.sql.SQLDataException: Unsupported conversion from LONG to java.sql.Timestampat org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:84)
实体类:
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("teacher_question")
@ApiModel(value="TeacherQuestion对象", description="")
public class TeacherQuestion implements Serializable {private static final long serialVersionUID = 1L;@TableId(value = "id", type = IdType.AUTO)private Integer id;@TableField("teacher_id")private Integer teacherId;@TableField("question_id")private Integer questionId;@TableField("createtime")private Date createtime;public TeacherQuestion(Integer teacherId, Integer questionId, Date createtime) {this.teacherId = teacherId;this.questionId = questionId;this.createtime = createtime;}
}
mapper类和mapper文件如下:
public interface TeacherQuestionMapper extends BaseMapper<TeacherQuestion> {}<?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="cn.tedu.straw.portal.mapper.TeacherQuestionMapper"><!-- 通用查询映射结果 --><resultMap id="BaseResultMap" type="cn.tedu.straw.portal.model.TeacherQuestion"><id column="id" property="id" /><result column="teacher_id" property="teacherId" /><result column="question_id" property="questionId" /><result column="createtime" property="createtime" /></resultMap></mapper>
数据库表如下:
测试类如下:
@SpringBootTest(classes = StrawPortalApplication.class)
@RunWith(SpringRunner.class)
public class TestTeacherQuestionMapper {@Resourceprivate TeacherQuestionMapper teacherQuestionMapper;@Testpublic void selectOne(){QueryWrapper queryWrapper=new QueryWrapper();queryWrapper.eq("teacher_id",4);queryWrapper.eq("question_id",44);List<TeacherQuestion> teacherQuestions = teacherQuestionMapper.selectList(queryWrapper);System.out.println(teacherQuestions);}}
反复测试了好多次,不管什么测试条件都是报Cause: java.sql.SQLDataException: Unsupported conversion from LONG to java.sql.Timestamp错误。
后来才想起来自己在实体类里加了一个带参数的构造方法:
public TeacherQuestion(Integer teacherId, Integer questionId, Date createtime) {this.teacherId = teacherId;this.questionId = questionId;this.createtime = createtime;}
这样就导致该实体类没有无参构造方法了,众所周知,一旦我们手动添加了带参数的构造方法,编译器就不会给我们添加默认的无参构造方法。导致mybatis无法new这个实体类。也就无法做映射。
解决的办法是:给该实体类手动添加一个无参构造方法。
public TeacherQuestion() {}