sql插入表student的语句:
insert into student (id, name, tid) values ('9', '梁梁','2');
insert into student (id, name,tid) values ('7', '亮亮','2');
两张表:
所对应的实体类,学术类:
package com.kuang.pojo;
import lombok.Data;
public class Student {
private int id;
private String name;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", teacher=" + teacher +
'}';
}
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 Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public Student(int id, String name, Teacher teacher) {
this.id = id;
this.name = name;
this.teacher = teacher;
};
public Student(){};
private Teacher teacher;//利用这个来做到一个老师关联多个学生
}
老师:
package com.kuang.pojo;
import lombok.Data;
import java.util.List;
@Data
public class Teacher {
private int id;
private String name;
private List<Student>stus;//一个老师关联多个学生
}
多对一查询语句:
sql编辑器下面输入:
select s.id,s.name,t.name from student s,teacher t where s.tid = t.id;
显示:
嵌套查询:
按照查询嵌套处理
目录:
查询所有的学生信息
根据查询出来的学生tid,寻找对应的老师,子查询:
<mapper namespace="com.kuang.dao.Studenmapper">
<select id="getstudent" resultMap="StudenTeacher">
select * from student
</select>
<resultMap id="StudenTeacher" type="com.kuang.pojo.Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<association property="teacher" column="tid" javaType="com.kuang.pojo.Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="com.kuang.pojo.Teacher">
select * from teacher where id=#{id}
</select>
</mapper>
复杂的属性,我们需要单独处理,对象:association,集合:collection
测试类:
@Test
public void testStudent(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
Studenmapper stuma = sqlSession.getMapper(Studenmapper.class);
List<Student> stu = stuma.getstudent();
for(Student stu1:stu){
System.out.println(stu1);
}
sqlSession.close();
}
注意,type要用全类名,不然会报错
按照结果嵌套处理
测试类:
@Test
public void testStudent(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
Studenmapper stuma = sqlSession.getMapper(Studenmapper.class);
List<Student> stu = stuma.getstudent2();
for(Student stu1:stu){
System.out.println(stu1);
}
sqlSession.close();
}
Studenmapper:
package com.kuang.dao;
import com.kuang.pojo.Student;
import java.util.List;
public interface Studenmapper
{
public List<Student>getstudent();
public List<Student>getstudent2();
}
xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.Studenmapper">
<select id="getstudent2" resultMap="StudenTeacher2">
select s.id sid,s.name sname,t.name tname
from student s,teacher t
where s.tid=t.id;
</select>
<resultMap id="StudenTeacher2" type="com.kuang.pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="com.kuang.pojo.Teacher" >
<result property="name" column="tname"></result>
</association>
</resultMap>
</mapper>
这里因为把teacher拼成了teahcer导致报错,还有注意type的写法是全类名。
select s.id sid,s.name sname,t.name tname:
取了三个别名