gpt4 book ai didi

java - 如果数据库记录中有 LocalDate 属性,如何按月和年过滤数据库记录?

转载 作者:行者123 更新时间:2023-12-02 11:38:15 25 4
gpt4 key购买 nike

我的数据库中有一个 Workday 对象。我想显示员工每天在 Spring Web 应用程序上工作的小时数 (hoursWorked)。

为了做到这一点,我需要按日期(仅是月份和年份)过滤工作日。然后将它们显示在所选特定时期(即 2018-01)的日历上。

执行此操作的最佳做​​法是什么? Web 应用程序上有 2 个下拉列表。首先选择月份,其次选择年份。然后将两个字符串都转换为 LocalDate?我对 Spring 很陌生,找不到合适的解决方案。

public class Workday {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private Integer hoursWorked;
private Integer advancePayment;
private LocalDate date;

@ManyToOne
private Employee employee;

-------------

所以,这可能有点困难。我有 4 个 HTML View :

  • 主要
  • 员工
  • 特定员工
  • 新员工

我确实有一个 EmployeeController 来处理渲染所有 View 以及处理新的 Employee 创建。

@Controller
public class EmployeeController {

private final EmployeeService employeeService;
private final WorkdayService workdayService;

public EmployeeController(EmployeeService employeeService, WorkdayService workdayService) {
this.employeeService = employeeService;
this.workdayService = workdayService;
}

//Renders Employees view. There's a list of employees with some details.
@RequestMapping("/employees")
public String showEmployees(Model model) {

model.addAttribute("employees", employeeService.getEmployees());

return "employees";
}

//Renders a specific Employee view that contains a specific Employee information.
//Like name, address etc. There's also a list of hours worked a day that I want to filter.
//This view includes the <input type="month"> to actually get the yyyy-mm date required for filtering.

@RequestMapping("/employee/{id}")
public String showSpecificEmployee(@PathVariable String id, Model model){

model.addAttribute("employee", employeeService.findEmployeeById(new Long(id)));
//Newly created date command object to retrieve the date String from the form
model.addAttribute("date", new DateCommand());

return "specificEmployee";
}

//A method rendering the new Employee forms and binding that Employee command object to it.
@GetMapping
@RequestMapping("/new_employee")
public String newEmployee(Model model){

model.addAttribute("employee", new EmployeeCommand());

return "newEmployee";
}

//Saving the Employee command object with forms information to the database and rendering the specific employee view with the new employee.
@PostMapping
@RequestMapping("employee")
public String saveOrEdit(@ModelAttribute EmployeeCommand commandObject){

EmployeeCommand savedCommand = employeeService.saveEmployeeCommand(commandObject);

return "redirect:/employee/" + savedCommand.getId();
}

//New employee view is also used for editing employees.
@GetMapping
@RequestMapping("employee/{id}/edit")
public String editEmployee(@PathVariable String id, Model model){

model.addAttribute("employee", employeeService.findCommandById(new Long(id)));

return "newEmployee";
}

//A method used for deleting employees. Returns employees view.
@GetMapping
@RequestMapping("employee/{id}/delete")
public String deleteEmployee(@PathVariable String id, Model model){

employeeService.deleteEmployee(new Long(id));
model.addAttribute("employees", employeeService.getEmployees());

return "employees";
}

//That's the one I need to figure out. Since the Date I want to get is on the specificEmployee view, I completly do not know what to return and what @RequestMapping to use.
@PostMapping
@RequestMapping("/employee/{id}")
public String updateWorkmonth(@ModelAttribute String date){

String data = date;
System.out.println(data);

return "redirect://employee/{id}";
}

}

以及 HTML 表单代码。我再次不知道 th:action 应该指向哪里。

<form th:object="${date}" th:action="@{}">
Choose a month:
<input type="month" th:field="*{actualDate}">
<input type="submit">
</form>

如果您还有其他需要,请随时询问。我也可以提供一个 github 存储库。

最佳答案

在您的表单中,您需要用户可以输入开始日期和结束日期的内容。这两个日期将被传递给您的 Controller 。

在您的获取请求中,您可以执行类似/employees?startDate=20180212&endDate=20180214 的操作

然后对于您的 Controller :

@RequestMapping("/employees")
public String showEmployees(Model model, @RequestParam(value="startDate") String startDate, @RequestParam(value="endDate") String endDate) {
SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMDD");
Date start = sdf.parse(startDate);
Date end = sdf.parse(endDate);
model.addAttribute("employees", employeeService.getEmployees(start, end));

return "employees";
}

然后用逻辑来测试 startDate 和/或 endDate 在解析和相应处理之前是否为 null。

然后您可以创建一个 Spring 查询,例如

List<Workday> findByDateBetween(Date date1, Date date2);

这将返回两个日期之间的工作日列表。

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation

关于java - 如果数据库记录中有 LocalDate 属性,如何按月和年过滤数据库记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48772064/

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