我无法通过CRUD操作将实体插入数据库

时间:2019-10-20 00:37:10

标签: mysql spring spring-boot jpa repository

我正在尝试在数据库中插入实体,但是.save(),编辑等……不起作用。它没有显示任何错误。另外,如果我用CommandLineRunner插入它,也可以,但是这种方式不行。我忘了什么吗?

@Entity
@Table(name = "categories")
public class Categories {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_category")
    private int id_category;
    @Column(name = "category_name")
    private String category_name;

    public Categories() {
    }
    ...
@Repository("categoriesRepository")
public interface CategoriesRepository extends JpaRepository<Categories, Integer> {

}
@Service
@Transactional
public class CategoriesService{

    public List<Categories> findAll() {
        return categoriesRepository.findAll();
    }

    public void saveCat(Categories categories) {
        this.categoriesRepository.save(categories);
    }
    ...
@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private CategoriesService categoriesService;

    @RequestMapping(value = "saveCategory", method = RequestMethod.POST)
    public String saveCategories(@ModelAttribute("categories") Categories theCategories) {

        categoriesService.saveCat(theCategories);
        return "redirect:/admin/allCategories";
    }

    @RequestMapping("/editCategories/{id_category}")
    public ModelAndView showEditCategoriesForm(@PathVariable(name = "id_category") int id_category) {

        ModelAndView mav = new ModelAndView("admin/admin_editCategories");
        Categories theCategories = categoriesService.getView(id_category);
        mav.addObject("categories", theCategories);
        return mav;
    }

    @RequestMapping("/deleteCategories/{id_category}")
    public String deleteCategories(@PathVariable(name = "id_category") int id_category) {

        categoriesService.delete(id_category);
        return "redirect:/admin/allCategories";
    }
<form action="#" th:action="@{/saveCategory}" th:object="${categories}"
                      method="post">
  <table border="1" cellpadding="10">
       <tr>
        <td>category</td>
        <td><input type="text" placeholder="enter category" th:field="*{category_name}"/></td>
       </tr>                 
       <tr>
       <td colspan="2"><button type="submit">SAVE</button> </td>
       </tr>
    </table>
</form>
  

Whitelabel错误页面此应用程序没有针对   /错误,因此您将其视为备用。

     

2019年10月20日星期日CEST 2019出现意外错误(type = Not   找到,状态为404)。没有可用消息

1 个答案:

答案 0 :(得分:0)

更改

@Controller
@RequestMapping("admin")
public class AdminController 

@RequestMapping(value = "saveCategory", method = RequestMethod.POST)
    public String saveCategories(@ModelAttribute("categories") Categories theCategories) {

        categoriesService.saveCat(theCategories);
        return "redirect:/admin/allCategories";
    }

<form action="#" th:action="@{/saveCategory}" th:object="${categories}"
                      method="post">

收件人

@Controller
@RequestMapping("/admin")
public class AdminController 

@RequestMapping(value = "/saveCategory", method = RequestMethod.POST)
    public String saveCategories(@ModelAttribute("categories") Categories theCategories) {

        categoriesService.saveCat(theCategories);
        return "redirect:/admin/allCategories";
    }

<form action="#" th:action="@{/admin/saveCategory}" th:object="${categories}"
                      method="post">