首先是我们需要先建表。
创建teacher表
(
id int(10) not null
primary key,
name varchar(30) null
);
创建student
create table student
(
id int(10) not null,
name varchar(10) null,
tid int(10) null,
constraint student_teacher_id_fk
foreign key (tid) references teacher (id)
);
要明白他们之间的住外键关系。
多对一查询
这里的多对一是指的多个学生对应一个老师。
创建相应的实体类
Teacher类
@Data
public class Teacher {
private int id;
private String name;
}
student类,虽然我们数据库中的字段是tid,但是我们这里是Teacher类。
@Data
public class Student {
private int id;
private String name;
private Teacher teacher;
}
创建接口,我们这里只需要创建学生接口就行,在这里因为学生是多个的所以返回值类型list类行。
List<Student> getStudent();
写相应的xml文件。
方法一 嵌套查询
首先是用以条sql语句查出来我们所需要的字段,然后我们在结果及映射中在进行字段和属性对应,association是对应到对象用的,javaType意思是属性对应实体类的类型,然后我们在再里面进行相应的字段和属性的对应。
<resultMap id="studentTeacher2" type="aw.pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="aw.pojo.Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
<select id="getStudent" resultMap="studentTeacher2">
select s.id sid,s.name sname,t.name tname from student s,teacher t where s.tid=t.id;
</select>
方法二子查询
这种方法是先查出所有的结果然后在映射集中进行相应的结果对应。
association 通这里也是使用对象, 这里和上一个嵌套的相比属性多了一个column()用来传递参数、select执行另一条select标签。另一条标签是查Teacher类。
<resultMap id="studentTeacher1" type="aw.pojo.Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<association property="teacher" column="tid" javaType="aw.pojo.Teacher" select="getTeacher"/>
</resultMap>
<select id="getStudent" resultMap="studentTeacher1">
select * from student;
</select>
<select id="getTeacher" resultType="aw.pojo.Teacher" >
select * from teacher where id=#{id};
</select>
首先是这两中方法不存在好与坏,嵌套sql语句麻烦,但是标签明了,子查询sql简单,标签复杂,根据自己情况使用。
测试类
public void test2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> student = mapper.getStudent();
for (Student student1 : student) {
System.out.println(student1);
}
sqlSession.close();
}
一对多
一个老师对应多个学生,一对多和多对一的实体类不同。
学生类
@Data
public class Student {
private int id;
private String name;
private int tid;
}
老师类,这里我们添加了一个学生集合。
@Data
public class Teacher {
private int id;
private String name;
private List<Student> student;
}
Teacher接口。
Teacher getTeacher(@Param("id") int id);
xml对应代码
使用的嵌套查询,这里的方法和多对一基本上差不多,<collection这个标签是代表集合的意思,因为我么属性相对应的是List<>所以我们使用collection 因为这里没有对应的实体类所以不用写javaType类型,ofType是代表泛型的类型。
<resultMap id="yiduiduo" type="aw.pojo.yiduiduo.Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="student" ofType="aw.pojo.yiduiduo.Student" >
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
<select id="getTeacher" resultMap="yiduiduo">
select s.id sid,s.name sname,t.id tid,t.name tname from student s,teacher t where s.tid=t.id and t.id=#{id}
</select>
子查询
用法基本上和前面的一样,这不过我们这里javaType类型是ArrayList。
<resultMap id="ss" type="aw.pojo.yiduiduo.Teacher" >
<collection property="student" column="id" javaType="ArrayList" ofType="aw.pojo.yiduiduo.Student" select="getStudent"/>
</resultMap>
<select id="getStudent" resultType="aw.pojo.yiduiduo.Student">
select * from student where tid=#{id}
</select>
<select id="getTeacher" resultMap="ss">
select * from teacher where id=#{id};
</select>