需求:
小学组织结构--> 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();
}
}
获取数据:
至此结!