最近在开发一个权限管理系统,用户是两张表组合的,分别是基础属性表和扩展属性表
基础属性表 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"