Mybatis关联查询之一对多和多对一XML如何配置?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
首先是有两张表(学生表Student和老师Teacher表,注:这里只是为了演示一对多和多对一的情况,请不要杠),为了更易懂,这里只设置了最简单的几个必要字段。表结构如下图
Student表:
Teacher表:
创建实体bean
Teacher.java:
import java.util.List;
public class Teacher {
private Integer id;
private String name;
private String className;
private List<Student> students;
// get、set方法省略
}
Sfudent.java
public class Student {
private Integer id;
private String name;
private Integer teacherId;
private String className;
private Teacher teacher;
// get、set方法省略
}
下面重点来了:配置Mapper.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.tz.mybatis.dao.studentDao">
<!-- ///一对多的第一种写法/// -->
<resultMap type="Teacher" id="teacherMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="students" ofType="Student" column="id">
<!-- 这里的column对应的是下面查询的别名,而不是表字段名 -->
<id column="sid" property="id"/>
<!-- property对应JavaBean中的属性名 -->
<result column="sname" property="name"/>
<result column="className" property="className"/>
</collection>
</resultMap>
<!-- 查询所有的老师级各自的所有学生 -->
<select id="getTeachers" parameterType="Teacher" resultMap="teacherMap">
SELECT
t.id,
t.NAME,
t.class_Name,
s.id AS sid,
s. NAME AS sname,
s.class_name as className
FROM
teacher t
LEFT JOIN student s ON t.id = s.teacher_id
</select>
</mapper>
测试类:
package com.tz.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import com.tz.mybatis.bean.Student;
import com.tz.mybatis.bean.Teacher;
public class TeacherTest {
private SqlSessionFactory sqlSessionFactory;
@Before
public void init() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void getTeachers() {
SqlSession session = sqlSessionFactory.openSession();
List<Teacher> list = session.selectList("com.tz.mybatis.dao.studentDao.getTeachers");
System.out.println(list);
}
}
下面给出第二种写法:
<!-- //一对多的第二种写法/ -->
<resultMap type="Teacher" id="teacherMaps">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="class_name" property="className"/>
<collection property="students" ofType="Student" select="getStudents" column="id">
</collection>
</resultMap>
<!-- 查询所有的老师级各自的所有学生 -->
<select id="getAllTeacher" parameterType="Teacher" resultMap="teacherMaps">
SELECT
t.id,
t.NAME,
t.class_name
FROM
teacher t
</select>
<select id="getStudents" parameterType="int" resultType="Student">
select
s.id,
s. NAME,
s.class_name as className
from student s
where teacher_id = #{id}
</select>
测试类:
@Test
public void getTeachers2() {
SqlSession session = sqlSessionFactory.openSession();
List<Teacher> list = session.selectList("com.tz.mybatis.dao.studentDao.getAllTeacher");
System.out.println(list);
}
查询学生信息(多对一):
首先还是配置文件:
<resultMap type="Student" id="studentMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="class_name" property="className"/>
<result column="teacher_id" property="teacherId"/>
<association property="teacher" select="getTeacher" column="teacher_id" javaType="Teacher">
<!-- 这里要注意的是column对应的是student中的外键,而且需是表字段名 -->
</association>
</resultMap>
<select id="getStudent" resultMap="studentMap">
SELECT
s.id,
s.name,
s.class_name,
s.teacher_id
FROM
student s
</select>
<select id="getTeacher" resultType="Teacher" parameterType="int">
SELECT
t.id,
t.name,
t.class_name as className
FROM teacher t
where id = #{teacher_id}
</select>
测试类:
@Test
public void getStudents() {
SqlSession session = sqlSessionFactory.openSession();
List<Student> list = session.selectList("com.tz.mybatis.dao.studentDao.getStudent");
System.out.println(list);
}