Java开发笔记9(一、从另一个表导入另一个表获取区域树数据;二、根据stationId生成二级树)
一、从另一个表导入另一个表获取区域树数据:
1.Controller:
/**
* 将 b_stationinfrelatree 表中的前两级数据导入到 b_stationregiontree 表中接口。
*/
@PostMapping("/toStationInfRelaTree")
public Result InfToRegionTree() {
stationRegionTreeService.setInfToRegion();
return Result.ok();
}
2.Service:
Result setInfToRegion();
3.Impl:
@Override
public Result setInfToRegion() {
//查询所有数据
List<StationInfRelaTree> allDate = stationInfRelaTreeDao.getAllDate();
//创建一个List用来存放所有parentId为'1'的根节点。
List<StationInfRelaTree> allRoot = new ArrayList<>();
for(StationInfRelaTree date : allDate) {
//如果parentId为1,则代表是根节点,存入到allRoot集合中。
if(date.getParent().getId().equals("1")) {
allRoot.add(date);
}
}
for(StationInfRelaTree allroot : allRoot) {
StationRegionTree stationRegionTree = new StationRegionTree();
stationRegionTree.setId(Long.parseLong(allroot.getId()));
stationRegionTree.setRelaTreeId(allroot.getRelaTreeId());
stationRegionTree.setParent(allroot.getParent().getId());
stationRegionTree.setInfValue(allroot.getInfValue());
stationRegionTree.setInfCode(allroot.getInfCode());
stationRegionTree.setInfType(allroot.getInfType());
stationRegionTree.setInfName(allroot.getInfName());
stationRegionTree.setStationName(allroot.getStationName());
stationRegionTree.setInfMark(allroot.getInfMark());
stationRegionTree.setRelaTreeState(allroot.getRelaTreeState());
stationRegionTree.setStationCode(allroot.getInfValue());
// if (allroot.getRelaTreeId() == null || allroot.getRelaTreeId().isEmpty()) {
// continue;
// }
StationRegionTree RegionTrees = stationRegionTreeDao.selectRegion(allroot.getRelaTreeId());
if(RegionTrees != null) {
// stationRegionTreeMapper.updateRegin(stationRegionTree);
stationRegionTreeMapper.updateTreeById(stationRegionTree);
}else {
stationRegionTreeMapper.insertTree(stationRegionTree);
// stationRegionTreeDao.save(stationRegionTree);
}
}
//查找根节点下的子节点,子节点的parent_id就是根节点的rela_tree_id
for(StationInfRelaTree relaTree : allRoot) {
List<StationInfRelaTree> children = getChild(relaTree.getRelaTreeId(), allDate);
for(StationInfRelaTree childs : children) {
StationRegionTree RegionTree = new StationRegionTree();
RegionTree.setId(Long.parseLong(childs.getId()));
RegionTree.setRelaTreeId(childs.getRelaTreeId());
RegionTree.setParent(relaTree.getId());
RegionTree.setInfValue(childs.getInfValue());
RegionTree.setInfCode(childs.getInfCode());
RegionTree.setInfType(childs.getInfType());
RegionTree.setInfName(childs.getInfName());
RegionTree.setStationName(childs.getStationName());
RegionTree.setInfMark(childs.getInfMark());
RegionTree.setRelaTreeState(childs.getRelaTreeState());
RegionTree.setStationCode(relaTree.getInfValue());
StationRegionTree RegionTrees = stationRegionTreeDao.selectRegion(RegionTree.getRelaTreeId());
if(RegionTrees != null) {
// stationRegionTreeMapper.updateRegin(RegionTree);
stationRegionTreeMapper.updateTreeById(RegionTree);
}else {
stationRegionTreeMapper.insertTree(RegionTree);
// stationRegionTreeDao.save(RegionTree);
}
}
}
return Result.ok();
}
4.dao:
@Query(value = "select * from b_stationinfrelatree s", nativeQuery = true)
List<StationInfRelaTree> getAllDate();
@Query(value = "select s from StationRegionTree s where s.relaTreeId = :relaTreeId")
StationRegionTree selectRegion(@Param("relaTreeId") String relaTreeId);
void updateTreeById(StationRegionTree tree);
<update id="updateTreeById">
update b_stationregiontree set rela_tree_id =#{relaTreeId}, parent_id=#{parent}, inf_value=#{infValue}, inf_code=#{infCode}, inf_type=#{infType},
inf_name=#{infName}, station_name=#{stationName}, inf_mark=#{infMark}, rela_tree_state=#{relaTreeState}, station_code=#{stationCode}
where id=#{id}
</update>
void insertTree(StationRegionTree regionTree);
<insert id="insertTree">
insert into b_stationregiontree (id, rela_tree_id, parent_id, inf_value, inf_code, inf_type, inf_name, station_name, inf_mark, rela_tree_state, station_code)
values (#{id}, #{relaTreeId}, #{parent}, #{infValue}, #{infCode}, #{infType}, #{infName}, #{stationName}, #{infMark}, #{relaTreeState}, #{stationCode})
</insert>
@Query(value = "select s from StationRegionTree s where s.relaTreeId = :relaTreeId")
StationRegionTree selectRegion(@Param("relaTreeId") String relaTreeId);
4.entity:
package com.cars.ict.rbpsems.entity.controlpisled;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
import java.util.Set;
/**
* Created by
*/
@Entity
@Table(name = "b_stationinfrelatree")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
public class StationInfRelaTree {
@Id
@Column(length = 20, name = "id")
private String id;
@Column(length = 20, name = "rela_tree_id")
private String relaTreeId;
/**
* 上級
*/
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private StationInfRelaTree parent;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "parent")
//@OrderBy(value = "code asc")
private Set<StationInfRelaTree> children;
/**
* 车站基础信息值
*/
@Column(length = 20, name = "inf_value")
private String infValue;
/**
* 车站基础信息编码
*/
@Column(length = 500, name = "inf_code")
private String infCode;
/**
* 车站基础信息类型
*/
@Column(length = 2, name = "inf_type")
private String infType;
/**
* 车站基础信息名称
*/
@Column(length = 20, name = "inf_name")
private String infName;
/**
* 车站名称
*/
@Column(length = 20, name = "station_name")
private String stationName;
/**
* 车站基础信息标示
*/
@Column(length = 100, name = "inf_mark")
private String infMark;
/**
* 信息状态
*/
@Column(length = 1, name = "rela_tree_state")
private String relaTreeState;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRelaTreeId() {
return relaTreeId;
}
public void setRelaTreeId(String relaTreeId) {
this.relaTreeId = relaTreeId;
}
public StationInfRelaTree getParent() {
return parent;
}
public void setParent(StationInfRelaTree parent) {
this.parent = parent;
}
public Set<StationInfRelaTree> getChildren() {
return children;
}
public void setChildren(Set<StationInfRelaTree> children) {
this.children = children;
}
public String