mybatis-一对多、多对一
作者:互联网
多对一:
多个学生对一个老师 就是 多对一
对学生:多个学生关联一个老师
一对多:
一个老师 对 多个学生
对老师而言:一个老师 有 很多学生
SQL
#创建teachaer表
CREATE TABLE `teacher`(
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET=utf8;
#插入teachaer数据
INSERT INTO teacher(id,name) VALUES(1,'张老师');
#创建student表
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`)
) DEFAULT CHARACTER SET = utf8;
#插入student表数据
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、导入pom文件的 lomnok依赖,刷新 maven
2、创建工具类,实体类
3、写对应接口,一个实体类对应一个dao层的接口
4、在mapper文件中写对应接口的xml文件,通过namespace和dao层接口关联。
5、核心配置文件中对xml文件进行绑定注册。
6、测试类测试。
多对一处理
获取所有学生信息以及对应的老师信息
1、按照查询嵌套处理(有点难理解)
先去查学生信息,拿学生信息的tid去子查询中查询老师的信息,然后返回。
<mapper namespace="com.zy.dao.StudentMapper">
<!--
1、查询多有学生信息
2、根据查询的学生信息tid,查询老师信息
-->
<select id="getStudent" resultMap="StudentTeacher">
select * from 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>
//getTeacher 没有走mapper.xml文件 走的下面select
<select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{id}
</select>
</mapper>
打印结果
Student(id=1, name=张三, teacher=Teacher(id=1, name=张老师))
Student(id=2, name=李四, teacher=Teacher(id=1, name=张老师))
Student(id=3, name=王五, teacher=Teacher(id=1, name=张老师))
Student(id=4, name=赵六, teacher=Teacher(id=1, name=张老师))
Student(id=5, name=孙七, teacher=Teacher(id=1, name=张老师))
Student(id=6, name=张一, teacher=Teacher(id=2, name=刘老师))
Student(id=7, name=李二, teacher=Teacher(id=2, name=刘老师))
Student(id=8, name=王三, teacher=Teacher(id=2, name=刘老师))
Student(id=9, name=赵四, teacher=Teacher(id=2, name=刘老师))
Student(id=10, name=孙吴, teacher=Teacher(id=2, name=刘老师))
2、按照结果嵌套处理
<!--按照结果嵌套处理-->
<select id="getStudent" resultMap="teacherStudent2">
SELECT b.id AS sid, b.`name` AS sName,a.`name` AS tName
FROM teacher a,student b
WHERE a.id = b.tid
</select>
<resultMap id="teacherStudent2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sName"/>
<association property="teacher" javaType="Teacher">
<result property="name" column="tName"/>
</association>
</resultMap>
Student(id=1, name=张三, teacher=Teacher(id=0, name=张老师))
Student(id=2, name=李四, teacher=Teacher(id=0, name=张老师))
Student(id=3, name=王五, teacher=Teacher(id=0, name=张老师))
Student(id=4, name=赵六, teacher=Teacher(id=0, name=张老师))
Student(id=5, name=孙七, teacher=Teacher(id=0, name=张老师))
Student(id=6, name=张一, teacher=Teacher(id=0, name=刘老师))
Student(id=7, name=李二, teacher=Teacher(id=0, name=刘老师))
Student(id=8, name=王三, teacher=Teacher(id=0, name=刘老师))
Student(id=9, name=赵四, teacher=Teacher(id=0, name=刘老师))
Student(id=10, name=孙吴, teacher=Teacher(id=0, name=刘老师))
一对多处理
按照结果嵌套处理
实体类:
public class Teacher {
private int id;
private String name;
/* private Student student;*/
//一个老师关联多个学生
private List<Student> students;
}
接口:
public interface TeacherMapper {
Teacher getTeacherAndStudent(@Param("id") int id);
}
mapper.xml
<mapper namespace="com.zy.dao.TeacherMapper">
<select id="getTeacherAndStudent" resultMap="TeacherStudent" parameterType="int">
SELECT t.`name` AS teacherName,s.`name` AS studentName,s.id AS sid
FROM teacher t,student s
WHERE t.id = #{id}
AND t.id = s.tid
</select>
<resultMap id="TeacherStudent" type="Student">
<result property="id" column="id"/>
<result property="name" column="teacherName"/>
<!--javaTypey用于指定的属性的类型
集合中的泛型用ofType
-->
<collection property="students" ofType="Teacher">
<result property="name" column="studentName"/>
<result property="id" column="sid"/>
</collection>
</resultMap>
</mapper>
结果
Teacher(id=0, name=张老师, students=[Student(id=1, name=张三, tid=0), Student(id=2, name=李四, tid=0), Student(id=3, name=王五, tid=0), Student(id=4, name=赵六, tid=0), Student(id=5, name=孙七, tid=0)])
按照查询嵌套处理
mapper.xml
<!--===========================================-->
<select id="getTeacherAndStudent2" resultMap="TeacherStudent2" parameterType="int">
select * from teacher where id = #{id}
</select>
<resultMap id="TeacherStudent2" type="Teacher">
<collection property="students" column="id" javaType="ArrayList" ofType="Student" select="getStudent1"/>
</resultMap>
<select id="getStudent1" resultType="Student">
select * from student where tid = #{id}
</select>
小结:
1、关联 assciation 【多对一】
2、集合 collection 【一对多】
3、javaType & ofType
(1) javaType 用来指定实体类中属性的类型
(2)ofType 用来制动映射到list或者集合中的pojo类型,泛型中的约束类型
注意点:
保证sql可读性,尽量保证通俗易懂
注意一对多和多对一中,属性和字段的问题
如果问题不好排查,可以使用日志,推荐Log4j
标签:name,Teacher,id,tid,Student,mybatis,一对,teacher 来源: https://www.cnblogs.com/weijie1215/p/16616004.html