Spring Boot 创建一个对象,从同一对象的表中插入他的parentID

b4wnujal  于 2023-08-04  发布在  Spring
关注(0)|答案(2)|浏览(124)

我是Java环境(Sping Boot )的新手,并尝试操作注入。我有一个表Section,一个Section可以有一个 parent,但如果一个Section Object是 parent,我们必须能够列出它的childrenSection。
这是我的服务

@Override
    public SectionDTO addSectionByParent(SectionDTO sectionDTO, Long parentId) {
        log.warn(" Checking the theParentSectionId:={}",parentId);
        Section parent = sectionRepository.findById(parentId).orElseThrow(() -> new RuntimeException("SectionParent not found exception"));
        Section section = sectionMapper.fromSectionDTO(sectionDTO);
        section.setParentSection(parent);
        Section addSection = sectionRepository.save(section);
        return sectionMapper.fromSection(addSection);
    }

字符串
这是我的控制器

@GetMapping("/newa/{varId}")
    public ModelAndView newSectionByParentId(@PathVariable Long varId){
        ModelAndView mav = new ModelAndView("detail/forma");
        SectionDTO sectionDTO = new SectionDTO();
        SectionDTO sectionParent = sectionService.showSection(varId);

        mav.addObject("detail",sectionDTO);
        mav.addObject("pro",sectionParent);
        return mav;
    }
@PostMapping("/savingx")
    public String savingx(@ModelAttribute("section") @Valid SectionDTO sectionDTO, BindingResult bindingResult, @RequestParam(value = "parentId", required = false) Long parentId){
        SectionDTO sectionParent = sectionService.showSection(parentId);
      
        String returnValue;
        if (bindingResult.hasErrors()) {
            return "project/forma";
        }
        System.out.println("******************* parentId : "+ parentId);
        if (sectionParent != null) {
            sectionDTO.setParentId(sectionParent.getParentId());
            sectionService.addSectionByParent(sectionDTO, sectionParent.getProductId());
        } 

        returnValue = "redirect:/product/detail/by/parent/{sectionId}";

        return returnValue;
    }

的数据
当运行此控制器时,这是我遇到的错误
第一个月
没有明确的
请你解释一下出了什么问题??

xcitsw88

xcitsw881#

确保你的主类在一个根包中,在其他类之上。
当您运行Sping Boot 应用程序时,(即a class annotated with @SpringBootApplication),Spring只会扫描main class包下面的类。

com
   +- APP
         +- Application.java  <--- your main class should be here, above your controller classes
         |
         +- model
         |   +- user.java
         +- controller
             +- UserController.java

字符串

vuktfyat

vuktfyat2#

对不起,如果我花了很多时间来回答,对于这段代码,我正在做一些事情,告诉我,我不理解注入的概念。
最后,这是我应该放在我的控制器中的代码,非常简单:

@PostMapping("/savingx/{parentId}")
public String savingxx(@ModelAttribute("section") SectionDTO sectionDTO, @PathVariable Long parentId, BindingResult bindingResult, Model model){
    sectionService.addSectionByParent(sectionDTO, parentId);
        returnValue= "redirect:/product/detail/by/parent/{parentId}";
}

字符串
非常感谢你的贡献

相关问题