Topic: CRUD with Spring MVC
You need to make a task list (todo-list) with the ability to view the list of tasks, add new tasks, edit and delete existing tasks.
It is advisable not to use Spring Boot, but to figure out how to configure everything yourself.
What to do:
- Deploy sql script
- Create a new Maven project.
- Add dependencies that are needed to work with MySQL, Hibernate, Spring, Spring MVC, Thymeleaf.
- Add an entity layer (domain package) to the project. Add a
Task
class - it will be responsible for the task in the to-do list. Required fields:description
– task description,status
– task execution status. Use enum for status: - Add the config package. Inside the package create the necessary classes for setting up a Spring MVC application, working with the database (via Hibernate) and all other settings.
- Add a dao layer, which should contain the
TaskDAO
class, which will be responsible for working with the database. The methods that should be there are CRUD and paging support. - Add a service layer in which you need the logic for creating and editing tasks.
- Now the controller layer: it should have the following methods:
- get a list of tasks (including paging)
- add new task
- edit an existing task
- delete task
- The last step is a template (html or js file). Optionally, you can move styles and scripts into different files by type. As usual, for a backend developer, functionality is important, not appearance, so the application visuals are at your discretion. Mine turned out like this:
CREATE DATABASE IF NOT EXISTS `todo` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
USE `todo`;
DROP TABLE IF EXISTS `task`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `task` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`description` varchar(100) NOT NULL,
`status` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
LOCK TABLES `task` WRITE;
/*!40000 ALTER TABLE `task` DISABLE KEYS */;
INSERT IGNORE INTO `task` VALUES (1,'aaa',1),(2,'bbb',2),(3,'ccc',0),(4,'ddd',1),(5, 'eee',2),(6,'fff',0),(7,'ggg',1),(8,'hhh',2),(9,'jjj',0),(10, 'kkk',1),(11,'lll',2),(12,'mmm',0),(13,'nnn',1),(14,'ooo',2),(15, 'ppp',0);
/*!40000 ALTER TABLE `task` ENABLE KEYS */;
UNLOCK TABLES;
public enum Status {
IN_PROGRESS,
DONE,
PAUSED
}
Think about what the methods and their mappings should be.
Optional task:
Pack our application into a docker container, add a docker-compose file, in which we configure the operation of the application-database combination in docker containers.
Project execution:
GO TO FULL VERSION