mybatis将返回的多条数据映射为一条数据的多个字段

最近在开发一个权限管理系统,用户是两张表组合的,分别是基础属性表和扩展属性表
基础属性表 pluto_user

id user_name remarks
1 曹操 热爱加班
2 曹丕 热爱吃饭
3 蔡徐坤 喜欢唱、跳、rap、篮球
4 郭嘉 喜欢唱、跳、rap、篮球
5 荀彧 办事效率高
6 程昱 喜欢写代码
7 夏侯渊 精通java

扩展属性表 pluto_user_cust_attr

attribute_name attribute_value user_id
phone 17605018833 1
married true 1
birthday 1995年12月12日 1
phone 17605018833 2
married true 2
birthday 1995年12月12日 2
post 运维 1
post 开发 2
post 测试 3
married false 3
phone 17605018833 3
post 运维 4
married true 4
phone 13523456789 4

一个扩展属性就对应一行,最后要把这两张表映射成一个对象传给前端
但是查出来结果不对,比如我的查询sql是

select p1.*, p2.attribute_name, p2.attribute_value
from  pluto_user p1 left join  pluto_user_cust_attr p2 on p1.id = p2.user_id where p1.id = 1;

结果

id user_name remarks attribute_name attribute_value
1 曹操 热爱加班 1 phone
1 曹操 热爱加班 1 married
1 曹操 热爱加班 1 birthday
1 曹操 热爱加班 1 post

返回的是多个对象,要映射为一个对象,传给前端:

{
    "id": 1,
    "userName": "曹操",
    "remarks": "热爱加班",
    "customAttributes": {
      "birthday": "1995年12月12日",
      "post": "运维",
      "phone": "17605018833",
      "married": "true"
    }

这里就要用mybatie来转换了

<mapper namespace="com.swcote.pluto.mappers.PlutoUserMapper">
    <resultMap id="plutoUserDto" type="com.swcote.pluto.entity.PlutoUserDto">
        <id property="id" column="id"/>
        <result property="userName" column="user_name"/>
        <result property="remarks" column="remarks"/>
        <collection property="customAttributes" ofType="HashMap">
            <result property="key" column="attribute_name" />
            <result property="value" column="attribute_value"/>
        </collection>
    </resultMap>
    <select id="getUserAll" resultMap="plutoUserDto">
        SELECT u.id, u.user_name, u.remarks, ca.attribute_name, ca.attribute_value
        FROM pluto_user u left join pluto_user_cust_attr ca on u.id = ca.user_id
    </select>
</mapper>    

这样映射还是差一点,返回的类型是list<Map<String,String>>,我们需要的是Map<String, String>,把UserDto转换为User
UserDto

public class PlutoUserDto {
    /**
     * 人员ID
     */
    private Integer id;
    /**
     * 人员名
     */
    private String userName;
    /**
     * 说明
     */
    private String remarks;
    /**
     * 扩展属性, PlutoUser就这里不一样,是Map<String, String>类型
     */
    private List<Map<String, String>> customAttributes;

    //转换为PlutoUser
    public PlutoUser toPlutoUser(){
        HashMap<String, String> map = new HashMap<>();
        getCustomAttributes().stream().forEach(p -> {
            String key = p.get("key");
            String value = p.get("value");
            map.put(key, value);
        });
        return new PlutoUser(getId(), getUserName(), getRemarks(), map);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public List<Map<String, String>> getCustomAttributes() {
        return customAttributes;
    }

    public void setCustomAttributes(List<Map<String, String>> customAttributes) {
        this.customAttributes = customAttributes;
    }
}

最后在service层调用

    @Override
    public List<PlutoUser> getPlutoUserAll(){
        List<PlutoUserDto> listDto = plutoUserMapper.getUserAll();
        List<PlutoUser> list = new ArrayList<>();
        listDto.stream().forEach(dto -> {
            list.add(dto.toPlutoUser());
        });
        return list;
    }

猜你喜欢

转载自blog.csdn.net/qq_36939013/article/details/91347351