如何将选定的for循环数据保存到mysql数据库中?

我是 Spring Boot 的新手,遇到问题已经有一段时间了。在我的 thymeleaf 页面中,我使用了 for 循环,我需要将当前迭代的项目保存在数据库中。(例如:我在 for 循环中使用一周中的几天,用户可以为我的 for loo 中的每个项目选择一个主题,那么 5 个 sql 行应该与日期和主题一起保存),但目前它不保存日期并将 2 个选定的主题保存在 1 个 sql 行中

添加时间表 thymeleaf 视图

<!DOCTYPE html>

<html xmlns:th="https://www.thymeleaf.org">

<head>

    <link rel="stylesheet" type="text/css" href="static/css/timeTableMapping.css" th:href="@{/css/timeTableMapping.css}">

    <meta charset="UTF-8">

    <title>Time Table</title>

</head>

<body>




</form>

<div class="container2">

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

        <table border="0" cell[adding="10">

            <tr>

                <td><h1>Time Table:</h1></td>

                <td>

                    <select th:field="*{time_table_code}">

                        <option value="">Choose..</option>

                        <option th:each="timeTable: ${timeTables}" th:value="${timeTable.name}" th:text="${timeTable.name}"/>

                    </select>

                </td>

            </tr>

        </table>


        <table border="1" >

            <thead>

            <tr>

            </tr>

            <br>


            <th:block th:each="day : ${days}">

                <th th:value="${day.name}" th:text="${day.name}"></th>

            </th:block>



            </thead>

            <tbody>





                    <th:block th:each="day : ${days}">

                        <td>

                            <select th:field="*{subject_code}">

                                <option value=""></option>

                                <option th:each="subject: ${subjects}" th:value="${subject.subject_code}" th:text="${subject.name}"/>


                            </select>

                        </td>

                    </th:block>



            <tr>

                <td colspan="2">

                    <button type="submit">Save</button>


                </td>

            </tr>


            </tbody>

        </table>

    </form>

</div>


</body>

</html>


富国沪深
浏览 65回答 2
2回答

精慕HU

在 DAO 中自动装配 TimeTableMappingRepository 之前,您应该在 TimeTableMappingRepository 接口上添加 @Repository。像这样@Repository 公共接口 TimeTableMappingRepository 扩展 JpaRepository {}通过在接口上添加@Repository,spring-data-jpa将在运行时创建该接口的代理对象,您将能够自动装配它。

叮当猫咪

您正在获取多个数据。所以你应该使用List<TimeTableMapping> timeTableMapping在您的服务实现中,您可以传递您获得的列表并保存所有@Overridepublic void saveAll(List<TimeTableMapping> timeTableMapping) {&nbsp; &nbsp; List<TimeTableMapping> timetable=new ArrayList<>();&nbsp; &nbsp; repository.saveAll(timetable).forEach(timeTableMapping::add);&nbsp; &nbsp;// TimeTable repository}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java