如何使用mybatisplus实现多表查询

如何使用mybatisplus实现多表查询

1.mybatisplus依赖必须是3.5版本以上,这里使用3.5.3.1版本

           <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.5.3.1</version>
            </dependency>

2,导入mybatis-plus-join相关依赖

            <dependency>
                <groupId>com.github.yulichang</groupId>
                <artifactId>mybatis-plus-join-boot-starter</artifactId>
                <version>1.4.3.2</version>
            </dependency>

3.需要是springBoot项目

4.自行导入mysql相关依赖

新建表

创建学生表

drop table if exists edu_student;
create table edu_student(
                            id bigint not null AUTO_INCREMENT comment '编号'
                                primary key,
                            student varchar (50) not null comment '学生姓名',
                            sex varchar (1) not null comment '性别',
                            parent_id bigint not null comment '家长编号',
                            age int not null comment '年龄',
                            status varchar (50) not null comment '状态'
)engine =innodb default  charset=utf8mb4 comment='学生表';

创建家长表

drop table if exists edu_parent;
create table edu_parent(
    id bigint not null default 0 comment '编号'
                       primary key ,
    telephone varchar(11) not null comment '手机号',
    name varchar(50) not null  comment '家长姓名',
    wechat varchar(50) not null comment '关联微信',
    status varchar(50) not null  comment '状态'
)engine =innodb default  charset=utf8mb4 comment='家长表';

实体类

1.eduStudent类


import java.io.Serializable;
import lombok.Data;

/**
 * edu_student
 * @author 
 */
@Data
public class EduStudent implements Serializable {
    /**
     * 编号
     */
    private Long id;

    /**
     * 学生姓名
     */
    private String student;

    /**
     * 性别
     */
    private String sex;

    /**
     * 家长编号
     */
    private Long parentId;

    /**
     * 年龄
     */
    private Integer age;

    /**
     * 状态
     */
    private String status;

    private static final long serialVersionUID = 1L;
}

2.eduParent类


import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * edu_parent
 * @author 
 */
@Data
public class EduParent implements Serializable {
    /**
     * 编号
     */
    private Long id;

    /**
     * 手机号
     */
    private String telephone;

    /**
     * 家长姓名
     */
    private String name;

    /**
     * 关联微信
     */
    private String wechat;


    private static final long serialVersionUID = 1L;
}

3.EduStudentDTO类

@Data
public class EduStudentDTO{
    /**
     * 编号
     */
    private Long id;

    /**
     * 学生姓名
     */
    private String student;

    /**
     * 性别
     */
    private String sex;

    /**
     * 家长编号
     */
    private String parent;

    /**
     * 年龄
     */
    private Integer age;

    /**
     * 状态
     */
    private String status;

    /**
     * 手机号
     */
    private String parentTele;

    /**
     * 家长姓名
     */
    private String parentName;

    /**
     * 关联微信
     */
    private String parentWechat;

   
}

实现

public class Test{
public static void main(String [] args){
    private EduStudentMapper eduStudentMapper;
    MPJLambdaWrapper<EduStudent> wrapper = new MPJLambdaWrapper<>();
    //选择eduStudent里面所有字段
    warapper.selectALL(EduStudent.class)
             重命名 as
            .selectAs(EduParent::getTelephone,EduStudentDTO::getParentTele)
            .selectAs(EduParent::getName,EduStudentDTO::getParentName)
            .selectAs(EduParent::getWeChat,EduStudentDTO::getParentWeChat)
            .leftjoin(EduParenT.class,EduParent::getId,EduStudent::getParentId)
            .eq(EduStudent::getParentId,EduParent::getId);
   List<EduStudentDTO> list=eduStudentMapper.selectList(EduStudentDTO.class,wrapper);
 
}}

类似mysql中

select s.*,
       p.telephone as parent_tele,
       p.name as parent_name,
       p.wechat as parent_we_chat
       from eduStudent s, eduParent p
       where s.parent_id=p.id;
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值