MyBatis学习笔记(四) MyBatis关系映射

本文详细介绍了MyBatis框架下如何实现一对一和一对多的关系映射,通过具体的XML配置文件和Java接口方法,展示了如何在AddressMapper、StudentMapper和GradeMapper中实现数据的查询,包括地址、学生和年级之间的关联查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工程结构

一对一关系实现

AddressMapper.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.java1234.mappers.AddressMapper">

    <resultMap type="Address" id="AddressResult">
	<result property="id" column="id"/>
	<result property="sheng" column="sheng"/>
	<result property="shi" column="shi"/>
	<result property="qu" column="qu"/>
    </resultMap>
	
    <select id="findById" parameterType="Integer" resultType="Address">
	select * from t_address where id=#{id}
    </select>

</mapper> 

AddressMapper.java

public interface AddressMapper {
    public Address findById(Integer id);
}

StudentMapper.xml

<resultMap type="Student" id="StudentResult">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <association property="address" column="address" select="com.java1234.mappers.AddressMapper.findById"></association>
</resultMap>

<select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
    select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
</select>

<!-- 此处省略Student类的增删改查 -->

StudentMapper.java新增方法findStudentWithAddress

public Student findStudentWithAddress(Integer id);

测试类

@Test
public void testFindStudentWithAddress() {
    logger.info("查询学生(带地址)");
    Student student=studentMapper.findStudentWithAddress(2);
    System.out.println(student);
}

一对多关系实现

一个年级中有很多学生、

GradeMapper.xml

<resultMap type="Grade" id="GradeResult">
    <result property="id" column="id"/>
    <result property="gradeName" column="gradeName"/>
    <!-- Grade类中有属性List<Student> students -->
    <collection property="students" column="id" select="com.java1234.mappers.StudentMapper.findByGradeId"></collection>
</resultMap>

<select id="findById" parameterType="Integer" resultMap="GradeResult">
    select * from t_grade where id=#{id}
</select>

GradeMapper.java

public interface GradeMapper {
    public Grade findById(Integer id);
}

StudentMapper.xml

<resultMap type="Student" id="StudentResult">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <association property="address" column="addressId" select="com.java1234.mappers.AddressMapper.findById"></association>
    <association property="grade" column="gradeId" select="com.java1234.mappers.GradeMapper.findById"></association>
</resultMap>

<select id="findByGradeId" resultMap="StudentResult" parameterType="Integer">
    select * from t_student where gradeId=#{gradeId}
</select>

StudentMapper.java新增方法findByGradeId

public Student findByGradeId(Integer gradeId);

Test

@Test
public void testFindGradeWithStudents() {
    logger.info("查询年级(带学生)");
    Grade grade=gradeMapper.findById(1);
    System.out.println(grade);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值