1

I have referred to -> Spring MVC how to display data from database into a table

My aim is to try and understand what is the syntax and process to create queries, and whether I'm correct.

The following code tries to display all Order entities.

@AutoWired
private OrderService orderService;

@RequestMapping("/")
//public String orderPage(Model model) {
//  model.addAttribute("orderList", SomeApp.getStore().getOrderList());
//    return "form/orderPage"};
// this is the code I am trying to translate below

    @ResponseBody 
    public List<order> orderList(Map<String, Object> model) {
        List<order> orderList = OrderService.findALl();
        //orderRepository.findAll <- where does this come in? is it needed at all
        return orderList;
      }  

If the Service layer is not being used, in my Repo do I only state

List<Order> findAll();

Additional Info: Service layer is not used in this project and instead business logic will be in Controller (partly why I'm confused as to what code goes where)

0

1 Answer 1

1

You need to @Autowire the OrderRepository so that you can call orderRepository.findAll() in your Controller as shown below. For that, you also need to define the OrderRepository and Order Entity classes.

Controller:

@Controller
public class Controller {

    @AutoWired
    private OrderRepository orderRepository;

    @RequestMapping("/")
    @ResponseBody 
    public List<order> orderList(Map<String, Object> model) {
        List<order> orderList = OrderService.findALl();
        orderRepository.findAll();
        return orderList;
      }  

}

Repository:

@Repository
public interface OrderRepository extends JpaRepository<Order, Integer> {
    public Order findAll();
}

Entity:

@Entity
public class Order {

    //add your entity fields with getters and setters
}

You can refer here for spring-data-jpa basic example.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks! I have already defined those so I'll change the autowire on my controller accordingly
since the repos are extending CRUD I would just replace jpa repository with that right? And add an import statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.