如何在不使用注释的情况下提取与TodoList关联的Todo项目?

时间:2018-01-17 07:04:12

标签: java spring spring-mvc spring-boot spring-data

创建一个待办事项应用程序,其中TodoList有许多与之关联的Todos

e.g。 "购物" TodoList有Todos,如食物,衣服等。

我想以某种方式编写代码,以便当用户点击"购物"在索引页面上,将显示一个显示页面,其中包含与之关联的所有待办事项。

我想我需要在getOneTodoList方法中编写代码,但我不确定应该如何构造它。

TodoListController.java

package com.teamlab.todolist.web;

import com.teamlab.todolist.domain.TodoList;
import com.teamlab.todolist.repository.TodoListRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
@RequestMapping("/todoLists")
public class TodoListController {

    @Autowired
    TodoListRepository todoListRepository;

    @GetMapping
    public String getAllTodoLists(Model model) {
        List<TodoList> todoLists = todoListRepository.findAll();
        model.addAttribute("todoLists", todoLists);
        return "todoLists/index";
    }

    @GetMapping("{id}")
    public String getOneTodoList(@PathVariable Long id, Model model) {
        TodoList todoList = todoListRepository.findOne(id);
        model.addAttribute("todoList", todoList);
        return "todoLists/show";
    }

    @PostMapping
    public String createTodoList(@ModelAttribute TodoList todoList) {
        todoListRepository.save(todoList);
        return "redirect:/todoLists";
    }

}

TodoList.java

package com.teamlab.todolist.domain;

import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;

@Entity
public class TodoList {

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

    private String todoListName;

    public TodoList() {
    }

    public TodoList(String todoListName) {
        this.todoListName = todoListName;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTodoListName() {
        return todoListName;
    }

    public void setTodoListName(String todoListName) {
        this.todoListName = todoListName;
    }
}

Todo.java

package com.teamlab.todolist.domain;

import javax.persistence.*;
import java.time.LocalDate;

@Entity
public class Todo {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String todoName;
    private LocalDate due_date;
    private Boolean completed;

    public Todo() {
    }

    public Todo(String todoName, LocalDate due_date, Boolean complete) {
        this.todoName = todoName;
        this.due_date = due_date;
        this.completed = complete;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTodoName() {
        return todoName;
    }

    public void setTodoName(String todoName) {
        this.todoName = todoName;
    }

    public LocalDate getDue_date() {
        return due_date;
    }

    public void setDue_date(LocalDate due_date) {
        this.due_date = due_date;
    }

    public Boolean getComplete() {
        return completed;
    }

    public void setComplete(Boolean complete) {
        this.completed = complete;
    }
}

1 个答案:

答案 0 :(得分:0)

我假设您有一个TodoList类,其中包含表示每种类型的待办事项及其getter和setter的待办事项列表对象。像这样。

class TodoList{
    Shopping shopping;
    Work work;
    //getters and setters
}

您可以将您的ID传递给DAO图层并检索与该用户关联的TodoList。然后调用与该ID相对应的getter方法。像这样的东西。在这里,我假设你正在使用Hibernate。

@Repository
public class MapExample {
@Autowired
SessionFactory sessionFactory;

@Transactional
public String todoListRepository(String id) {
    String query = "from TodoList WHERE username = :username";
    Query query = sessionFactory.getCurrentSession()
            .createQuery(query).setString("username","admin");
    TodoList todoList = query.uniqueResult();
    if(id.equals("work")){
        model.addAttribute("todoList", todoList.getShopping())
    }
    if(id.equals("shopping")){
        model.addAttribute("todoList", todoList.getWork())
    }
    return "todoLists/show";
}

}