Mybatis关联查询 一对多和多对一

27 篇文章 0 订阅
10 篇文章 0 订阅
  • 一对多的第一种写法

首先是有两张表(学生表Student和老师Teacher表),为了更易懂,这里只设置了最简单的几个必要字段。表结构如下图

Student表:

Teacher表:

  • 创建实体bean
Teacher.java:
public class Teacher {
 
	private Integer id;
	private String name;
	private String className;
	private List<Student> students;
 
	public List<Student> getStudents() {
		return students;
	}
 
	public void setStudents(List<Student> students) {
		this.students = students;
	}
 
	public Integer getId() {
		return id;
	}
 
	public void setId(Integer id) {
		this.id = id;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public String getClassName() {
		return className;
	}
 
	public void setClassName(String className) {
		this.className = className;
	}
 
}

Sfudent.java

public class Student {
 
	private Integer id;
	private String name;
	private Integer teacherId;
	private String className;
	private Teacher teacher;
	
 
	public Teacher getTeacher() {
		return teacher;
	}
 
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
 
	public Integer getId() {
		return id;
	}
 
	public void setId(Integer id) {
		this.id = id;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public Integer getTeacherId() {
		return teacherId;
	}
 
	public void setTeacherId(Integer teacherId) {
		this.teacherId = teacherId;
	}
 
	public String getClassName() {
		return className;
	}
 
	public void setClassName(String className) {
		this.className = className;
	} 

} 
  • 下面重点来了:配置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对应的是teacher中的主键id,而且需是表字段名,传给student的外键teacherId -->
			<id column="sid" property="id"/><!-- 这里的column对应的是下面查询的别名,而不是表字段名 -->
			<result column="sname" property="name"/><!-- property对应JavaBean中的属性名 -->
			<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>
  • 测试类:
  •  
    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">
<!-- 这里要注意的是column对应的是student中的外键,而且需是表字段名 -->
		</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} <!-- 这里要注意的是#{id}对应的id是student中的外键,而且需是表字段名 -->
	</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);
	}

 最后:当然如果不想配置这么麻烦的信息,可以直接写一个关联查询的SQL语句,返回结果直接由Map接受即可。不过这样就不太符合面向对象的理念了。

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus是一款MyBatis的增强工具,它提供了很多实用的功能,比如一对多关联查询。在MyBatis-Plus中,一对多关联查询可以通过使用@TableName注解和@TableField注解来实现。 假设我们有两张表,一张是学生表,另一张是课程表,一个学生可以选多门课程,那么我们就可以用一对多关联查询来查询某个学生选的所有课程。 首先,在学生表中定义一个属性List<Course> courses,并使用@TableField注解将该属性与课程表的外键关联起来: ``` public class Student { @TableId private Long id; private String name; @TableField(exist = false) private List<Course> courses; } ``` 然后,在课程表中定义一个属性Long studentId,并使用@TableField注解将该属性与学生表的主键关联起来: ``` public class Course { @TableId private Long id; private String name; @TableField("student_id") private Long studentId; } ``` 最后,我们使用MyBatis-Plus提供的wrapper类进行关联查询: ``` QueryWrapper<Student> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("id", studentId); List<Student> students = studentMapper.selectList(queryWrapper); for (Student student : students) { QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>(); courseQueryWrapper.eq("student_id", student.getId()); List<Course> courses = courseMapper.selectList(courseQueryWrapper); student.setCourses(courses); } ``` 以上就是MyBatis-Plus实现一对多关联查询的方法。如果您还有其他问题或需要进一步的帮助,请随时提出。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值