级别类目数据的嵌套封装返回

需求:

    小学组织结构--> xx小学--xx年级--xx班级--xx小组--xx小队(五个级别),查所有数据进行封装后返回前端,前端用Tree控件展示树形结构.

建表:

CREATE TABLE IF NOT EXISTS organization (
	or_id VARCHAR(50),
	or_name VARCHAR(50),
	or_upId VARCHAR(50),
	PRIMARY KEY(or_id)
)

插数据:

INSERT INTO organization VALUES('1001', '曙光小学', 'x');
INSERT INTO organization VALUES('2001', '一年级', '1001');
INSERT INTO organization VALUES('3001', '一班', '2001');
INSERT INTO organization VALUES('4001', '一组', '3001');
INSERT INTO organization VALUES('4002', '二组', '3001');
INSERT INTO organization VALUES('5001', '一队', '4001');
INSERT INTO organization VALUES('5002', '二队', '4001');
INSERT INTO organization VALUES('5003', '一队', '4002');
INSERT INTO organization VALUES('5004', '二队', '4002');
INSERT INTO organization VALUES('3002', '二班', '2001');
INSERT INTO organization VALUES('4003', '一组', '3002');
INSERT INTO organization VALUES('4004', '二组', '3002');
INSERT INTO organization VALUES('5005', '一队', '4003');
INSERT INTO organization VALUES('5006', '二队', '4003');
INSERT INTO organization VALUES('5007', '三队', '4003');
INSERT INTO organization VALUES('5008', '一队', '4004');
INSERT INTO organization VALUES('5009', '二队', '4004');

查询参数封装类(get, set, 构造自行补充):

public class IdNameUpId {
    private String id;
    private String name;
    private String upId;
}
嵌套封装类(其他get, set, 构造自行补充):
public class OrganizationView {
    private String id;
    private String title;
    private List<OrganizationView> children;

    public OrganizationView(String id, String title) {
        this.id = id;
        this.title = title;
        this.children = new ArrayList<OrganizationView>();
    }
}

dao层(mybatis的mapper):

@Mapper
public interface OrganizationMapper {
    @Select("SELECT * FROM organization")
    @Results({
                @Result(property = "id",  column = "or_id"),
                @Result(property = "name",  column = "or_name"),
                @Result(property = "upId",  column = "or_upId")
    })
    List<IdNameUpId> queryAll();
}

service层:

 public interface OrganizationService {
 List<OrganizationView> queryOrganization();
 }
@Service
@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor=Exception.class)
public class PatrolServiceImpl implements OrganizationService{
    @Autowired
    private OrganizationMapper organizationMapper;
    @Override
    public List<OrganizationView> queryOrganization (){
        List<IdNameUpId> listAll = this.organizationMapper.queryAll();
        List<OrganizationView> list = new ArrayList<OrganizationView>();//存返回值用
        HashMap<String,Integer> index = new HashMap<String,Integer>();//对索引进行记录
        HashMap<String,String> idUpid = new HashMap<String,String>();//记录上级id用
        int indexA = 0;//最内层索引
        int indexB = 0;//次内层索引
        int indexC = 0;//内到外第三层索引
        int indexD = 0;//内到外第四层索引
        String s = "";
        for(IdNameUpId o : listAll){
            s = o.getUpId();
            OrganizationView ov = new  OrganizationView(o.getId(), o.getName());
            if("x".equals(s)){
                list.add(ov);
                index.put(o.getId(), list.size()-1);
                continue;
            }   
            indexA = index.get(s);
            s = idUpid.get(s);
            if(s == null){
                list.get(indexA).getChildren().add(ov);
                index.put(o.getId(), list.get(indexA).getChildren().size()-1);
                idUpid.put(o.getId(), o.getUpId());
                continue;
            }
            indexB = index.get(s);
            s = idUpid.get(s);
            if(s == null){
                 Object ob = list.get(indexB).getChildren().get(indexA);
                 if(ob instanceof OrganizationView){
                     ((OrganizationView) ob).getChildren().add(ov);
                      index.put(o.getId(), ((OrganizationView) ob).getChildren().size()-1);
                      idUpid.put(o.getId(), o.getUpId());
                      continue;
                 }
            }
            indexC = index.get(s);
            s = idUpid.get(s);
             if(s == null){
                 Object ob = list.get(indexC).getChildren().get(indexB);
                 if(ob instanceof OrganizationView){
                     Object ob1 = ((OrganizationView) ob).getChildren().get(indexA);
                     if(ob1 instanceof OrganizationView){
                         ((OrganizationView) ob1).getChildren().add(ov);
                         index.put(o.getId(), ((OrganizationView) ob1).getChildren().size()-1);
                         idUpid.put(o.getId(), o.getUpId());
                         continue;
                     }
                 }
             }
            indexD = index.get(s);
            s = idUpid.get(s);
            if(s == null){
               Object ob = list.get(indexD).getChildren().get(indexC);
                 if(ob instanceof OrganizationView){
                     Object ob1 = ((OrganizationView) ob).getChildren().get(indexB);
                     if(ob1 instanceof OrganizationView){
                        Object ob2 = ((OrganizationView) ob1).getChildren().get(indexA);
                        if(ob2 instanceof OrganizationView){
                           ((OrganizationView) ob2).getChildren().add(ov);
                            index.put(o.getId(), ((OrganizationView) ob2).getChildren().size()-1);
                            idUpid.put(o.getId(), o.getUpId());
                            continue; 
                        }
                     }
                 } 
            }    
        }
        return list;
    }
}

controller层(Spring MVC):

@RestController
public class OrganizationController {
    @Autowired
    private OrganizationService organizationService;
    @RequestMapping(value = "/queryOrganization",method = RequestMethod.GET)
    public List<OrganizationView> queryOrganization (){
        return this.organizationService.queryOrganization();
    }
}

获取数据:


至此结!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值