一、多对一
- 要求:把所有学生所对应的这一个老师输出到一张表上(学生id,学生名字,老师id,老师名字)。
- 理解:多个学生有一个老师,学生查找老师。
maven依赖:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
项目文件:
- 需要在mybatis-config.xml配置中映射出你所要使用的类或接口的路径,这里 只用映射出StudentMaooer接口。
<mappers>
<mapper class="com.tan.dao.StudentMapper"/>
</mappers>
- 在StudentMapper接口中,写上方法getstudent()用来返回学生信息集。
List<Student> getStudent();
- 到StudentMapper.xml,首先导入头文件,不需要再去官网查找,找一找以前的项目,把配置文件中的红色框中的内容改为mapper,黄色框内容删除之后就可以正常使用了。
第一步,StudentMapper.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.tan.dao.studentMapper">
</mapper>
第二步,使用select,要注意的地方是这里需要用resultMap而不是resultType要想着,如果是单表查询就是用resultType,如果不是就使用resultMap。然后根据需求写出查找语句,这里使用别名的原因主要防止第三步中出现的异常。
<select id="getStudent" resultMap="StudentTeacher">
select s.id sid,s.name sname,t.name tname,t.id tid
from student s,teacher t
where s.tid=t.id;
</select>
第三步,使用resultMap建立SQL查询结果字段与实体属性的映射关系。
<result peoperty="实体类属性" column="数据库字段">
<resultMap id="StudentTeacher" type="com.tan.pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="com.tan.pojo.Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
</association>
</resultMap>
StudentMapper.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.tan.dao.StudentMapper">
<!--按照结果嵌套处理-->
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname,t.id tid
from student s,teacher t
where s.tid=t.id;
</select>
<resultMap id="StudentTeacher2" type="com.tan.pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="com.tan.pojo.Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
</association>
</resultMap>
</mapper>
测试类:
public class MyTest {
@Test
public void test(){
SqlSession session = MybatisUtils.getSqlSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudent();
for(Student student:studentList){
System.out.println(student);
}
session.close();
}
}
结果如下:
二、一对多
- 一对多就是把多对一反过来理解,一个老师有多个学生,具体代码和多对一差异不是很大。
TeacherMapper
public interface TeacherMapper {
List<Teacher> getTeacher(@Param("id") int id);
}
TeacherMapper.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.tan.dao.TeacherMapper">
<select id="getTeacher" resultMap="TeacherStudent">
select t.id tid,t.name tname,s.id sid,s.name sname,s.tid stid
from teacher t,student s
where s.tid=t.id and t.id=#{id}
</select>
<resultMap id="TeacherStudent" type="com.tan.pojo.Teacher">
<result property="id" column="tid" />
<result property="name" column="tname" />
<collection property="student" ofType="com.tan.pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="stid"/>
</collection>
</resultMap>
</mapper>
测试类:
public class MyTest {
@Test
public void test(){
SqlSession session = MybatisUtils.getSqlSession();
TeacherMapper sessionMapper = session.getMapper(TeacherMapper.class);
List<Teacher> teacher = sessionMapper.getTeacher(1);
System.out.println(teacher);
session.close();
}
}
结果: