The java data dictionary (parent-child structure) bind to List collection, in order to achieve database synchronization
recent company to do a project, which is a function of the local database synchronization to a remote server, the synchronization process is not the focus of this article, focus is on the import process, involving a type of data dictionary table, which is the parent table with ID, that (his son table), which the proposed new import requirements: that is, when the new import table You must import the parent table, and then import the child table, if the multi-level, then sequentially introduced from the top-down start, leading a meal and I said, what ah while loop, recursive ah, to be honest, I know he wants to express meaning, but I could not write, can not look for the Internet, and finally I saw the one-sided article:
Kaer_GG article, tree-list (menu tree) recursive traversal list helped me a lot, well, does not cost, then I look at the database structure:
, well, then the corresponding entities for everyone to see
package com.ringchi.entity.department;
import com.ringchi.entity.NamedObject;
import com.ringchi.entity.NamedTreeObject;
import com.ringchi.entity.dictionary.Dictionary;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
@Entity
@Table(name="basic_department")
public class Department extends NamedObject
implements NamedTreeObject
{
private static final long serialVersionUID = 3256440322035822899L;
private String assignedCode;
private String abbreviation;
private Dictionary type;
private Dictionary kind;
private Date createDate;
private Date dropDate;
private int sequence;
private String location;
private Department parent;
private Dictionary level;
private String levelCode;
public Department()
{
}
public Department(String id)
{
super(id);
}
public Department(String id, String name) {
super(id, name);
}
@Column(length=20)
public String getAbbreviation()
{
return this.abbreviation;
}
public void setAbbreviation(String abbreviation)
{
this.abbreviation = abbreviation;
}
@Column(length=20)
public String getAssignedCode() {
return this.assignedCode;
}
public void setAssignedCode(String assignedCode) {
this.assignedCode = assignedCode;
}
@Temporal(TemporalType.DATE)
public Date getCreateDate()
{
return this.createDate;
}
public void setCreateDate(Date createDate)
{
this.createDate = createDate;
}
@ManyToOne
@JoinColumn(name="Type")
public Dictionary getType()
{
return this.type;
}
public void setType(Dictionary type)
{
this.type = type;
}
@Temporal(TemporalType.DATE)
public Date getDropDate()
{
return this.dropDate;
}
public void setDropDate(Date dropDate)
{
this.dropDate = dropDate;
}
public int getSequence()
{
return this.sequence;
}
public void setSequence(int sequence)
{
this.sequence = sequence;
}
@ManyToOne
@JoinColumn(name="Parent")
public Department getParent()
{
return this.parent;
}
public void setParent(Department parent)
{
this.parent = parent;
}
public String toString()
{
String result = getId() + ":" + getName() + "[";
if (getParent() != null) {
result = result + "{" + getParent().getId() + "} ";
}
result = result + "]";
return result;
}
@Transient
public NamedTreeObject getTreeParent() {
return getParent();
}
public void setTreeParent(NamedTreeObject parent) {
setParent((Department)parent);
}
@ManyToOne
@JoinColumn(name="Level")
public Dictionary getLevel() { return this.level; }
public void setLevel(Dictionary level)
{
this.level = level;
}
@ManyToOne
@JoinColumn(name="Kind")
public Dictionary getKind() { return this.kind; }
public void setKind(Dictionary kind)
{
this.kind = kind;
}
@Column(length=100)
public String getLocation() {
return this.location;
}
public void setLocation(String location) {
this.location = location;
}
@Column(length=50)
public String getLevelCode()
{
return this.levelCode;
}
public void setLevelCode(String levelCode) {
this.levelCode = levelCode;
}
}
以上是实体部分,重点来了,如何将这些数据绑定成List集合呢?看代码
//先查找原数据库
List<Department> equips = theDMO.getObjects("FROM Department");
System.out.println("原数据库");
for (Department department : equips) {
System.out.println(department);
}
//我们来看看打印的结果:
![这里写图片描述](http://img.blog.csdn.net/20170822095744282?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDI5ODQ0NA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
可以看到原来的数据库是无序的,此时要是导入的话,肯定出错,下面看具体的转换过程
List<Department> resultList=new ArrayList<Department>();
for (Department department : equips) {
if (department.getParent()==null) {//父级菜单开始添加
resultList.add(department);
if (ifChilds(equips, department.getId())) {//存在子集
List<Department> childs = new ArrayList<>();
childs = getChildList(equips,department.getId(),childs);
resultList.addAll(childs);
}
}
}
//判断是否存在子集
private static boolean ifChilds(List<Department> list,String pId) {
boolean flag = false;
for (Department department : list) {
if (department.getParent()!=null) {
if (department.getParent().getId().equals(pId)) {
flag=true;
break;
}
}
}
return flag;
}
//获取父id下的子集合
private static List<Department> getChildList(List<Department> list,String pId,List<Department> reList) {
for (Department department : list) {
if (department.getParent()!=null) {
if (department.getParent().getId().equals(pId)) {//查询下级菜单
reList.add(department);
if (ifChilds(list, department.getId())) {
getChildList(list, department.getId(), reList);
}
}
}
}
return reList;
}
现在看看转换后的情况吧:
System.out.println(“新数据库”);
for (Department department : resultList) {
if (department.getParent()!=null) {
System.out.println(department.getId()+”\t”+department.getName()+”\t”+department.getParent().getName());
}else {
System.out.println(department.getId()+"\t"+department.getName());
System.out.println("########");
}
}
"`
Well, we want to help, I was a help to me!