Mybatis-Plus 如何实现一对多关系 举例 用户与角色
Mybatis-Plus 一对多Mybatis-Plus
不写一句sql语句实现一对多
首先来看效果
Mysql数据库
用户表
角色表
用户与角色的中间表
中间表如下
将三张表通过Mybatis Plus 的代码生成器生成到目录下
Pojo
在User的Pojo 添加List
package com.zcx.pojo;
import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;
/**
* @author zhaochangxin
* @date 2022/3/2 14:34
*/
// 生成getAndSet方法
@Data
// 有参
@AllArgsConstructor
// 无参
@NoArgsConstructor
@TableName("platform_usertest")
public class User implements Serializable {
// 默认自增字段
@TableId(type = IdType.ASSIGN_ID)
private Long id;
// 用户名
private String username;
// 密码
private String password;
// 状态
private int status;
// 创建者
@TableField("created_by")
private BigInteger createdBy;
// 创建时间
@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")
// 字段添加填充内容
@TableField(value = "created_date", fill = FieldFill.INSERT)
private Date createdDate;
// 最后修改者
@TableField("last_modified_by")
private BigInteger lastModifiedBy;
// 最后修改时间
@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")
// 字段添加填充内容
@TableField(value = "last_modified", fill = FieldFill.INSERT_UPDATE)
private Date lastModified;
// 所有者
private String owner;
// 乐观锁
@Version
private Integer version;
// 逻辑删除
@TableLogic
private Integer deleted;
// 权限
@TableField(exist = false)
private List<Role> roles;
}
IuserService
package com.zcx.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zcx.pojo.User;
import java.util.List;
/**
* @author zhaochangxin
* @Title: IUserService
* @Package com.zcx.service
* @Description: IUserService
* @date 2022/3/30 17:21
*/
public interface IUserService extends IService<User> {
List<User> queryAllUser();
}
在ServiceImpl 实现该接口方法
package com.zcx.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcx.mapper.RoleMapper;
import com.zcx.mapper.UserMapper;
import com.zcx.mapper.UserRoleMapper;
import com.zcx.pojo.Role;
import com.zcx.pojo.User;
import com.zcx.pojo.UserRole;
import com.zcx.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* @author zhaochangxin
* @Title: UserServiceImpl
* @Package com.zcx.service.impl
* @Description: UserServiceImpl
* @date 2022/3/30 17:21
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Autowired
UserMapper userMapper;
@Autowired
UserRoleMapper userRoleMapper;
@Autowired
RoleMapper roleMapper;
public List<User> queryAllUser(){
// 首先查询所有用户
List<User> users = userMapper.selectList(null);
// 用于存储role_Id的集合
List<Integer> role_idList = new ArrayList<>();
for (User user : users) {
// 循环开始时清空集合
role_idList.clear();
// 通过中间表用户user_id和中间表的user_id相等 查询出该用户 有的role_id
QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
wrapper.eq("user_id",user.getId());
List<UserRole> userRoles = userRoleMapper.selectList(wrapper);
// 将查出来的role_id 放入一个List里 好利于BatchIds
for (UserRole userRole : userRoles) {
role_idList.add(userRole.getRoleId());
}
// 查询该用户所有的Role_Id 有的角色
List<Role> roles = roleMapper.selectBatchIds(role_idList);
user.setRoles(roles);
}
return users;
}
}
}
有问题或更好的办法请留言告知,谢谢您的观看。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现