package com.leo.pojo;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private Teacher teacher;
}
package com.leo.pojo;
import lombok.Data;
@Data
public class Teacher {
private int id;
private String name;
}
3.建立Mapper接口
package com.leo.dao;
public interface StudentMapper {
}
package com.leo.dao;
import com.leo.pojo.Teacher;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface TeacherMapper {
@Select("select * from teacher where id = #{tid}")
Teacher getTeacher(@Param("tid") int id);
}
package com.leo.dao;
import com.leo.pojo.Student;
import java.util.List;
public interface StudentMapper {
public List getStudent();
}
(2) mapper xml
select * from student
(3) 测试代码
package com.leo.dao;
import com.leo.pojo.Student;
import com.leo.pojo.Teacher;
import com.leo.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class StudentMapperTest {
@Test
public void getStudent() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List studentList = mapper.getStudent();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
}
(4)执行
2.按照结果嵌套处理(连表查询)
(1)mapper接口
package com.leo.dao;
import com.leo.pojo.Student;
import java.util.List;
public interface StudentMapper {
// 按照查询嵌套处理
public List getStudent();
// 按照结果嵌套处理
public List getStudent2();
}
(2) mapper xml
(3) 测试代码
package com.leo.dao;
import com.leo.pojo.Student;
import com.leo.pojo.Teacher;
import com.leo.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class StudentMapperTest {
@Test
public void getStudent() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List studentList = mapper.getStudent();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
@Test
public void getStudent2() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List studentList = mapper.getStudent2();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
}
(4)执行
三、一对多查询
1.实体pojo
package com.leo.pojo;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private int tid;
}
package com.leo.pojo;
import lombok.Data;
import java.util.List;
@Data
public class Teacher {
private int id;
private String name;
// 一个老师拥有多个学生
private List students;
}
2.dao
package com.leo.dao;
import com.leo.pojo.Teacher;
import org.apache.ibatis.annotations.Param;
public interface TeacherMapper {
// 获取指定老师下的所有学生及老师的信息(嵌套查询)
Teacher getTeacher(@Param("tid") int id);
// 获取指定老师下的所有学生及老师的信息(子查询)
Teacher getTeacher2(@Param("tid") int id);
}
package com.leo.dao;
public interface StudentMapper {
}
3.mapper.xml
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 = #{tid}
select * from teacher where id = #{tid}
select * from mybatis.student where tid = #{tid}