场景描述
在使用ssm框架写项目的时候,在构建mybatis中发现报错;
报错内容(如下):
Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'singerName' from result set. Cause: java.sql.SQLDataException: Cannot convert string '周杰伦' to java.sql.Time value
项目中的实体类:
public class music{
private Integer MucId;
private String MucName;
private String singerName;
private Time MucTime;
private String MucPath;
private String MucImg;
public Integer getMucId() {
return MucId;
}
public void setMucId(Integer mucId) {
MucId = mucId;
}
public String getMucName() {
return MucName;
}
public void setMucName(String mucName) {
MucName = mucName;
}
public String getSingerName() {
return singerName;
}
public void setSingerName(String singerName) {
this.singerName = singerName;
}
public Time getMucTime() {
return MucTime;
}
public void setMucTime(Time mucTime) {
MucTime = mucTime;
}
public String getMucPath() {
return MucPath;
}
public void setMucPath(String mucPath) {
MucPath = mucPath;
}
public String getMucImg() {
return MucImg;
}
public void setMucImg(String mucImg) {
MucImg = mucImg;
}
public music(String mucName, String singerName, Time mucTime, String mucPath, String mucImg) {
MucName = mucName;
this.singerName = singerName;
MucTime = mucTime;
MucPath = mucPath;
MucImg = mucImg;
}
@Override
public String toString() {
return "music{" +
"MucId=" + MucId +
", MucName='" + MucName + '\'' +
", singerName='" + singerName + '\'' +
", MucTime=" + MucTime +
", MucPath='" + MucPath + '\'' +
", MucImg='" + MucImg + '\'' +
'}';
}
}
Dao层:
public interface MusicAskDao {
List<music> findAll();
Integer addMusic(music music);
}
Mapper:
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yohane.dao.MusicAskDao">
<resultMap id="musicDo" type="com.yohane.pojo.music">
<id column="id" property="MucId"/>
<result column="name" property="MucName"/>
<result column="musicTime" property="MucTime"/>
<result column="musicPath" property="MucPath"/>
<result column="musicImg" property="MucImg"/>
</resultMap>
<select id="findAll" resultMap="musicDo">
select * from music_table
</select>
<insert id="addMusic" parameterType="com.yohane.pojo.music">
insert into music_table(name,singerName,musicTime,musicPath,musicImg) values(#{MucName},#{singerName},#{MucTime},#{MucPath},#{MucImg})
</insert>
</mapper>
test:
@Test
public void testMybatis(){
SqlSession sqlSession = LoadMybatis.getSqlSession();
MusicAskDao ask = sqlSession.getMapper(MusicAskDao.class);
List<music> musicList = ask.findAll();
for (music value:musicList){
System.out.println(value.toString());
}
}
数据库:
问题:
在查询所有数据的时候出现了
Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'singerName' from result set. Cause: java.sql.SQLDataException: Cannot convert string '周杰伦' to java.sql.Time value
报错
翻译过来就是:
原因:org.apache.ibatis.executor.result.ResultMapException:尝试从结果集中获取列“singerName”时出错。原因:java.sql.SQLDataException:无法转换字符串'周杰伦' 到java.sql.Time值
解决方案:
在使用添加Music到数据库中方法使创建了一个有参构造函数(id不包含,因为在数据中是自增)。
添加完之后,查询的时候就发现了报错。
原因是有了有参构造函数之后无参构造函数则不存在,添加上后即可解决。文章来源:https://www.toymoban.com/news/detail-509613.html
扩展问题:
mybatis实体为什么要提供一个无参的构造函数
Mybatis 会调用这个默认构造函数来构造实例,即实体类需要通过Mybatis进行动态反射生成。文章来源地址https://www.toymoban.com/news/detail-509613.html
到了这里,关于mybatis中Error attempting to get column ‘xx‘ from result set 问题解决的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!