Mybatis_06 对应关系
多对一: 使用关联 association
一对多: 使用集合 collection
创建SQL表:
CREATE TABLE `teacher` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8
CREATE TABLE `student`(
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`tid` INT(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fktid` (`tid`),
CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO `student`(`id`,`name`,`tid`) VALUES('1','小赵','1');
INSERT INTO `student`(`id`,`name`,`tid`) VALUES('2','小李','1');
INSERT INTO `student`(`id`,`name`,`tid`) VALUES('3','小张','1');
INSERT INTO `student`(`id`,`name`,`tid`) VALUES('4','小红','1');
INSERT INTO `student`(`id`,`name`,`tid`) VALUES('5','小明','1');
INSERT INTO `teacher`(`id`,`name`) VALUES('1','赵老师');
表的关系:
测试环境搭建:
1、新建实体类Teacher、Student
2、建立Mapper接口,StudentMapper,TeacherMapper
3、建立Mapper.xml文件
4、在核心配置文件中绑定Mapper接口
5、测试查询是否成功
mapper接口
mapper对应的mapper.xml文件
多对一:
按照查询嵌套处理:
<!--
思路:
1、查询所有的学生信息
2、根据查出来的学生的tid,查询对应的老师!
-->
<select id="getStudent" resultMap="StudentTeacher">
select * from mybatis.student;
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!-- 复杂的属性需要单独处理
对象: association
集合: collection
-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from mybatis.teacher where id=#{tid};
</select>
按照结果嵌套处理:
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname
from mybatis.student s,mybatis.teacher t
where s.tid=t.id;
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
Mysql 多对一查询方式:
- 子查询
- 联表查询
一对多:
新建实体类:
Student:
public class Student {
private int id;
private String name;
private int tid;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", tid=" + tid +
'}';
}
}
按照结果嵌套处理:
TeacherMapper:
public interface TeacherMapper {
// List<Teacher> getTeacher();
//获取指定老师下的所有学生
Teacher getTeacher(@Param("tid") int id);
Teacher getTeacher2(@Param("tid") int id);
}
<!-- 按结果嵌套查询 -->
<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid,s.name sname,t.name tname,t.id tid
from mybatis.student s,mybatis.teacher t
where s.tid=t.id and t.id=#{tid};
</select>
<resultMap id="TeacherStudent" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
按照查询嵌套处理:
<!--子查询-->
<select id="getTeacher2" resultMap="TeacherStudent2">
select * from mybatis.teacher where id= #{tid};
</select>
<resultMap id="TeacherStudent2" type="Teacher">
<collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherID" column="id"/>
</resultMap>
<select id="getStudentByTeacherID" resultType="Student">
select *
from mybatis.student
where tid=#{tid};
</select>
测试:
@Test
public void test2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
@Test
public void test3(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher2(1);
System.out.println(teacher);
sqlSession.close();
}
总结:
- 关联 association 【多对一】
- 集合 collection 【一对多】
- JavaType & ofType
- JavaType 用来指定实体类中属性的类型
- ofType 用来指定映射到List或者集合中的pojo类型, 泛型中的约束类型