多对一处理
复杂查询环境搭建
CREATE TABLE `teacher` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO teacher(`id`, `name`) VALUES (1, ‘秦老师‘);
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);
- 建立Student,Teacher实体类,其属性与数据库对应。StudentMapper.java,StudentMapper.xml等,并在mybatis-config中导入
public class Teacher {
private int id;
private String name;
}
public class Student {
private int id;
private String name;
private Teacher teacher;
}
按照查询嵌套处理StudentMapper.xml
<?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="com.dao.StudentMapper">
<resultMap id="Student_Teacher" type="com.pojo.Student">
<association property="teacher" column="tid" javaType="com.pojo.Teacher" select="getTeacher"/>
</resultMap>
<select id="getStudent" resultMap="Student_Teacher">
select *
from student
</select>
<select id="getTeacher" resultType="com.pojo.Teacher">
select * from teacher where id = #{id}
</select>
</mapper>
按照结果嵌套处理
<?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="com.dao.StudentMapper">
<resultMap id="Student_Teacher" type="com.pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="com.pojo.Teacher">
<result property="name" column="tname"/>
<result property="id" column="tid"/>
</association>
</resultMap>
<select id="getStudent" resultMap="Student_Teacher">
select s.id sid, s.name sname, t.name tname,t.id tid
from student s,
teacher t
where s.tid = t.id;
</select>
</mapper>
一对多处理
基本类
public class Teacher {
private int id;
private String name;
private List<Student> students;
}
public class Student {
private int id;
private String name;
private int tid;
}
public interface TeacherMapper {
Teacher getTeacher(@Param("tid") int id);
}
按照结果嵌套处理TeacherMapper.xml
<?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="com.dao.TeacherMapper">
<select id="getTeacher" resultMap="Teacher_Student">
select s.id sid, s.name sname, t.name tname, t.id tid
from student s,
teacher t
where s.tid = t.id and t.id = #{tid}
</select>
<resultMap id="Teacher_Student" type="com.pojo.Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" ofType="com.pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
</mapper>
按照查询嵌套处理
<?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="com.dao.TeacherMapper">
<resultMap id="Teacher_Student" type="com.pojo.Teacher">
<collection property="students" javaType="ArrayList" ofType="com.pojo.Student" select="getStudentByTeacherId" column="id"/>
</resultMap>
<select id="getTeacher" resultMap="Teacher_Student">
select *
from teacher
where id = #{tid}
</select>
<select id="getStudentByTeacherId" resultType="com.pojo.Student">
select *
from student
where tid = #{tid}
</select>
</mapper>
小结
关联-association 【多对一】
集合-collection 【一对多】
javaType & ofType
- javaType用来指定实体类中属性的类型
- ofType用来指定映射到List或者集合中的pojo类型,泛型中的约束类型
面试高频
- Mysql引擎
- innoDB底层原理
- 索引
- 索引优化
复杂查询,一对多,多对一,面试高频