比如:
一对多: 一个老师对应多个学生
多对一: 多个学生对应一个老师
建表+实体类
创建一个学生表和一个老师表
通过学生的tid与老师的id形成联系
SQLyog的建表代码
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','1');
INSERT INTO student (`id`,`name`,`tid`) VALUES ('2','学生2','1');
INSERT INTO student (`id`,`name`,`tid`) VALUES ('3','学生3','1');
INSERT INTO student (`id`,`name`,`tid`) VALUES ('4','学生4','1');
INSERT INTO student (`id`,`name`,`tid`) VALUES ('5','学生5','1');
pojo(使用了Lombok)
student:
package com.zy.pojo;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private int tid;
private Teacher teacher;
}
teacher
package com.zy.pojo;
import lombok.Data;
import java.util.List;
@Data
public class Teacher {
private int id;
private String name;
private List<Student> students;
}
多对一
多个学生对应一个老师
StudentMapper
package com.zy.mapper;
import com.zy.pojo.Student;
import java.util.List;
public interface StudentMapper {
List<Student> getStudentList();
List<Student> getStudentList2();
List<Student> getStudentList3();
}
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.zy.mapper.StudentMapper">
<select id="getStudentList" resultType="student">
select * from student
</select>
<select id="getStudentList2" resultMap="studentMap2">
select * from student
</select>
<resultMap id="studentMap2" type="student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="tid" column="tid"/>
<association property="teacher" javaType="teacher" select="getTeacherById" column="tid"/>
</resultMap>
<select id="getTeacherById" resultType="teacher">
select * from teacher where id=#{tid}
</select>
<select id="getStudentList3" resultMap="studentMap3">
select s.id sid,s.name sname,s.tid stid,t.id tid,t.name tname
from student s,teacher t
where s.tid=t.id
</select>
<resultMap id="studentMap3" type="student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="stid"/>
<association property="teacher" javaType="teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
</association>
</resultMap>
</mapper>
测试
getStudentList:
getStudentList2:
getStudentList3:
getStudentList2与getStudentList3代码不同,效果是相同的
一对多
一个老师对应多个学生
TeacherMapper
package com.zy.mapper;
import com.zy.pojo.Teacher;
import java.util.List;
public interface TeacherMapper {
List<Teacher> getTeacherList();
List<Teacher> getTeacherList2();
List<Teacher> getTeacherList3();
}
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.zy.mapper.TeacherMapper">
<select id="getTeacherList" resultType="teacher">
select * from teacher
</select>
<select id="getTeacherList2" resultMap="teacherMap2">
select * from teacher
</select>
<resultMap id="teacherMap2" type="teacher">
<result property="id" column="id"/>
<result property="name" column="name"/>
<collection property="students" javaType="ArrayList" ofType="student" select="getStudentById" column="id"/>
</resultMap>
<select id="getStudentById" resultType="student">
select * from student where tid=#{id}
</select>
<select id="getTeacherList3" resultMap="teacherMap3">
select t.id tid,t.name tname,s.id sid,s.name sname
from teacher t,student s
where t.id=s.tid
</select>
<resultMap id="teacherMap3" type="teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" javaType="ArrayList" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
</mapper>
测试
getTeacherList:
getTeacherList2:
getTeacherList3:
getTeacherList2与getTeacherList3代码不同,效果是相同的
无论是一对多还是多对一,重点都在写xml中的代码,合理利用resultMap可以写出多个表的CRUD