简体   繁体   English

如何使用 spring 引导从数据库 MYSQL 数据库创建动态菜单

[英]How to create Dynamic Menu from Database MYSQL database with spring boot

I have been trying to load the menu from database using spring jpa and spring boot with Mysql database.我一直在尝试使用 spring jpa 和 spring 启动数据库从数据库加载菜单。

But I have to load only active menu from database I have tried with JPQL ie findByActiveTrue() menthod of JPA but getting all records.但是我必须从数据库中只加载活动菜单,我曾尝试使用 JPQL 即 JPA 的 findByActiveTrue() 方法,但获取所有记录。

@Entity
@Table(name = "menu_tbl")
//@Cacheable
//@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)

public class Menu implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "menu_description")
    private String menuName;

    @Column(name = "menu_link", nullable = false)
    private String link;

    @Column(name = "menu_css")
    private String css;

    @Column(name = "menu_ico")
    private String icon;
     
    @Column(name = "active")
    @
    private Boolean active;

    @JsonManagedReference
    @OneToMany(mappedBy = "parent")
    @OrderBy("id ASC")
    private Set<Menu> children;

    @ManyToOne
    @JsonBackReference
    private Menu parent;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getMenuName() {
        return menuName;
    }

    public void setMenuName(String menuName) {
        this.menuName = menuName;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getCss() {
        return css;
    }

    public void setCss(String css) {
        this.css = css;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Set<Menu> getChildren() {
        return children;
    }

    public void setChildren(Set<Menu> children) {
        this.children = children;
    }

    public Menu getParent() {
        return parent;
    }

    public void setParent(Menu parent) {
        this.parent = parent;
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

}

Repository存储库

public interface MenuRepository extends JpaRepository<Menu, Long> {
    
    
    List<Menu> findByActiveTrue();

}

And getting output并得到 output

[ { "id": 1, "menuName": "Customer", "link": "/customer", "css": null, "icon": null, "active": true, "children": [ { "id": 2, "menuName": "Add Customer", "link": "/addCustomer", "css": null, "icon": null, "active": false, "children": [] }, { "id": 3, "menuName": "View Customer", "link": "/viewCustomer", "css": null, "icon": null, "active": false, "children": [] } ] } ] [ {“id”:1,“menuName”:“客户”,“链接”:“/customer”,“css”:null,“图标”:null,“活动”:真,“儿童”:[{“ id”:2,“menuName”:“添加客户”,“link”:“/addCustomer”,“css”:null,“icon”:null,“active”:false,“children”:[] },{ “id”:3,“menuName”:“查看客户”,“link”:“/viewCustomer”,“css”:null,“icon”:null,“active”:false,“children”:[] } ] }]

Could Any one suggests?有人可以建议吗? Thanks in Advance提前致谢

enter image description here在此处输入图像描述

I would do like this:我会这样做:

@Entity
@Table(name = "menu_tbl")
public class Menu {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "menu_description")
    private String menuName;

    @Column(name = "menu_link", nullable = false)
    private String link;

    @Column(name = "menu_css")
    private String css;

    @Column(name = "menu_ico")
    private String icon;

    @Column(name = "active")
    private Boolean active;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "main_id",
            nullable = true)
    private Menu mainMenu;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "mainMenu", cascade = CascadeType.REMOVE)
    private Set<Menu> children = new HashSet<>();
}

And

List<Menu> findAllByMainMenuIdAndActiveIsTrue(Long mainMenuid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM