为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

富国沪深 2023-09-27 10:33:38
我是 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>
查看完整描述

2 回答

?
精慕HU

TA贡献1845条经验 获得超8个赞

在 DAO 中自动装配 TimeTableMappingRepository 之前,您应该在 TimeTableMappingRepository 接口上添加 @Repository。

像这样

@Repository 公共接口 TimeTableMappingRepository 扩展 JpaRepository {}

通过在接口上添加@Repository,spring-data-jpa将在运行时创建该接口的代理对象,您将能够自动装配它。


查看完整回答
反对 回复 2023-09-27
?
叮当猫咪

TA贡献1776条经验 获得超12个赞

您正在获取多个数据。所以你应该使用List<TimeTableMapping> timeTableMapping


在您的服务实现中,您可以传递您获得的列表并保存所有


@Override

public void saveAll(List<TimeTableMapping> timeTableMapping) {


    List<TimeTableMapping> timetable=new ArrayList<>();

    repository.saveAll(timetable).forEach(timeTableMapping::add);   // TimeTable repository

}


查看完整回答
反对 回复 2023-09-27
  • 2 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信