1.mybatisplus依赖必须是3.5版本以上,这里使用3.5.3.1版本
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.3.1version>
dependency>
2,导入mybatis-plus-join相关依赖
<dependency>
<groupId>com.github.yulichanggroupId>
<artifactId>mybatis-plus-join-boot-starterartifactId>
<version>1.4.3.2version>
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;