上一次我们入门了mybatis的单表查询,这一次我们学习mybatis的高级映射中最简单的一对一映射
我们想象这样一个一对一的场景,一个班级对应一个班主任,那么这就是一种一对一的关系
上次我们已经建立了tb_teacher表,这次我们再建立一张tb——classes表
这个表中的t_id就是关联tb_teacher的id主键
我们来写相应的pojo类,其中要包含Teacher对象
package com.pojo;
import java.util.List;
public class Classes {
private int id;
private String name;
private Teacher teacher;
public Classes() {
}
public Classes(int id, String name, Teacher teacher) {
this.id = id;
this.name = name;
this.teacher = teacher;
}
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 Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Classes{" +
"id=" + id +
", name='" + name + '\'' +
", teacher=" + teacher +
'}';
}
}
下一步写classesMapper(方法一)
<?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="mapper.classesMapper">
<select id="findClasses" resultMap="classesMap">
select * from tb_class c inner join tb_teacher t on t.id = c.t_id
</select>
<resultMap id="classesMap" type="Classes">
<id property="id" column="id"/>
<result property="name" column="c_name"/>
<association property="teacher" javaType="Teacher">
<id property="id" column="id"/>
<result property="name" column="c_name"/>
<result property="role" column="role"/>
</association>
</resultMap>
</mapper>
下一步写classesMapper(方法二)
select中的值一定是按逐渐查询返回的一个对象的id,column中的值是外键的名称
<?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="mapper.classesMapper">
<select id="findClasses" resultMap="classesMap">
select * from tb_class c inner join tb_teacher t on t.id = c.t_id
</select>
<resultMap id="classesMap" type="Classes">
<id property="id" column="id"/>
<result property="name" column="c_name"/>
<association property="teacher" select="mapper.teacherMapper.findTeacherById" column="t_id"/>
</resultMap>
</mapper>
然后再写ClassesDao
package com.dao;
import com.pojo.Classes;
import com.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class ClassesDao {
private SqlSession sqlSession = MyBatisUtil.getSqlSession();
private String sql = "mapper.classesMapper.";
public List<Classes> findClasses(){
sql +="findClasses";
List<Classes> list = sqlSession.selectList(sql);
return list;
}
}
最后就是测试了
package com.test;
import com.dao.ClassesDao;
import com.dao.TeacherDao;
import com.pojo.Classes;
import com.pojo.Teacher;
import java.util.List;
public class Test {
public static void main(String[] args) {
ClassesDao classesDao = new ClassesDao();
List<Classes> list = classesDao.findClasses();
for (Classes classes:list) {
System.out.println("id="+classes.getId()+" name="+classes.getName()+" t_id="+classes.getTeacher().getId());
}
}
}
测试结果如下
image.png
网友评论