Article Directory
〇, demand
Demand: find the student corresponding to the specified teacher
1. Build the environment
Many-to-one is like multiple students corresponding to one teacher. Many-to-one query is based on multiple tables. First, set up the MySQL
environment:
CREATE DATABASE MyBatis_DB;
USE MyBatis_DB;
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');
The results shown below in Table construction:
Open idea
, a new maven
item, and then pom.xml
injected into the file mysql
, MyBatis
, Junit
, LOG4J
dependent
Then create the entity classes corresponding to the two tables:
Teacher
Table corresponding entity class:
package com.wzq.pojo;
import java.util.List;
public class Teacher {
private int id;
private String name;
private List<Student> students;
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 List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
", students=" + students +
'}';
}
}
Student
Table corresponding entity class:
package com.wzq.pojo;
public class Student {
private int id;
private String name;
private int tid;
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 getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", tid=" + tid +
'}';
}
}
2. Nested query according to the result
First TeacherMapper
write the method in:
Teacher getTeacherById1(@Param("tid") int id);
Then TeacherMapper.xml
write in sql
:
<select id="getTeacherById1" resultMap="Teacher_Student1">
select
t.id tid,
t.name tname,
s.id sid,
s.name sname
from
teacher t,
student s
where
t.id = s.tid and t.id = #{tid};
</select>
<resultMap id="Teacher_Student1" type="Teacher">
<id property="id" column="tid" />
<result property="name" column="tname" />
<collection property="students" ofType="Student">
<id property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
Final test:
@Test
public void getTeacherById1Test(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacherById1(1);
System.out.println(teacher);
sqlSession.close();
}
result:
[com.wzq.dao.TeacherMapper.getTeacherById1]-==> Preparing: select t.id tid, t.name tname, s.id sid, s.name sname from teacher t, student s where t.id = s.tid and t.id = ?;
[com.wzq.dao.TeacherMapper.getTeacherById1]-==> Parameters: 1(Integer)
[com.wzq.dao.TeacherMapper.getTeacherById1]-<== Total: 5
Teacher{
id=1, name='刘老师', students=[Student{
id=1, name='小明', tid=1}, Student{
id=2, name='小红', tid=1}, Student{
id=3, name='小张', tid=1}, Student{
id=4, name='小李', tid=1}, Student{
id=5, name='小王', tid=1}]}
Three, according to query nesting processing
Same as the above steps, writing method:
Teacher getTeacherById2(@Param("tid") int id);
Then TeacherMapper.xml
write in sql
:
<select id="getTeacherById2" resultMap="Teacher_Student2">
select * from teacher where id = #{tid};
</select>
<resultMap id="Teacher_Student2" type="Teacher">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="students" javaType="ArrayList" ofType="Students" select="getStudentByTeacherId" column="id"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
select * from student where tid = #{tid};
</select>
Final test:
@Test
public void getTeacherById2Test(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacherById2(1);
System.out.println(teacher);
sqlSession.close();
}