SoFunction
Updated on 2025-03-03

How to query the return object with List attribute

Mybatis-plus query returns the object with List<String> attribute

Business logicWhen querying user data, carry the name of the user managed community List<String>

Users and managed communities are one-to-many

Query returns entity class

@Data
public class UserVo{

    private Long userId;

    private String loginName;

    private String userName;

    private String password;

    private Integer userTypeEnum;

    private String email;

    private String phone;

    private Integer sexEnum;

    private String avatar;

    private Integer statusEnum;

    private Integer deleteFlag;

    private String loginIp;

    private LocalDateTime loginDate;

    private Long companyId;

    private String companyName;

    private String remark;

    /**
      * Query according to the cell id
      */
    private Long estateId;

    private List&lt;String&gt; estateNameList;

<resultMap  type="">
        <collection property="estateNameList" ofType="" javaType="" select="getEstateName" column="user_id">
            <result column="estateNameList"/>
        </collection>
    </resultMap>


    <select  resultMap="BaseResultMap">
        SELECT
            distinct(t1.user_id) userId,
            t1.*,
            t2.company_name
        FROM
	        sys_user t1
	    LEFT JOIN charge_company t2 ON t1.company_id = t2.company_id
	    left join charge_user_estate t3 on t1.user_id=t3.user_id
	    </select>

    <select  parameterType="" resultType="" >
        select t4.house_estate_name estateNameList
        from charge_user_estate t3
	    inner join charge_base_estate t4 on t3.estate_id=t4.house_estate_id
	    where t3.user_id=#{user_id}
    </select>

public interface UserMapper extends BaseMapper<User> {

    List<UserVo> userVoList();

}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.