使用关联映射进行查询:一个学生属于一个班(一对一)
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测试类,运行结果如下: