MyBatis级联查询

本文详细介绍了使用MyBatis进行一对多、多对一以及多对多的级联查询操作。通过示例展示了如何在Student、Classes、Customer和Goods实体类中配置关联,并在对应的Mapper XML文件中编写SQL查询,实现数据的级联获取。
摘要由CSDN通过智能技术生成

MyBatis级联查询

  • ⭐一对多
    Student
package com.oyrf.frist;

import lombok.Data;

@Data
public class Student {
    private long id;
    private String name;
    private Classes classes;


}

Classes

package com.oyrf.frist;

import lombok.Data;

import java.util.List;
@Data
public class Classes {
    private long id;
    private String name;
    private List<Student> students;
}

StudentRepository.java

package com.oyrf.repository;

import com.oyrf.frist.Student;

public interface StudentRepository {
    Student findById(long id);

}

StudentRepository.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
        "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.oyrf.repository.StudentRepository">
    <resultMap id="studentMap" type="com.oyrf.frist.Student">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <association property="classes" javaType="com.oyrf.frist.Classes">
            <id column="cid" property="id"></id>
            <result column="cname" property="name"></result>
        </association>
    </resultMap>
    <select id="findById" parameterType="long" resultMap="studentMap">
        SELECT s.id,s.name,c.id AS cid,c.name AS cname FROM student s,classes c WHERE s.id = #{id} AND s.cid = c.id
    </select>
</mapper>
  • ⭐多对一
    ClassesRepository.java
package com.oyrf.repository;

import com.oyrf.frist.Classes;

public interface ClassessRepository {
    Classes findById(long id);
}

ClassesRepository.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
        "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.oyrf.repository.ClassessRepository">
    <resultMap id="classesMap" type="com.oyrf.frist.Classes">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <collection property="students" ofType="com.oyrf.frist.Student">
            <id column="sid" property="id"></id>
            <id column="sname" property="name"></id>
        </collection>
    </resultMap>
    <select id="findById" parameterType="long" resultMap="classesMap">
        select s.id as sid,s.name as sname,c.id,c.name from student s,classes c where c.id=#{id} and c.id=s.cid
    </select>
</mapper>
  • ⭐多对多
  1. 通过顾客的名字看顾客买了什么商品
    Customer.java
package com.oyrf.frist;

import lombok.Data;

import java.util.List;

@Data
public class Customer {
    private long id;
    private String name;
    private  List<Goods> goods;
}

CustomerRepository.java

package com.oyrf.repository;

import com.oyrf.frist.Customer;

public interface CustomerRepository {
    Customer findByName(String name);
}

CustomerRepository.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
        "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.oyrf.repository.CustomerRepository">
    <resultMap id="customerMap" type="com.oyrf.frist.Customer">
        <result column="cname" property="name"></result>
        <collection property="goods" ofType="com.oyrf.frist.Goods">
            <result column="gname" property="name"></result>
        </collection>
    </resultMap>
    <select id="findByName" parameterType="java.lang.String" resultMap="customerMap">
        SELECT c.`name` AS cname,g.name AS gname FROM customer c,goods g,customer_goods cg WHERE c.`name`=#{name} AND c.`id`=cg.`cid` AND g.`id`=cg.`gid`
    </select>

</mapper>
  1. 同过商品看哪些顾客买了该商品
    Goods.java
package com.oyrf.frist;

import lombok.Data;

import java.util.List;

@Data
public class Goods {
    private long id;
    private String name;
    private List<Customer> customers;
}

GoodsRepository.java

package com.oyrf.repository;

import com.oyrf.frist.Goods;

public interface GoodsRepository {
    Goods findByName(String name);
}

GoodsRepository.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
        "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.oyrf.repository.CustomerRepository">
    <resultMap id="customerMap" type="com.oyrf.frist.Customer">
        <result column="cname" property="name"></result>
        <collection property="goods" ofType="com.oyrf.frist.Goods">
            <result column="gname" property="name"></result>
        </collection>
    </resultMap>
    <select id="findByName" parameterType="java.lang.String" resultMap="customerMap">
        SELECT c.name AS cname,g.name AS gname FROM customer c,goods g,customer_goods cg WHERE c.name=#{name} AND c.id=cg.cid AND g.id=cg.gid
    </select>

</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OYBox

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值