Mybatis-Plus 如何實現一對多關系 舉例 用戶與角色


Mybatis-Plus 一對多Mybatis-Plus

不寫一句sql語句實現一對多

首先來看效果
image

Mysql數據庫

用戶表
角色表
用戶與角色的中間表
中間表如下
image
將三張表通過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;
    }
}

}

有問題或更好的辦法請留言告知,謝謝您的觀看。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM