java.sql.SQLDataException: Cannot determine value type from string ‘XXX‘

本文探讨了在MyBatis中如何通过结果映射正确处理一对一关系,解决查询学生和班级信息时遇到的SQLDataException。关键步骤包括检查字段映射和添加默认构造函数。实例展示了如何修复代码并成功运行测试用例。
摘要由CSDN通过智能技术生成

使用关联映射进行查询:一个学生属于一个班(一对一)

S_student实体类

package com.itheima.pojo;

public class S_student {
    private int id;
    private String name;
    private int age;
    private int cid;
    private C_class clas; // 把班级类作为属性

//    public S_student(){}

    public S_student(int id, String name, int age, int cid,C_class clas) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.cid = cid;
        this.clas=clas;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public C_class getClas() {
        return clas;
    }

    public void setClas(C_class clas) {
        this.clas = clas;
    }

    @Override
    public String toString() {
        return "S_student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", cid=" + cid +
                ", clas=" + clas +
                '}';
    }
}

C_class实体类

package com.itheima.pojo;

public class C_class {
    private int id;
    private String classname;

    public C_class(int id, String classname) {
        this.id = id;
        this.classname = classname;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getClassname() {
        return classname;
    }

    public void setClassname(String classname) {
        this.classname = classname;
    }

    @Override
    public String toString() {
        return "C_class{" +
                "id=" + id +
                ", classname='" + classname + '\'' +
                '}';
    }
}

StudentInfo

package com.itheima.dao;


import com.itheima.pojo.S_student;

import java.util.List;

public interface StudentInfo {
    // 查询每个学生对应的班级信息
    List<S_student> getStudentinfo();
}

StudentInfo.xml

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.dao.StudentInfo">

    <select id="getStudentinfo" resultMap="StudentClass">
        select s.age age,s.cid cid,s.id id,s.name name,c.classname classname,c.id classid
        FROM s_student s,c_class c
        WHERE s.cid=c.id
    </select>
    <resultMap id="StudentClass" type="com.itheima.pojo.S_student">
        <!--property是实体类中的名称,column是数据表中字段名称-->
        <result property="id" column="id"></result>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <result property="cid" column="cid"></result>
        <association property="clas" javaType="com.itheima.pojo.C_class">
            <result property="id" column="classid" />
            <result property="classname" column="classname"/>
        </association>
    </resultMap>

</mapper>

注意:在核心配置文件中映射路径
在这里插入图片描述
StudentInfoTest

package dao;

import com.itheima.dao.StudentInfo;
import com.itheima.pojo.S_student;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;

import java.util.List;

public class StudentInfoTest {
    @Test
    public void getPersonOneTest() {
        //第一个:获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        StudentInfo student = sqlSession.getMapper(StudentInfo.class);

        List<S_student> studentList = student.getStudentinfo();
        System.out.println(studentList);
        //第三步:关闭SqlSession
        sqlSession.close();
    }
}

运行报错

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'name' from result set.  Cause: java.sql.SQLDataException: Cannot determine value type from string '张三'
### The error may exist in com/itheima/dao/StudentInfo.xml
### The error may involve com.itheima.dao.StudentInfo.getStudentinfo
### The error occurred while handling results
### SQL: select s.age age,s.cid cid,s.id id,s.name name,c.classname classname,c.id classid         FROM s_student s,c_class c         WHERE s.cid=c.id
### Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'name' from result set.  Cause: java.sql.SQLDataException: Cannot determine value type from string '张三'

解决方法:
1、检查在进行结果映射的时候是否对应的字段名有写错
2、如果代码检查没有错,运行还是报错,在S_student实体类中加上默认的构造方法就可以了

在这里插入图片描述
最后测试StudentInfo测试类,运行结果如下:
在这里插入图片描述

  • 14
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

*neverGiveUp*

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值