Java从表中获取分层数据并生成树

时间:2019-04-29 08:31:26

标签: java microservices

我正在为数据库表中的角树视图设置分层结构。我有类别表。 Category Table

我想要以下格式的输出

[{
    "category_id": 1,
    "category_name": "Action",
    "categoryParentId": 0,
    "categoryDto": [{
        "category_id": 4,
        "category_name": "Classical",
        "categoryParentId": 1,
        "categoryDto": [{
            "category_id": 3,
            "category_name": "Instrumental",
            "categoryParentId": 4,
        }]
    }]
}]

每个类别都有一个列category_parent_id,如果该列为父列,则该列可以为零,否则它将另存一个category_id

我有Java代码,我想在其中从JSON树状结构中获取此数据,然后Angular树视图将使用它来生成必要的UI组件。

类别存储库

    @Repository
    public interface CategoryRepository extends JpaRepository<Category, Integer> {

        List<Category> findByCategoryParentId(int categoryParentId);
    }

类别模型

@Entity
@Table(name = "content_category")
public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int category_id;

    private String category_name;

    @Column(name="category_parent_id")
    private int categoryParentId;

CategoryDto

public class CategoryDto {

    private int category_id;

    private String category_name;

    private int categoryParentId;

    private List<CategoryDto> categoryDto;

我尝试使用递归设置dto,但没有成功。我能够将数据深入到一个层次,但不能超出此层次。

我的方法实现。

List<CategoryDto> categoryDto=new ArrayList<CategoryDto>();
@Override
    public List<CategoryDto> getAllParentCategories(int category_parent_id) {
        List<CategoryDto> categoryDtoList = new ArrayList<CategoryDto>();
        List<Category> categoryList = categoryRepository.findByCategoryParentId(category_parent_id);
        for (int i = 0; i < categoryList.size(); i++) {
            CategoryDto dto = new CategoryDto();
            dto.setCategory_id(categoryList.get(i).getCategory_id());
            dto.setCategory_name(categoryList.get(i).getCategory_name());
            dto.setCategoryDto(printTree(categoryList.get(i).getCategory_id()));

            categoryDtoList.add(dto);

        }

        return categoryDtoList;
    }


    public List<CategoryDto> printTree(int id) {


        List<Category> categoryList = categoryRepository.findByCategoryParentId(id);
        if (categoryList.size() > 0) {

            for (int i = 0; i < categoryList.size(); i++) {

                CategoryDto dto = new CategoryDto();
                dto.setCategory_id(categoryList.get(i).getCategory_id());
                dto.setCategory_name(categoryList.get(i).getCategory_name());
                if (categoryRepository.findByCategoryParentId(categoryList.get(i).getCategory_id()).size() > 0) {
                    dto.setCategoryDto(printTree(categoryList.get(i).getCategory_id()));
                }
//              else {
//                  dto.setCategoryDto(null);
//                  categoryDto.add(dto);
//              }
            }
            return categoryDto;
        } else {
            return categoryDto;
        }

    }

getAllParentCategories()parent_is调用,并声明从db提取的每个parent_id都作为参数传递

我的递归方法未按预期工作。

输出并显示错误消息“ SyntaxError:JSON.parse:JSON数据的第1行第79905列的未终止字符串文字”

[{"category_id":1,"category_name":"Action","categoryParentId":0,"category_uniq_id":null,"category":null,"categoryDto":[{"category_id":4,"category_name":"Classical","categoryParentId":0,"category_uniq_id":null,"category":null,"categoryDto":[{"category_id":4,"category_name":"Classical","categoryParentId":0,"category_uniq_id":null,"category":null,"categoryDto":[{"category_id":4,"category_name":"Classical","categoryParentId":0,"category_uniq_id":null,"category":null,"categoryDto":[{"category_id":4,"category_name":"Classical","categoryParentId":0,"category_uniq_id":null,"category":null,"categoryDto":[{"category_id":4,"category_name":"Classical","categoryParentId":0,"category_uniq_id":null,"category":null,"categoryDto":[{"category_id":4,"category_name":"Classical","categoryParentId":0,"category_uniq_id":null,"category":null,"categoryDto":[{"category_id":4,"category_name":"Classical","categoryParentId":0,"category_uniq_id":null,"category":null,"categoryDto":[{"category_id":4,"category_name":"Classical","categoryParentId":0,"category_uniq_id":null,"category":null,"categoryDto":[{"category_id":4,"category_name":"Classical","categoryParentId":0,"category_uniq_id":null,"category":null,"categoryDto":[{"category_id":4,"category_name":"Classical","categoryParentId":0,"category_uniq_id":null,"category":null,"categoryDto":

0 个答案:

没有答案