Spring Boot (Building a web application - JPA integration) - Part 3

in #spring7 years ago (edited)

This is the third part of our Spring Boot application.

The application is going to be called "My List" and it is a simple to-do list and task manager that helps you get stuff done. Whether you’re sharing a grocery list with a loved one, working on a project, or planning a vacation.

At this part we are going to build our List Domain wich means that we'll be able to create as many lists as we want.

First, lets create a List Domain. In this case we will create an Entity called "ListDomain". Our list is gonna have and Id, a name and a date.


import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import lombok.Data;

@Entity
@Data
public class ListDomain implements Serializable {
	private static final long serialVersionUID = 1L;

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

	private String name;
	
	private Date date;

	public ListDomain(String name) {
		this.name = name;
		this.date = new Date();
	}
	
	public ListDomain() {
	}

}

Second, lets create an interface to be able to connect our list to the data base.


import org.springframework.data.jpa.repository.JpaRepository;

import br.com.namom.todolist.domain.ListDomain;

public interface ListRepository extends JpaRepository<ListDomain, Long> {

}

Third, before to create our controller, lets create our view part. We have created a simple index page that you can find here. Then, we have to create another page to handle our list, i.e, a page where we can create and list them.

Fouth, now lets create our controller. It is resposible to manage our routes, list and save our objects.


import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import br.com.namom.todolist.domain.ListDomain;
import br.com.namom.todolist.persistence.ListRepository;

@Controller
public class ListController {

	@Autowired
	private ListRepository repoList;
	
	@Transactional
	@RequestMapping("mylists")
	public String lists(Model model) {
		Iterable lists = repoList.findAll();
		model.addAttribute("lists", lists);
		return "lists";
	}
	
	@RequestMapping("")
	public String index(Model model) {
		return "index";
	}

	@Transactional
	@RequestMapping(value = "savelist", method = RequestMethod.POST)
	public String save(@RequestParam("name") String name, Model model) {
		ListDomain list = new ListDomain(name);
		repoList.save(list);
		return this.lists(model);
	}

}

That's it.

Just run the application and you will see it working.

Checkout the full code on my github.

Coin Marketplace

STEEM 0.13
TRX 0.34
JST 0.035
BTC 108074.45
ETH 4399.12
USDT 1.00
SBD 0.83