mybatis 注解 一对多 返回参数问题

本文介绍了如何修复用户查询时返回参数缺失的问题,通过将HashMap类型从<String, String>改为<String, Object>,解决了角色列表和仓库列表无法正确映射的问题。详细展示了User实体类和Mapper接口的修改,以及查询语句的调整。
摘要由CSDN通过智能技术生成

1   实体类(实体类有删除部分代码)

package com.longtu.admin.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.*;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 *
 * 
 * 用户实体类
 *
 * @author lyx
 * @since 2020-12-18
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 用户主键
     */
    @TableId(value = "user_id", type = IdType.AUTO)
    private Integer userId;

    /**
     * 用户名
     */
    private String username;


    /**
     * 角色列表
     */
    @TableField(exist = false)
    private List<HashMap<String,String>> roleList;

    /**
     * 仓库列表
     */
    @TableField(exist = false)
    private List<HashMap<String,String>> warehouseList;

}

 

2 mapper  配置

package com.longtu.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longtu.admin.pojo.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author lyx
 * @since 2020-12-18
 */

public interface UserMapper extends BaseMapper<User> {


    /**
     * 用户管理界面面查询用户列表
     * @param page
     * @param companyId
     * @param departmentId
     * @param roleId
     * @param userName
     * @param employeeName
     * @param employeePhone
     * @param userStatus
     * @param warehouseId
     * @return
     */
    @Select("<script> " +
            "SELECT  " +
            "  u.user_id, " +
            "  u.employee_id,  " +
            "  emp.employee_name,  " +
            "  emp.employee_phone,  " +
            "  emp.employee_email,  " +
            "  u.company_id,  " +
            "  u.expiration ," +
            "  des1.describe_info company_name,  " +
            "  u.department_id,  " +
            "  des2.describe_info department_name,  " +
            "  u.user_status   " +
            "FROM  " +
            "  `user` u  " +
            "  LEFT JOIN employee emp ON u.employee_id = emp.employee_id  " +
            "  LEFT JOIN `describe` des1 ON des1.auote_id = u.company_id   " +
            "  AND des1.describe_type = 'COMPANY_NAME'   " +
            "  AND des1.`language` = u.`language`  " +
            "  LEFT JOIN department dep ON u.department_id = dep.department_id  " +
            "  LEFT JOIN `describe` des2 ON des2.auote_id = u.department_id   " +
            "  AND des2.describe_type = 'DEPARTMENT_NAME'   " +
            "  AND des2.`language` = u.`language`  " +
            "  LEFT JOIN user_warehouse uw ON uw.user_id = u.user_id  " +
            "  LEFT JOIN user_role ur ON u.user_id = ur.user_id   " +
            "<where> " +
            "<if test= 'companyId != null and companyId != \"\"' > " +
            "and  u.company_id = #{companyId}  " +
            "</if> " +
            "<if test= 'departmentId != null and departmentId != \"\"' > " +
            "  AND u.department_id = #{departmentId}  " +
            "</if> " +
            "<if test= 'roleId != null and roleId != \"\"' > " +
            "  AND ur.role_id = #{roleId}   " +
            "</if> " +
            "<if test= 'userName != null and userName != \"\"' >  " +
            "  AND u.username LIKE '${userName}%'   " +
            "</if> " +
            "<if test= 'employeeName != null and employeeName != \"\"' >  " +
            "  AND emp.employee_name LIKE '${employeeName}%'   " +
            "</if> " +
            "<if test= 'employeePhone != null and employeePhone != \"\"' >  " +
            "  AND emp.employee_phone LIKE '${employeePhone}%'  " +
            "</if> " +
            "<if test= 'userStatus != null and userStatus != \"\"' >  " +
            "  AND u.user_status = #{userStatus}   " +
            "</if> " +
            "<if test= 'warehouseId != null and warehouseId != \"\"' >  " +
            "  AND uw.warehouse_id =#{warehouseId}   " +
            "</if> " +
            "</where> " +
            "GROUP BY  " +
            "  u.user_id,  " +
            "  emp.employee_name,  " +
            "  emp.employee_phone,  " +
            "  emp.employee_email,  " +
            "  u.company_id,  " +
            "  des1.describe_info,  " +
            "  u.department_id,  " +
            "  des2.describe_info,  " +
            "  u.user_status " +

            "</script> ")
    @Results({
            @Result(id = true,column = "user_id",property = "userId"),
            @Result(column = "user_id",property = "roleList",many = @Many(select = "com.longtu.admin.mapper.UserRoleMapper.selectRoleNameListByUserId")),
            @Result(column = "user_id",property = "warehouseList",many = @Many(select = "com.longtu.admin.mapper.UserWarehouseMapper.selectWarehouseNameListByUserId"))

    } )
    List<User> selectAllUserList(Page<User> page,String companyId,String departmentId,String roleId,String userName,String employeeName,String employeePhone,String userStatus,String warehouseId);



}

3  请求出现返回参数 缺失

4解决方法  将HashMap<String,String>    改为HashMap<String,Object>可解决此问题,如下图:

这里可以正常显示了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值