自定义SQL(多表关联查询)
1.实体类
这里使用的是一个实体类还有一个VO类
UserMoney类
package com.mybatisplusstudty.entity.entity;
import lombok.Data;
import java.io.Serializable;
/**
* @Program: mybatisplus
* @Description
* @Author: 涛涛 * ^ *
* @Create: 2021-01-08 16:58
**/
@Data
public class UserMoney implements Serializable {
private Integer id;
private Double money;
private Integer userId;
private String lastName;
}
ResultVo 类
package com.mybatisplusstudty.entity.vo;
import lombok.Data;
/**
* @Program: mybatisplus
* @Description
* @Author: 涛涛 * ^ *
* @Create: 2021-01-08 17:02
**/
@Data
public class ResultVo {
private Integer id;
private Double money;
private Integer userId;
private String lastName;
}
2.数据库

3.mapper
package com.mybatisplusstudty.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mybatisplusstudty.entity.entity.User;
import com.mybatisplusstudty.entity.vo.ResultVo;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @Program: mybatisplus
* @Description
* @Author: 涛涛 * ^ *
* @Create: 2021-01-07 15:12
**/
public interface UserMapper extends BaseMapper<User> {
@Select("SELECT `user`.last_name,usermoney.* FROM `usermoney`,`user` WHERE `user`.id=usermoney.userId ")
List<ResultVo> rList();
}
4.测试
System.out.println(userMapper.rList());
5.结果

本文介绍了如何在MybatisPlus中通过自定义SQL实现多表关联查询,包括创建UserMoney和ResultVo实体类,数据库表的设计,mapper接口的编写,以及测试和获取结果的过程。
1772

被折叠的 条评论
为什么被折叠?



