gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:27:37 28 4
gpt4 key购买 nike

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

添加时间表 thymeleaf View

<!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>

时间表映射 Controller

@Controller
public class TimeTableMappingController {
@Autowired
private TimeTableMappingDAO timeTableMappingDAO;

@Autowired
private TimeTableDAO timeTableDAO;

@Autowired
private SubjectDAO subjectDAO;

@Autowired
private StudentGroupDAO studentGroupDAO;

@Autowired
private DayDAO dayDAO;



@RequestMapping("/timeTableMapping")
public String viewHomePage(Model model){
List<TimeTableMapping> timeTableMappingDetails= timeTableMappingDAO.findAll();
model.addAttribute("timeTableMappingDetails",timeTableMappingDetails);
return "timeTableMapping";
}



@RequestMapping("/timeTableMapping/new")
public String addTimeTableMapping(Model model){
System.out.println("hey");
System.out.println("hey");
TimeTableMapping timeTableMapping =new TimeTableMapping();
model.addAttribute("timeTableMapping",timeTableMapping);

List<TimeTable> timeTableDetail = timeTableDAO.findAll();
model.addAttribute("timeTables", timeTableDetail);

List<Subject> subjectDetail = subjectDAO.findAll();
model.addAttribute("subjects", subjectDetail);

List<Day> dayDetails = dayDAO.findAll();
model.addAttribute("days",dayDetails);

List<Time> timeDetails = timeDAO.findAll();
model.addAttribute("times",timeDetails);

return "addTimeTableMapping";
}

@RequestMapping(value="/timeTableMapping/save",method= RequestMethod.POST)
public String saveTimeTable(@ModelAttribute("timeTableMapping") TimeTableMapping timeTableMapping){

timeTableMappingDAO.save(timeTableMapping);
return "redirect:/timeTableMapping";
}

/*
@RequestMapping(value="/timeTableMapping/saveAll",method= RequestMethod.POST)
public String saveAll(@ModelAttribute("timeTableMapping") TimeTableMapping timeTableMapping){
timeTableDAO.saveAll(timeTableMapping);
return "redirect:/timeTableMapping";
}*/

@RequestMapping("/timeTableMapping/edit/{id}")
public ModelAndView updateTimeTable(@PathVariable(name="id")Long id){
ModelAndView mav=new ModelAndView(("updateTimeTable"));

TimeTable timeTable = timeTableDAO.findById(id);
mav.addObject("timeTable",timeTable);

List<Subject> subjectDetails = subjectDAO.findAll();
mav.addObject("subjects", subjectDetails);

List<StudentGroup> studentGroupDetails = studentGroupDAO.findAll();
mav.addObject("studentGroups",studentGroupDetails);

List<Day> dayDetails = dayDAO.findAll();
mav.addObject("days",dayDetails);

List<Time> timeDetails = timeDAO.findAll();
mav.addObject("times",timeDetails);

return mav;
}

@RequestMapping("/timeTableMapping/delete/{id}")
public String deleteProduct(@PathVariable(name="id") Long id){
timeTableDAO.delete(id);
return "redirect:/timeTable";
}
}

添加时间表 DAO

@Service
public class TimeTableMappingDAO {

@Autowired
TimeTableRepository timeTableRepository;

@Autowired
TimeTableMappingRepository timeTableMappingRepository;

//to save a time table
public TimeTableMapping save(TimeTableMapping timeTableMapping){
return timeTableMappingRepository.save(timeTableMapping);
}

//@Override
public void saveAll(List<TimeTableMapping> timeTableMapping) {

List<TimeTableMapping> timetable=new ArrayList<>();
timeTableMappingRepository.saveAll(timetable).forEach(timeTableMapping::add); // TimeTable repository
}
/*
public TimeTableMapping saveAll(TimeTableMapping timeTableMapping){
return timeTableMappingRepository.saveAll(timeTableMapping);
}

*/
//to search all
public List<TimeTableMapping> findAll(){
return timeTableMappingRepository.findAll();
}

//get by id
public TimeTableMapping findById(Long id){
return timeTableMappingRepository.findById(id).orElse(null);
}


//delete
public void delete(Long id){
timeTableMappingRepository.deleteById(id);
}


}

时间表映射存储库

  @Repository
public interface TimeTableMappingRepository extends JpaRepository<TimeTableMapping, Long> {
}

最佳答案

在 DAO 中 Autowiring TimeTableMappingRepository 之前,您应该在接口(interface) TimeTableMappingRepository 上添加 @Repository。

就像这样

@存储库公共(public)接口(interface) TimeTableMappingRepository 扩展 JpaRepository {}

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

关于java - 如何将选定的for循环数据保存到mysql数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58230478/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com