Article Directory
1. Many-to
More students are associated with the same teacher for more student-centered
Table 1.1 to create student, teacher tables in MySQL
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,`student``student``user``teacher`
`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');
1.2 write entity class
Teacher categories:
package com.zz.pojo;
import lombok.Data;
@Data
public class Teacher {
private int id;
private String name;
}
Student categories:
package com.zz.pojo;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
//学生关联老师
private Teacher teacher;
}
1.3 write Interface
TeacherMapper interface empty:
package com.zz.mapper;
public interface TeacherMapper {
}
StudentMapper Interface:
there are ways to get a list of student
package com.zz.mapper;
import com.zz.pojo.Student;
import java.util.List;
public interface StudentMapper {
//获取所有学生及学生对应老师的信息
List<Student> getStudents();
}
1.4 interface to write the corresponding configuration file
TeacherMapper.xml empty:
<?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.zz.mapper.TeacherMapper">
</mapper>
StudentMapper.xml profile:
<?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.zz.mapper.StudentMapper">
<!--获取所有学生及学生对应老师的信息:
1. 获取所有学生的信息
2. 获取所有学生的tid,然后再去老师表中查询这个id所对应的老师
-->
<resultMap id="StudentTeacher" type="Student">
<id property="id" column="sid"/>
<result property="name" column="sname"/>
<!--数据库中的tid是一个字段,而teacher是一个对象,所以不能使用result标签
这是多对一的情况,采用association标签(关联) student表关联teacher表,
且这个teacher对象是Teacher类型
-->
<association property="teacher" javaType="Teacher">
<id property="id" column="tid"/>
<result property="name" column="tname"/>
</association>
</resultMap>
<select id="getStudents" resultMap="StudentTeacher">
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>
1.5 determine the two xml files are bound to the core mybatis-config.xml configuration file
Scan method to bind packages, respectively, do not bind one
<mappers>
<!--通过扫描包来绑定-->
<package name="com.zz.mapper"/>
</mappers>
1.6 write test classes
Because it is many-using students binding teacher, so students write the corresponding test class
to write the test class StudentMapperTest
package com.zz.mapper;
import com.zz.pojo.Student;
import com.zz.utils.MyBatisUtils;
import org.junit.Test;
import java.util.List;
public class StudentMapperTest {
@Test
public void testGetStudents(){
StudentMapper mapper = MyBatisUtils.getSession().getMapper(StudentMapper.class);
List<Student> students = mapper.getStudents();
for (Student student : students) {
System.out.println(student);
}
}
}
operation result:
2. many
Available through student teacher, teacher-centered
steps are the same, only in this writing is different from the above
(1) preparation of an entity class
package com.zz.pojo;
import lombok.Data;
import java.util.List;
@Data
public class Teacher {
private int id;
private String name;
//老师对应多个学生 (包含:集合)
private List<Student> students;
}
package com.zz.pojo;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private int tid;
}
(2) write Interface
package com.zz.mapper;
import com.zz.pojo.Teacher;
public interface TeacherMapper {
//获取这个老师下的所有学生
public Teacher getTeacher(int id);
}
(3) write an interface configuration file
<?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.zz.mapper.TeacherMapper">
<resultMap id="TeacherStudent" type="Teacher">
<id property="id" column="tid"/>
<result property="name" column="tname"/>
<!--如果是集合(包含关系)-->
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<!--学生的tid就是老师的id-->
<result property="tid" column="tid"/>
</collection>
</resultMap>
<!--Teacher里有个list集合,所以不能使用resultType,采用resultMap-->
<select id="getTeacher" resultMap="TeacherStudent">
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=#{id}
</select>
</mapper>
(4) write test classes
package com.zz.mapper;
import com.zz.pojo.Teacher;
import com.zz.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class TeacherMapperTest {
@Test
public void testGetTeacher(){
SqlSession session = MyBatisUtils.getSession();
TeacherMapper mapper = session.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
}
}
The result:
Summary:
- Association - association [] many to one
- Collection - collection [many]
- & ofType the javaType
the javaType used to specify the type of entity class attribute
ofType used to specify the map to set pojo List type or, in generic constraint type!